git-annex/Assistant.hs

254 lines
9 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-07-27 01:51:47 +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.
2012-07-27 01:51:47 +00:00
- Thread 5: Committer
2012-06-13 16:36:33 +00:00
- Waits for changes to occur, and runs the git queue to update its
- index, then commits. Also queues Transfer events to send added
- files to other remotes.
2012-07-27 01:51:47 +00:00
- 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-07-27 01:51:47 +00:00
- Thread 7: PushRetryer
2012-06-25 20:38:12 +00:00
- Runs every 30 minutes when there are failed pushes, and retries
- them.
2012-07-27 01:51:47 +00:00
- 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-07-27 01:51:47 +00:00
- Thread 9: TransferWatcher
- Watches for transfer information files being created and removed,
- and maintains the DaemonStatus currentTransfers map.
- (This uses inotify on .git/annex/transfer/, so there are
- additional inotify threads associated with it, too.)
2012-08-28 18:04:28 +00:00
- Thread 10: TransferPoller
- Polls to determine how much of each ongoing transfer is complete.
- Thread 11: Transferrer
2012-07-05 20:34:20 +00:00
- Waits for Transfers to be queued and does them.
2012-08-28 18:04:28 +00:00
- Thread 12: StatusLogger
2012-06-13 16:36:33 +00:00
- Wakes up periodically and records the daemon's status to disk.
2012-08-28 18:04:28 +00:00
- Thread 13: SanityChecker
2012-06-13 23:32:09 +00:00
- Wakes up periodically (rarely) and does sanity checks.
2012-08-28 18:04:28 +00:00
- Thread 14: MountWatcher
- Either uses dbus to watch for drive mount events, or, when
- there's no dbus, polls to find newly mounted filesystems.
- Once a filesystem that contains a remote is mounted, updates
- state about that remote, pulls from it, and queues a push to it,
- as well as an update, and queues it onto the
- ConnectedRemoteChan
2012-08-28 18:04:28 +00:00
- Thread 15: NetWatcher
- Deals with network connection interruptions, which would cause
- transfers to fail, and can be recovered from by waiting for a
- network connection, and syncing with all network remotes.
- Uses dbus to watch for network connections, or when dbus
- cannot be used, assumes there's been one every 30 minutes.
2012-08-28 18:04:28 +00:00
- Thread 16: TransferScanner
- Does potentially expensive checks to find data that needs to be
- transferred from or to remotes, and queues Transfers.
2012-09-08 19:07:44 +00:00
- Uses the ScanRemotes map.a
- Thread 17: PairListener
- Listens for incoming pairing traffic, and takes action.
- Thread 18: ConfigMonitor
- Triggered by changes to the git-annex branch, checks for changed
- config files, and reloads configs.
2012-11-03 18:16:17 +00:00
- Thread 19: XMPPClient
- Built-in XMPP client.
- Thread 20: WebApp
2012-07-27 01:51:47 +00:00
- Spawns more threads as necessary to handle clients.
- Displays the DaemonStatus.
- Thread 21: Glacier
- Deals with retrieving files from Amazon Glacier.
2012-06-13 16:36:33 +00:00
-
- ThreadState: (MVar)
- The Annex state is stored here, which allows resuscitating the
2012-06-28 17:04:02 +00:00
- Annex monad in IO actions run by the watcher and committer
2012-06-13 16:36:33 +00:00
- threads. Thus, a single state is shared amoung the threads, and
- only one at a time can access it.
- DaemonStatusHandle: (STM TMVar)
- The daemon's current status.
2012-06-13 16:36:33 +00:00
- 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.
- TransferQueue (STM TChan)
- Transfers to make are indicated by writing to this channel.
- TransferSlots (QSemN)
- Count of the number of currently available transfer slots.
- Updated by the transfer watcher, this allows other threads
- to block until a slot is available.
- This MVar should only be manipulated from inside the Annex monad,
- which ensures it's accessed only after the ThreadState MVar.
- ScanRemotes (STM TMVar)
- Remotes that have been disconnected, and should be scanned
- are indicated by writing to this TMVar.
- BranchChanged (STM SampleVar)
- Changes to the git-annex branch are indicated by updating this
- SampleVar.
- NetMessager (STM TChan, TMVar, SampleVar)
- Used to feed messages to the built-in XMPP client, handle
- pushes, and signal it when it needs to restart due to configuration
- or networking changes.
- UrlRenderer (MVar)
- A Yesod route rendering function is stored here. This allows
- things that need to render Yesod routes to block until the webapp
- has started up and such rendering is possible.
2012-06-13 16:36:33 +00:00
-}
{-# LANGUAGE CPP #-}
2012-06-13 16:36:33 +00:00
module Assistant where
import Assistant.Common
2012-06-13 16:36:33 +00:00
import Assistant.DaemonStatus
2012-10-30 18:34:48 +00:00
import Assistant.NamedThread
2012-10-29 23:07:10 +00:00
import Assistant.Types.ThreadedMonad
import Assistant.Threads.DaemonStatus
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.TransferWatcher
2012-07-05 20:34:20 +00:00
import Assistant.Threads.Transferrer
2012-06-25 20:10:10 +00:00
import Assistant.Threads.SanityChecker
#ifdef WITH_CLIBS
import Assistant.Threads.MountWatcher
#endif
import Assistant.Threads.NetWatcher
import Assistant.Threads.TransferScanner
2012-08-28 18:04:28 +00:00
import Assistant.Threads.TransferPoller
import Assistant.Threads.ConfigMonitor
import Assistant.Threads.Glacier
#ifdef WITH_WEBAPP
import Assistant.WebApp
import Assistant.Threads.WebApp
2012-09-08 19:07:44 +00:00
#ifdef WITH_PAIRING
import Assistant.Threads.PairListener
#endif
#ifdef WITH_XMPP
import Assistant.Threads.XMPPClient
#endif
#else
#warning Building without the webapp. You probably need to install Yesod..
2013-04-04 05:48:26 +00:00
import Assistant.Types.UrlRenderer
#endif
2012-06-13 16:36:33 +00:00
import qualified Utility.Daemon
import Utility.LogFile
import Utility.ThreadScheduler
import qualified Build.SysConfig as SysConfig
import System.Log.Logger
import Network.Socket (HostName)
stopDaemon :: Annex ()
stopDaemon = liftIO . Utility.Daemon.stopDaemon =<< fromRepo gitAnnexPidFile
{- Starts the daemon. If the daemon is run in the foreground, once it's
- running, can start the browser.
-
- startbrowser is passed the url and html shim file, as well as the original
- stdout and stderr descriptors. -}
startDaemon :: Bool -> Bool -> Maybe HostName -> Maybe (Maybe Handle -> Maybe Handle -> String -> FilePath -> IO ()) -> Annex ()
startDaemon assistant foreground listenhost startbrowser = do
pidfile <- fromRepo gitAnnexPidFile
logfile <- fromRepo gitAnnexLogFile
logfd <- liftIO $ openLog logfile
if foreground
then do
liftIO $ debugM desc $ "logging to " ++ logfile
liftIO $ Utility.Daemon.lockPidFile pidfile
origout <- liftIO $ catchMaybeIO $
fdToHandle =<< dup stdOutput
origerr <- liftIO $ catchMaybeIO $
fdToHandle =<< dup stdError
liftIO $ Utility.LogFile.redirLog logfd
2013-03-12 10:45:56 +00:00
showStart "." desc
start id $
case startbrowser of
Nothing -> Nothing
Just a -> Just $ a origout origerr
else
start (Utility.Daemon.daemonize logfd (Just pidfile) False) Nothing
2012-10-29 02:09:09 +00:00
where
2013-03-12 10:45:56 +00:00
desc
| assistant = "assistant"
| otherwise = "watch"
start daemonize webappwaiter = withThreadState $ \st -> do
checkCanWatch
dstatus <- startDaemonStatus
logfile <- fromRepo gitAnnexLogFile
liftIO $ debugM desc $ "logging to " ++ logfile
liftIO $ daemonize $
flip runAssistant (go webappwaiter)
=<< newAssistantData st dstatus
2012-09-23 15:48:09 +00:00
#ifdef WITH_WEBAPP
go webappwaiter = do
2013-01-26 03:14:32 +00:00
d <- getAssistant id
#else
go _webappwaiter = do
2012-09-23 15:48:09 +00:00
#endif
notice ["starting", desc, "version", SysConfig.packageversion]
urlrenderer <- liftIO newUrlRenderer
mapM_ (startthread urlrenderer)
2012-10-29 15:40:22 +00:00
[ watch $ commitThread
#ifdef WITH_WEBAPP
, assist $ webAppThread d urlrenderer False listenhost Nothing webappwaiter
2012-09-08 19:07:44 +00:00
#ifdef WITH_PAIRING
, assist $ pairListenerThread urlrenderer
2012-09-08 19:07:44 +00:00
#endif
#ifdef WITH_XMPP
, assist $ xmppClientThread urlrenderer
#endif
#endif
2012-10-29 15:40:22 +00:00
, assist $ pushThread
, assist $ pushRetryThread
, assist $ mergeThread
, assist $ transferWatcherThread
, assist $ transferPollerThread
, assist $ transfererThread
, assist $ daemonStatusThread
, assist $ sanityCheckerDailyThread
, assist $ sanityCheckerHourlyThread
#ifdef WITH_CLIBS
, assist $ mountWatcherThread
#endif
, assist $ netWatcherThread
, assist $ netWatcherFallbackThread
, assist $ transferScannerThread urlrenderer
2012-10-29 15:40:22 +00:00
, assist $ configMonitorThread
, assist $ glacierThread
, watch $ watchThread
2012-10-29 02:09:09 +00:00
]
liftIO waitForTermination
2012-10-29 02:09:09 +00:00
watch a = (True, a)
assist a = (False, a)
startthread urlrenderer (watcher, t)
| watcher || assistant = startNamedThread urlrenderer t
2012-10-29 02:09:09 +00:00
| otherwise = noop