git annex watch --stop

This commit is contained in:
Joey Hess 2012-06-11 02:01:20 -04:00
parent 8539a7bde8
commit d0a0a6ae21
3 changed files with 49 additions and 20 deletions

View file

@ -50,27 +50,36 @@ data Change = Change
deriving (Show) deriving (Show)
def :: [Command] def :: [Command]
def = [withOptions [foregroundOption] $ def = [withOptions [foregroundOption, stopOption] $
command "watch" paramPaths seek "watch for changes"] command "watch" paramPaths seek "watch for changes"]
seek :: [CommandSeek] seek :: [CommandSeek]
seek = [withFlag foregroundOption $ withNothing . start] seek = [withFlag stopOption $ \stopdaemon ->
withFlag foregroundOption $ \foreground ->
withNothing $ start foreground stopdaemon]
foregroundOption :: Option foregroundOption :: Option
foregroundOption = Option.flag [] "foreground" "do not daemonize" foregroundOption = Option.flag [] "foreground" "do not daemonize"
start :: Bool -> CommandStart stopOption :: Option
start foreground = notBareRepo $ withStateMVar $ \st -> do stopOption = Option.flag [] "stop" "stop daemon"
if foreground
then do start :: Bool -> Bool -> CommandStart
showStart "watch" "." start foreground stopdaemon = notBareRepo $ do
liftIO $ watch st if stopdaemon
else do then liftIO . stopDaemon =<< fromRepo gitAnnexPidFile
logfd <- liftIO . openLog =<< fromRepo gitAnnexLogFile else withStateMVar $ startDaemon (not foreground)
pidfile <- fromRepo gitAnnexPidFile
liftIO $ daemonize logfd (Just pidfile) False $ watch st
stop stop
startDaemon :: Bool -> MVar Annex.AnnexState -> Annex ()
startDaemon False st = do
showStart "watch" "."
liftIO $ watch st
startDaemon True st = do
logfd <- liftIO . openLog =<< fromRepo gitAnnexLogFile
pidfile <- fromRepo gitAnnexPidFile
liftIO $ daemonize logfd (Just pidfile) False $ watch st
watch :: MVar Annex.AnnexState -> IO () watch :: MVar Annex.AnnexState -> IO ()
#if defined linux_HOST_OS #if defined linux_HOST_OS
watch st = withINotify $ \i -> do watch st = withINotify $ \i -> do

View file

@ -27,7 +27,7 @@ daemonize logfd pidfile changedirectory a = do
_ <- forkProcess child2 _ <- forkProcess child2
out out
child2 = do child2 = do
maybe noop lockPidFile pidfile maybe noop (lockPidFile True alreadyrunning) pidfile
when changedirectory $ when changedirectory $
setCurrentDirectory "/" setCurrentDirectory "/"
nullfd <- openFd "/dev/null" ReadOnly Nothing defaultFileFlags nullfd <- openFd "/dev/null" ReadOnly Nothing defaultFileFlags
@ -39,12 +39,31 @@ daemonize logfd pidfile changedirectory a = do
redir newh h = do redir newh h = do
closeFd h closeFd h
dupTo newh h dupTo newh h
alreadyrunning = error "Daemon is already running."
out = exitImmediately ExitSuccess out = exitImmediately ExitSuccess
lockPidFile :: FilePath -> IO () lockPidFile :: Bool -> IO () -> FilePath -> IO ()
lockPidFile file = void $ do lockPidFile write onfailure file = do
fd <- openFd file ReadWrite (Just stdFileMode) defaultFileFlags fd <- openFd file ReadWrite (Just stdFileMode) defaultFileFlags
catchIO when (write) $ void $
(setLock fd (WriteLock, AbsoluteSeek, 0, 0)) fdWrite fd =<< show <$> getProcessID
(const $ error "Daemon is already running.") catchIO (setLock fd (locktype, AbsoluteSeek, 0, 0)) (const onfailure)
fdWrite fd =<< show <$> getProcessID where
locktype
| write = WriteLock
| otherwise = ReadLock
{- Stops the daemon.
-
- The pid file is used to get the daemon's pid.
-
- To guard against a stale pid, try to take a nonblocking shared lock
- of the pid file. If this *fails*, the daemon must be running,
- and have the exclusive lock, so the pid file is trustworthy.
-}
stopDaemon :: FilePath -> IO ()
stopDaemon pidfile = lockPidFile False go pidfile
where
go = do
pid <- readish <$> readFile pidfile
maybe noop (signalProcess sigTERM) pid

View file

@ -177,7 +177,8 @@ subdirectories).
background, you no longer need to manually run git commands when background, you no longer need to manually run git commands when
manipulating your files. manipulating your files.
To not daemonize, run with --foreground To not daemonize, run with --foreground ; to stop a running daemon,
run with --stop
# REPOSITORY SETUP COMMANDS # REPOSITORY SETUP COMMANDS