2012-06-13 16:36:33 +00:00
|
|
|
{- git-annex assistant daemon
|
|
|
|
-
|
2013-05-22 17:10:54 +00:00
|
|
|
- Copyright 2012-2013 Joey Hess <joey@kitenet.net>
|
2012-06-13 16:36:33 +00:00
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
2012-07-26 01:26:13 +00:00
|
|
|
{-# LANGUAGE CPP #-}
|
|
|
|
|
2012-06-13 16:36:33 +00:00
|
|
|
module Assistant where
|
|
|
|
|
2013-05-25 04:37:41 +00:00
|
|
|
import qualified Annex
|
2012-07-20 23:29:59 +00:00
|
|
|
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
|
2012-09-06 18:56:04 +00:00
|
|
|
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
|
2012-07-03 14:58:40 +00:00
|
|
|
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
|
2013-10-08 15:48:28 +00:00
|
|
|
import Assistant.Threads.Cronner
|
2013-10-29 18:22:56 +00:00
|
|
|
import Assistant.Threads.ProblemFixer
|
2013-03-12 09:48:41 +00:00
|
|
|
#ifdef WITH_CLIBS
|
2012-07-19 17:01:41 +00:00
|
|
|
import Assistant.Threads.MountWatcher
|
2013-03-12 09:48:41 +00:00
|
|
|
#endif
|
2012-08-21 23:58:53 +00:00
|
|
|
import Assistant.Threads.NetWatcher
|
2012-07-23 03:16:56 +00:00
|
|
|
import Assistant.Threads.TransferScanner
|
2012-08-28 18:04:28 +00:00
|
|
|
import Assistant.Threads.TransferPoller
|
add ConfigMonitor thread
Monitors git-annex branch for changes, which are noticed by the Merger
thread whenever the branch ref is changed (either due to an incoming push,
or a local change), and refreshes cached config values for modified config
files.
Rate limited to run no more often than once per minute. This is important
because frequent git-annex branch changes happen when files are being
added, or transferred, etc.
A primary use case is that, when preferred content changes are made,
and get pushed to remotes, the remotes start honoring those settings.
Other use cases include propigating repository description and trust
changes to remotes, and learning when a remote has added a new special
remote, so the webapp can present the GUI to enable that special remote
locally.
Also added a uuid.log cache. All other config files already had caches.
2012-10-20 20:37:06 +00:00
|
|
|
import Assistant.Threads.ConfigMonitor
|
2012-11-29 18:49:20 +00:00
|
|
|
import Assistant.Threads.Glacier
|
2012-07-26 01:26:13 +00:00
|
|
|
#ifdef WITH_WEBAPP
|
2012-09-08 23:57:15 +00:00
|
|
|
import Assistant.WebApp
|
2012-07-26 01:26:13 +00:00
|
|
|
import Assistant.Threads.WebApp
|
2012-09-08 19:07:44 +00:00
|
|
|
#ifdef WITH_PAIRING
|
|
|
|
import Assistant.Threads.PairListener
|
|
|
|
#endif
|
2012-11-03 21:34:19 +00:00
|
|
|
#ifdef WITH_XMPP
|
|
|
|
import Assistant.Threads.XMPPClient
|
2013-05-22 19:13:31 +00:00
|
|
|
import Assistant.Threads.XMPPPusher
|
2012-11-03 21:34:19 +00:00
|
|
|
#endif
|
2012-07-27 16:14:57 +00:00
|
|
|
#else
|
|
|
|
#warning Building without the webapp. You probably need to install Yesod..
|
2013-04-04 05:48:26 +00:00
|
|
|
import Assistant.Types.UrlRenderer
|
2012-07-26 01:26:13 +00:00
|
|
|
#endif
|
2012-06-13 16:36:33 +00:00
|
|
|
import qualified Utility.Daemon
|
|
|
|
import Utility.LogFile
|
2013-01-26 06:09:33 +00:00
|
|
|
import Utility.ThreadScheduler
|
2013-10-26 16:42:58 +00:00
|
|
|
import Utility.HumanTime
|
2013-11-13 16:54:18 +00:00
|
|
|
import Annex.Perms
|
2013-03-17 18:45:24 +00:00
|
|
|
import qualified Build.SysConfig as SysConfig
|
2012-09-06 18:56:04 +00:00
|
|
|
|
2013-03-27 16:37:15 +00:00
|
|
|
import System.Log.Logger
|
2013-04-08 19:04:35 +00:00
|
|
|
import Network.Socket (HostName)
|
2013-03-27 16:37:15 +00:00
|
|
|
|
2012-08-01 20:10:26 +00:00
|
|
|
stopDaemon :: Annex ()
|
|
|
|
stopDaemon = liftIO . Utility.Daemon.stopDaemon =<< fromRepo gitAnnexPidFile
|
|
|
|
|
2013-01-15 17:34:59 +00:00
|
|
|
{- 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. -}
|
2013-10-26 16:42:58 +00:00
|
|
|
startDaemon :: Bool -> Bool -> Maybe Duration -> Maybe HostName -> Maybe (Maybe Handle -> Maybe Handle -> String -> FilePath -> IO ()) -> Annex ()
|
|
|
|
startDaemon assistant foreground startdelay listenhost startbrowser = do
|
2013-05-25 04:37:41 +00:00
|
|
|
Annex.changeState $ \s -> s { Annex.daemon = True }
|
2013-01-15 17:34:59 +00:00
|
|
|
pidfile <- fromRepo gitAnnexPidFile
|
2013-03-27 16:37:15 +00:00
|
|
|
logfile <- fromRepo gitAnnexLogFile
|
2013-11-12 18:54:02 +00:00
|
|
|
#ifndef mingw32_HOST_OS
|
2013-11-13 16:54:18 +00:00
|
|
|
createAnnexDirectory (parentDir logfile)
|
2013-11-13 01:02:45 +00:00
|
|
|
logfd <- liftIO $ openLog logfile
|
2013-01-15 17:34:59 +00:00
|
|
|
if foreground
|
|
|
|
then do
|
|
|
|
origout <- liftIO $ catchMaybeIO $
|
|
|
|
fdToHandle =<< dup stdOutput
|
|
|
|
origerr <- liftIO $ catchMaybeIO $
|
|
|
|
fdToHandle =<< dup stdError
|
2013-05-09 16:02:31 +00:00
|
|
|
let undaemonize a = do
|
|
|
|
debugM desc $ "logging to " ++ logfile
|
|
|
|
Utility.Daemon.lockPidFile pidfile
|
|
|
|
Utility.LogFile.redirLog logfd
|
|
|
|
a
|
|
|
|
start undaemonize $
|
2013-01-15 17:34:59 +00:00
|
|
|
case startbrowser of
|
|
|
|
Nothing -> Nothing
|
|
|
|
Just a -> Just $ a origout origerr
|
|
|
|
else
|
|
|
|
start (Utility.Daemon.daemonize logfd (Just pidfile) False) Nothing
|
2013-11-12 18:54:02 +00:00
|
|
|
#else
|
|
|
|
-- Windows is always foreground, and has no log file.
|
|
|
|
start id $
|
|
|
|
case startbrowser of
|
|
|
|
Nothing -> Nothing
|
|
|
|
Just a -> Just $ a Nothing Nothing
|
|
|
|
#endif
|
2012-10-29 02:09:09 +00:00
|
|
|
where
|
2013-03-12 10:45:56 +00:00
|
|
|
desc
|
|
|
|
| assistant = "assistant"
|
|
|
|
| otherwise = "watch"
|
2013-01-15 17:34:59 +00:00
|
|
|
start daemonize webappwaiter = withThreadState $ \st -> do
|
|
|
|
checkCanWatch
|
|
|
|
dstatus <- startDaemonStatus
|
2013-03-27 16:37:15 +00:00
|
|
|
logfile <- fromRepo gitAnnexLogFile
|
|
|
|
liftIO $ debugM desc $ "logging to " ++ logfile
|
2013-01-15 17:34:59 +00:00
|
|
|
liftIO $ daemonize $
|
|
|
|
flip runAssistant (go webappwaiter)
|
|
|
|
=<< newAssistantData st dstatus
|
2012-08-01 20:10:26 +00:00
|
|
|
|
2012-09-23 15:48:09 +00:00
|
|
|
#ifdef WITH_WEBAPP
|
2013-04-03 21:44:34 +00:00
|
|
|
go webappwaiter = do
|
2013-01-26 03:14:32 +00:00
|
|
|
d <- getAssistant id
|
2013-02-06 19:38:41 +00:00
|
|
|
#else
|
2013-04-03 21:44:34 +00:00
|
|
|
go _webappwaiter = do
|
2012-09-23 15:48:09 +00:00
|
|
|
#endif
|
2013-04-03 21:44:34 +00:00
|
|
|
notice ["starting", desc, "version", SysConfig.packageversion]
|
|
|
|
urlrenderer <- liftIO newUrlRenderer
|
|
|
|
mapM_ (startthread urlrenderer)
|
2012-10-29 15:40:22 +00:00
|
|
|
[ watch $ commitThread
|
2012-08-02 13:09:06 +00:00
|
|
|
#ifdef WITH_WEBAPP
|
2013-04-08 19:04:35 +00:00
|
|
|
, assist $ webAppThread d urlrenderer False listenhost Nothing webappwaiter
|
2012-09-08 19:07:44 +00:00
|
|
|
#ifdef WITH_PAIRING
|
2012-10-29 18:07:12 +00:00
|
|
|
, assist $ pairListenerThread urlrenderer
|
2012-09-08 19:07:44 +00:00
|
|
|
#endif
|
2012-11-03 21:34:19 +00:00
|
|
|
#ifdef WITH_XMPP
|
|
|
|
, assist $ xmppClientThread urlrenderer
|
2013-05-22 19:13:31 +00:00
|
|
|
, assist $ xmppSendPackThread urlrenderer
|
|
|
|
, assist $ xmppReceivePackThread urlrenderer
|
2012-11-03 21:34:19 +00:00
|
|
|
#endif
|
2012-08-02 13:09:06 +00:00
|
|
|
#endif
|
2012-10-29 15:40:22 +00:00
|
|
|
, assist $ pushThread
|
|
|
|
, assist $ pushRetryThread
|
|
|
|
, assist $ mergeThread
|
2012-10-29 17:09:58 +00:00
|
|
|
, assist $ transferWatcherThread
|
2012-10-29 06:21:04 +00:00
|
|
|
, assist $ transferPollerThread
|
2012-10-29 18:07:12 +00:00
|
|
|
, assist $ transfererThread
|
2012-10-29 06:21:04 +00:00
|
|
|
, assist $ daemonStatusThread
|
2013-03-01 17:30:48 +00:00
|
|
|
, assist $ sanityCheckerDailyThread
|
|
|
|
, assist $ sanityCheckerHourlyThread
|
2013-10-29 18:22:56 +00:00
|
|
|
, assist $ problemFixerThread urlrenderer
|
2013-03-12 09:48:41 +00:00
|
|
|
#ifdef WITH_CLIBS
|
2013-10-29 20:48:06 +00:00
|
|
|
, assist $ mountWatcherThread urlrenderer
|
2013-03-12 09:48:41 +00:00
|
|
|
#endif
|
2012-10-29 06:21:04 +00:00
|
|
|
, assist $ netWatcherThread
|
|
|
|
, assist $ netWatcherFallbackThread
|
2013-04-03 21:01:40 +00:00
|
|
|
, assist $ transferScannerThread urlrenderer
|
2013-10-10 22:02:33 +00:00
|
|
|
, assist $ cronnerThread urlrenderer
|
2012-10-29 15:40:22 +00:00
|
|
|
, assist $ configMonitorThread
|
2012-11-29 18:49:20 +00:00
|
|
|
, assist $ glacierThread
|
2012-10-29 13:55:40 +00:00
|
|
|
, watch $ watchThread
|
assistant: Detect stale git lock files at startup time, and remove them.
Extends the index.lock handling to other git lock files. I surveyed
all lock files used by git, and found more than I expected. All are
handled the same in git; it leaves them open while doing the operation,
possibly writing the new file content to the lock file, and then closes
them when done.
The gc.pid file is excluded because it won't affect the normal operation
of the assistant, and waiting for a gc to finish on startup wouldn't be
good.
All threads except the webapp thread wait on the new startup sanity checker
thread to complete, so they won't try to do things with git that fail
due to stale lock files. The webapp thread mostly avoids doing that kind of
thing itself. A few configurators might fail on lock files, but only if the
user is explicitly trying to run them. The webapp needs to start
immediately when the user has opened it, even if there are stale lock
files.
Arranging for the threads to wait on the startup sanity checker was a bit
of a bear. Have to get all the NotificationHandles set up before the
startup sanity checker runs, or they won't see its signal. Perhaps
the NotificationBroadcaster is not the best interface to have used for
this. Oh well, it works.
This commit was sponsored by Michael Jakl
2013-10-05 21:02:11 +00:00
|
|
|
-- must come last so that all threads that wait
|
|
|
|
-- on it have already started waiting
|
2013-10-26 16:42:58 +00:00
|
|
|
, watch $ sanityCheckerStartupThread startdelay
|
2012-10-29 02:09:09 +00:00
|
|
|
]
|
2013-01-15 17:34:59 +00:00
|
|
|
|
2013-01-26 06:09:33 +00:00
|
|
|
liftIO waitForTermination
|
2012-10-25 19:30:49 +00:00
|
|
|
|
2012-10-29 02:09:09 +00:00
|
|
|
watch a = (True, a)
|
|
|
|
assist a = (False, a)
|
2013-01-26 06:09:33 +00:00
|
|
|
startthread urlrenderer (watcher, t)
|
2013-02-06 19:38:41 +00:00
|
|
|
| watcher || assistant = startNamedThread urlrenderer t
|
2012-10-29 02:09:09 +00:00
|
|
|
| otherwise = noop
|