git-annex/Assistant.hs

107 lines
3.7 KiB
Haskell
Raw Normal View History

2012-06-13 16:36:33 +00:00
{- git-annex assistant daemon
-
- Copyright 2012 Joey Hess <joey@kitenet.net>
-
- Licensed under the GNU GPL version 3 or higher.
-
- Overview of threads and MVars, etc:
-
- Thread 1: parent
- The initial thread run, double forks to background, starts other
- threads, and then stops, waiting for them to terminate,
- or for a ctrl-c.
2012-06-13 23:32:09 +00:00
- Thread 2: watcher
2012-06-13 16:36:33 +00:00
- Notices new files, and calls handlers for events, queuing changes.
- Thread 3: inotify internal
- Used by haskell inotify library to ensure inotify event buffer is
- kept drained.
2012-06-13 23:32:09 +00:00
- Thread 4: inotify startup scanner
- Scans the tree and registers inotify watches for each directory.
2012-06-13 16:36:33 +00:00
- A MVar lock is used to prevent other inotify handlers from running
- until this is complete.
- Thread 5: committer
- Waits for changes to occur, and runs the git queue to update its
- index, then commits.
- Thread 6: pusher
- Waits for commits to be made, and pushes updated branches to remotes,
- in parallel. (Forks a process for each git push.)
2012-06-25 20:38:12 +00:00
- Thread 7: push retryer
- Runs every 30 minutes when there are failed pushes, and retries
- them.
- Thread 8: merger
- Waits for pushes to be received from remotes, and merges the
- updated branches into the current branch. This uses inotify
- on .git/refs/heads, so there are additional inotify threads
- associated with it, too.
2012-06-25 20:38:12 +00:00
- Thread 9: status logger
2012-06-13 16:36:33 +00:00
- Wakes up periodically and records the daemon's status to disk.
2012-06-25 20:38:12 +00:00
- Thread 10: sanity checker
2012-06-13 23:32:09 +00:00
- Wakes up periodically (rarely) and does sanity checks.
2012-06-13 16:36:33 +00:00
-
- ThreadState: (MVar)
- The Annex state is stored here, which allows resuscitating the
- Annex monad in IO actions run by the inotify and committer
- threads. Thus, a single state is shared amoung the threads, and
- only one at a time can access it.
- DaemonStatusHandle: (MVar)
- The daemon's current status. This MVar should only be manipulated
- from inside the Annex monad, which ensures it's accessed only
- after the ThreadState MVar.
- ChangeChan: (STM TChan)
- Changes are indicated by writing to this channel. The committer
- reads from it.
- CommitChan: (STM TChan)
- Commits are indicated by writing to this channel. The pusher reads
- from it.
- FailedPushMap (STM TMVar)
- Failed pushes are indicated by writing to this TMVar. The push
- retrier blocks until they're available.
2012-06-13 16:36:33 +00:00
-}
module Assistant where
import Common.Annex
import Assistant.ThreadedMonad
import Assistant.DaemonStatus
2012-06-19 06:40:21 +00:00
import Assistant.Changes
2012-06-22 17:39:44 +00:00
import Assistant.Commits
2012-06-25 20:38:12 +00:00
import Assistant.Pushes
2012-06-25 20:10:10 +00:00
import Assistant.Threads.Watcher
import Assistant.Threads.Committer
import Assistant.Threads.Pusher
import Assistant.Threads.Merger
import Assistant.Threads.SanityChecker
2012-06-13 16:36:33 +00:00
import qualified Utility.Daemon
import Utility.LogFile
import Control.Concurrent
startDaemon :: Bool -> Bool -> Annex ()
startDaemon assistant foreground
2012-06-13 16:36:33 +00:00
| foreground = do
showStart (if assistant then "assistant" else "watch") "."
2012-06-13 16:36:33 +00:00
go id
| otherwise = do
logfd <- liftIO . openLog =<< fromRepo gitAnnexLogFile
pidfile <- fromRepo gitAnnexPidFile
go $ Utility.Daemon.daemonize logfd (Just pidfile) False
where
go a = withThreadState $ \st -> do
checkCanWatch
2012-06-13 16:36:33 +00:00
dstatus <- startDaemonStatus
2012-06-13 18:02:40 +00:00
liftIO $ a $ do
changechan <- newChangeChan
2012-06-22 17:39:44 +00:00
commitchan <- newCommitChan
pushmap <- newFailedPushMap
2012-06-22 17:39:44 +00:00
_ <- forkIO $ commitThread st changechan commitchan
_ <- forkIO $ pushThread st commitchan pushmap
_ <- forkIO $ pushRetryThread st pushmap
_ <- forkIO $ mergeThread st
2012-06-13 18:02:40 +00:00
_ <- forkIO $ daemonStatusThread st dstatus
_ <- forkIO $ sanityCheckerThread st dstatus changechan
2012-06-17 18:25:02 +00:00
-- Does not return.
2012-06-13 18:02:40 +00:00
watchThread st dstatus changechan
2012-06-13 16:36:33 +00:00
stopDaemon :: Annex ()
stopDaemon = liftIO . Utility.Daemon.stopDaemon =<< fromRepo gitAnnexPidFile