2012-06-13 16:36:33 +00:00
|
|
|
{- git-annex assistant daemon status
|
|
|
|
-
|
|
|
|
- Copyright 2012 Joey Hess <joey@kitenet.net>
|
2012-06-23 05:20:40 +00:00
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
2012-06-13 16:36:33 +00:00
|
|
|
-}
|
|
|
|
|
|
|
|
module Assistant.DaemonStatus where
|
|
|
|
|
|
|
|
import Common.Annex
|
2012-06-13 18:02:40 +00:00
|
|
|
import Assistant.ThreadedMonad
|
2012-07-29 13:35:01 +00:00
|
|
|
import Assistant.Alert
|
2012-06-13 21:54:23 +00:00
|
|
|
import Utility.ThreadScheduler
|
|
|
|
import Utility.TempFile
|
2012-07-28 20:01:50 +00:00
|
|
|
import Utility.NotificationBroadcaster
|
2012-07-05 16:21:22 +00:00
|
|
|
import Logs.Transfer
|
2012-08-26 19:39:02 +00:00
|
|
|
import Logs.Trust
|
2012-08-26 18:56:26 +00:00
|
|
|
import qualified Remote
|
2012-06-13 16:36:33 +00:00
|
|
|
|
2012-07-28 22:02:11 +00:00
|
|
|
import Control.Concurrent.STM
|
2012-06-13 16:36:33 +00:00
|
|
|
import System.Posix.Types
|
2012-06-13 17:35:15 +00:00
|
|
|
import Data.Time.Clock.POSIX
|
|
|
|
import Data.Time
|
|
|
|
import System.Locale
|
2012-07-02 20:11:04 +00:00
|
|
|
import qualified Data.Map as M
|
2012-06-13 16:36:33 +00:00
|
|
|
|
|
|
|
data DaemonStatus = DaemonStatus
|
|
|
|
-- False when the daemon is performing its startup scan
|
|
|
|
{ scanComplete :: Bool
|
|
|
|
-- Time when a previous process of the daemon was running ok
|
2012-06-13 17:35:15 +00:00
|
|
|
, lastRunning :: Maybe POSIXTime
|
2012-06-13 21:54:23 +00:00
|
|
|
-- True when the sanity checker is running
|
|
|
|
, sanityCheckRunning :: Bool
|
|
|
|
-- Last time the sanity checker ran
|
|
|
|
, lastSanityCheck :: Maybe POSIXTime
|
2012-07-02 20:11:04 +00:00
|
|
|
-- Currently running file content transfers
|
2012-07-05 20:34:20 +00:00
|
|
|
, currentTransfers :: TransferMap
|
2012-07-29 13:35:01 +00:00
|
|
|
-- Messages to display to the user.
|
|
|
|
, alertMap :: AlertMap
|
2012-08-06 21:09:23 +00:00
|
|
|
, lastAlertId :: AlertId
|
2012-07-05 16:21:22 +00:00
|
|
|
-- Ordered list of remotes to talk to.
|
|
|
|
, knownRemotes :: [Remote]
|
2012-07-29 12:52:57 +00:00
|
|
|
-- Broadcasts notifications about all changes to the DaemonStatus
|
|
|
|
, changeNotifier :: NotificationBroadcaster
|
2012-07-29 13:35:01 +00:00
|
|
|
-- Broadcasts notifications when queued or current transfers change.
|
2012-07-29 12:52:57 +00:00
|
|
|
, transferNotifier :: NotificationBroadcaster
|
2012-07-29 13:35:01 +00:00
|
|
|
-- Broadcasts notifications when there's a change to the alerts
|
|
|
|
, alertNotifier :: NotificationBroadcaster
|
2012-06-13 16:36:33 +00:00
|
|
|
}
|
|
|
|
|
2012-07-05 20:34:20 +00:00
|
|
|
type TransferMap = M.Map Transfer TransferInfo
|
|
|
|
|
2012-07-28 22:02:11 +00:00
|
|
|
{- This TMVar is never left empty, so accessing it will never block. -}
|
|
|
|
type DaemonStatusHandle = TMVar DaemonStatus
|
2012-06-13 16:36:33 +00:00
|
|
|
|
2012-07-28 20:01:50 +00:00
|
|
|
newDaemonStatus :: IO DaemonStatus
|
2012-07-29 13:35:01 +00:00
|
|
|
newDaemonStatus = DaemonStatus
|
|
|
|
<$> pure False
|
|
|
|
<*> pure Nothing
|
|
|
|
<*> pure False
|
|
|
|
<*> pure Nothing
|
|
|
|
<*> pure M.empty
|
|
|
|
<*> pure M.empty
|
2012-07-30 18:08:22 +00:00
|
|
|
<*> pure firstAlertId
|
2012-07-29 13:35:01 +00:00
|
|
|
<*> pure []
|
|
|
|
<*> newNotificationBroadcaster
|
|
|
|
<*> newNotificationBroadcaster
|
|
|
|
<*> newNotificationBroadcaster
|
2012-06-13 16:36:33 +00:00
|
|
|
|
2012-07-28 22:02:11 +00:00
|
|
|
getDaemonStatus :: DaemonStatusHandle -> IO DaemonStatus
|
|
|
|
getDaemonStatus = atomically . readTMVar
|
2012-06-13 16:36:33 +00:00
|
|
|
|
2012-07-28 22:02:11 +00:00
|
|
|
modifyDaemonStatus_ :: DaemonStatusHandle -> (DaemonStatus -> DaemonStatus) -> IO ()
|
2012-07-29 13:35:01 +00:00
|
|
|
modifyDaemonStatus_ dstatus a = modifyDaemonStatus dstatus $ \s -> (a s, ())
|
2012-07-06 22:44:13 +00:00
|
|
|
|
2012-07-28 22:02:11 +00:00
|
|
|
modifyDaemonStatus :: DaemonStatusHandle -> (DaemonStatus -> (DaemonStatus, b)) -> IO b
|
2012-07-29 13:35:01 +00:00
|
|
|
modifyDaemonStatus dstatus a = do
|
2012-07-29 12:52:57 +00:00
|
|
|
(s, b) <- atomically $ do
|
2012-07-29 13:35:01 +00:00
|
|
|
r@(s, _) <- a <$> takeTMVar dstatus
|
|
|
|
putTMVar dstatus s
|
2012-07-29 12:52:57 +00:00
|
|
|
return r
|
|
|
|
sendNotification $ changeNotifier s
|
2012-07-28 20:01:50 +00:00
|
|
|
return b
|
2012-06-13 17:35:15 +00:00
|
|
|
|
2012-08-26 19:39:02 +00:00
|
|
|
{- Remotes ordered by cost, with dead ones thrown out. -}
|
2012-08-26 18:56:26 +00:00
|
|
|
calcKnownRemotes :: Annex [Remote]
|
2012-08-26 19:39:02 +00:00
|
|
|
calcKnownRemotes = do
|
|
|
|
rs <- concat . Remote.byCost <$> Remote.enabledRemoteList
|
|
|
|
alive <- snd <$> trustPartition DeadTrusted (map Remote.uuid rs)
|
|
|
|
let good r = Remote.uuid r `elem` alive
|
|
|
|
return $ filter good rs
|
2012-08-26 18:56:26 +00:00
|
|
|
|
2012-07-22 19:06:18 +00:00
|
|
|
{- Updates the cached ordered list of remotes from the list in Annex
|
|
|
|
- state. -}
|
|
|
|
updateKnownRemotes :: DaemonStatusHandle -> Annex ()
|
|
|
|
updateKnownRemotes dstatus = do
|
2012-08-26 18:56:26 +00:00
|
|
|
remotes <- calcKnownRemotes
|
2012-07-28 22:02:11 +00:00
|
|
|
liftIO $ modifyDaemonStatus_ dstatus $
|
2012-07-22 19:06:18 +00:00
|
|
|
\s -> s { knownRemotes = remotes }
|
|
|
|
|
2012-07-28 22:02:11 +00:00
|
|
|
{- Load any previous daemon status file, and store it in a MVar for this
|
2012-07-02 20:11:04 +00:00
|
|
|
- process to use as its DaemonStatus. Also gets current transfer status. -}
|
2012-06-13 18:02:40 +00:00
|
|
|
startDaemonStatus :: Annex DaemonStatusHandle
|
|
|
|
startDaemonStatus = do
|
|
|
|
file <- fromRepo gitAnnexDaemonStatusFile
|
|
|
|
status <- liftIO $
|
2012-07-28 20:01:50 +00:00
|
|
|
catchDefaultIO (readDaemonStatusFile file) =<< newDaemonStatus
|
2012-07-02 20:11:04 +00:00
|
|
|
transfers <- M.fromList <$> getTransfers
|
2012-08-26 18:56:26 +00:00
|
|
|
remotes <- calcKnownRemotes
|
2012-07-28 22:02:11 +00:00
|
|
|
liftIO $ atomically $ newTMVar status
|
2012-06-13 21:54:23 +00:00
|
|
|
{ scanComplete = False
|
|
|
|
, sanityCheckRunning = False
|
2012-07-02 20:11:04 +00:00
|
|
|
, currentTransfers = transfers
|
2012-07-05 16:21:22 +00:00
|
|
|
, knownRemotes = remotes
|
2012-06-13 21:54:23 +00:00
|
|
|
}
|
2012-06-13 18:02:40 +00:00
|
|
|
|
2012-07-28 20:01:50 +00:00
|
|
|
{- This writes the daemon status to disk, when it changes, but no more
|
|
|
|
- frequently than once every ten minutes.
|
|
|
|
-}
|
2012-06-13 18:02:40 +00:00
|
|
|
daemonStatusThread :: ThreadState -> DaemonStatusHandle -> IO ()
|
2012-07-29 13:35:01 +00:00
|
|
|
daemonStatusThread st dstatus = do
|
2012-07-29 12:52:57 +00:00
|
|
|
notifier <- newNotificationHandle
|
2012-07-29 13:35:01 +00:00
|
|
|
=<< changeNotifier <$> getDaemonStatus dstatus
|
2012-06-13 18:02:40 +00:00
|
|
|
checkpoint
|
2012-07-28 20:01:50 +00:00
|
|
|
runEvery (Seconds tenMinutes) $ do
|
2012-07-29 12:52:57 +00:00
|
|
|
waitNotification notifier
|
2012-07-28 20:01:50 +00:00
|
|
|
checkpoint
|
2012-06-13 18:02:40 +00:00
|
|
|
where
|
2012-07-28 22:02:11 +00:00
|
|
|
checkpoint = do
|
2012-07-29 13:35:01 +00:00
|
|
|
status <- getDaemonStatus dstatus
|
2012-07-28 22:02:11 +00:00
|
|
|
file <- runThreadState st $ fromRepo gitAnnexDaemonStatusFile
|
|
|
|
writeDaemonStatusFile file status
|
2012-06-13 18:02:40 +00:00
|
|
|
|
2012-06-13 17:35:15 +00:00
|
|
|
{- Don't just dump out the structure, because it will change over time,
|
|
|
|
- and parts of it are not relevant. -}
|
|
|
|
writeDaemonStatusFile :: FilePath -> DaemonStatus -> IO ()
|
|
|
|
writeDaemonStatusFile file status =
|
|
|
|
viaTmp writeFile file =<< serialized <$> getPOSIXTime
|
|
|
|
where
|
|
|
|
serialized now = unlines
|
|
|
|
[ "lastRunning:" ++ show now
|
|
|
|
, "scanComplete:" ++ show (scanComplete status)
|
2012-06-13 21:54:23 +00:00
|
|
|
, "sanityCheckRunning:" ++ show (sanityCheckRunning status)
|
2012-06-13 23:25:47 +00:00
|
|
|
, "lastSanityCheck:" ++ maybe "" show (lastSanityCheck status)
|
2012-06-13 17:35:15 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
readDaemonStatusFile :: FilePath -> IO DaemonStatus
|
2012-07-28 20:01:50 +00:00
|
|
|
readDaemonStatusFile file = parse <$> newDaemonStatus <*> readFile file
|
2012-06-13 17:35:15 +00:00
|
|
|
where
|
2012-07-28 20:01:50 +00:00
|
|
|
parse status = foldr parseline status . lines
|
2012-06-13 17:35:15 +00:00
|
|
|
parseline line status
|
|
|
|
| key == "lastRunning" = parseval readtime $ \v ->
|
|
|
|
status { lastRunning = Just v }
|
|
|
|
| key == "scanComplete" = parseval readish $ \v ->
|
|
|
|
status { scanComplete = v }
|
2012-06-13 21:54:23 +00:00
|
|
|
| key == "sanityCheckRunning" = parseval readish $ \v ->
|
|
|
|
status { sanityCheckRunning = v }
|
|
|
|
| key == "lastSanityCheck" = parseval readtime $ \v ->
|
|
|
|
status { lastSanityCheck = Just v }
|
2012-06-13 17:35:15 +00:00
|
|
|
| otherwise = status -- unparsable line
|
|
|
|
where
|
|
|
|
(key, value) = separate (== ':') line
|
|
|
|
parseval parser a = maybe status a (parser value)
|
|
|
|
readtime s = do
|
|
|
|
d <- parseTime defaultTimeLocale "%s%Qs" s
|
|
|
|
Just $ utcTimeToPOSIXSeconds d
|
|
|
|
|
|
|
|
{- Checks if a time stamp was made after the daemon was lastRunning.
|
|
|
|
-
|
|
|
|
- Some slop is built in; this really checks if the time stamp was made
|
|
|
|
- at least ten minutes after the daemon was lastRunning. This is to
|
|
|
|
- ensure the daemon shut down cleanly, and deal with minor clock skew.
|
|
|
|
-
|
|
|
|
- If the daemon has never ran before, this always returns False.
|
|
|
|
-}
|
|
|
|
afterLastDaemonRun :: EpochTime -> DaemonStatus -> Bool
|
2012-06-13 18:02:40 +00:00
|
|
|
afterLastDaemonRun timestamp status = maybe False (< t) (lastRunning status)
|
2012-06-13 17:35:15 +00:00
|
|
|
where
|
|
|
|
t = realToFrac (timestamp + slop) :: POSIXTime
|
2012-06-13 18:19:21 +00:00
|
|
|
slop = fromIntegral tenMinutes
|
|
|
|
|
|
|
|
tenMinutes :: Int
|
|
|
|
tenMinutes = 10 * 60
|
2012-07-05 20:34:20 +00:00
|
|
|
|
2012-07-28 22:47:24 +00:00
|
|
|
{- Mutates the transfer map. Runs in STM so that the transfer map can
|
|
|
|
- be modified in the same transaction that modifies the transfer queue.
|
|
|
|
- Note that this does not send a notification of the change; that's left
|
|
|
|
- to the caller. -}
|
|
|
|
adjustTransfersSTM :: DaemonStatusHandle -> (TransferMap -> TransferMap) -> STM ()
|
|
|
|
adjustTransfersSTM dstatus a = do
|
|
|
|
s <- takeTMVar dstatus
|
|
|
|
putTMVar dstatus $ s { currentTransfers = a (currentTransfers s) }
|
|
|
|
|
2012-08-28 18:19:11 +00:00
|
|
|
{- Alters a transfer's info, if the transfer is in the map. -}
|
2012-08-31 16:14:16 +00:00
|
|
|
alterTransferInfo :: DaemonStatusHandle -> Transfer -> (TransferInfo -> TransferInfo) -> IO ()
|
|
|
|
alterTransferInfo dstatus t a = updateTransferInfo' dstatus $ M.adjust a t
|
2012-08-28 18:19:11 +00:00
|
|
|
|
2012-08-29 18:14:57 +00:00
|
|
|
{- Updates a transfer's info. Adds the transfer to the map if necessary,
|
2012-08-31 17:06:27 +00:00
|
|
|
- or if already present, updates it while preserving the old transferTid,
|
|
|
|
- transferPaused, and bytesComplete values, which are not written to disk. -}
|
2012-08-10 19:45:00 +00:00
|
|
|
updateTransferInfo :: DaemonStatusHandle -> Transfer -> TransferInfo -> IO ()
|
2012-08-28 18:19:11 +00:00
|
|
|
updateTransferInfo dstatus t info = updateTransferInfo' dstatus $
|
2012-08-29 18:14:57 +00:00
|
|
|
M.insertWith' merge t info
|
|
|
|
where
|
|
|
|
merge new old = new
|
|
|
|
{ transferTid = maybe (transferTid new) Just (transferTid old)
|
|
|
|
, transferPaused = transferPaused new || transferPaused old
|
2012-08-31 17:06:27 +00:00
|
|
|
, bytesComplete = maybe (bytesComplete new) Just (bytesComplete old)
|
2012-08-29 18:14:57 +00:00
|
|
|
}
|
2012-08-28 18:19:11 +00:00
|
|
|
|
|
|
|
updateTransferInfo' :: DaemonStatusHandle -> (TransferMap -> TransferMap) -> IO ()
|
|
|
|
updateTransferInfo' dstatus a =
|
2012-07-29 12:52:57 +00:00
|
|
|
notifyTransfer dstatus `after` modifyDaemonStatus_ dstatus go
|
|
|
|
where
|
2012-08-28 18:19:11 +00:00
|
|
|
go s = s { currentTransfers = a (currentTransfers s) }
|
2012-07-06 22:44:13 +00:00
|
|
|
|
|
|
|
{- Removes a transfer from the map, and returns its info. -}
|
2012-07-28 22:02:11 +00:00
|
|
|
removeTransfer :: DaemonStatusHandle -> Transfer -> IO (Maybe TransferInfo)
|
2012-07-29 12:52:57 +00:00
|
|
|
removeTransfer dstatus t =
|
|
|
|
notifyTransfer dstatus `after` modifyDaemonStatus dstatus go
|
2012-07-06 22:44:13 +00:00
|
|
|
where
|
|
|
|
go s =
|
|
|
|
let (info, ts) = M.updateLookupWithKey
|
|
|
|
(\_k _v -> Nothing)
|
|
|
|
t (currentTransfers s)
|
|
|
|
in (s { currentTransfers = ts }, info)
|
2012-07-29 12:52:57 +00:00
|
|
|
|
|
|
|
{- Send a notification when a transfer is changed. -}
|
|
|
|
notifyTransfer :: DaemonStatusHandle -> IO ()
|
2012-07-29 13:35:01 +00:00
|
|
|
notifyTransfer dstatus = sendNotification
|
|
|
|
=<< transferNotifier <$> atomically (readTMVar dstatus)
|
|
|
|
|
|
|
|
{- Send a notification when alerts are changed. -}
|
|
|
|
notifyAlert :: DaemonStatusHandle -> IO ()
|
|
|
|
notifyAlert dstatus = sendNotification
|
|
|
|
=<< alertNotifier <$> atomically (readTMVar dstatus)
|
|
|
|
|
|
|
|
{- Returns the alert's identifier, which can be used to remove it. -}
|
|
|
|
addAlert :: DaemonStatusHandle -> Alert -> IO AlertId
|
|
|
|
addAlert dstatus alert = notifyAlert dstatus `after` modifyDaemonStatus dstatus go
|
|
|
|
where
|
2012-08-06 21:09:23 +00:00
|
|
|
go s = (s { lastAlertId = i, alertMap = m }, i)
|
2012-07-29 13:35:01 +00:00
|
|
|
where
|
2012-08-06 21:09:23 +00:00
|
|
|
i = nextAlertId $ lastAlertId s
|
|
|
|
m = mergeAlert i alert (alertMap s)
|
2012-07-29 13:35:01 +00:00
|
|
|
|
|
|
|
removeAlert :: DaemonStatusHandle -> AlertId -> IO ()
|
2012-07-30 06:07:02 +00:00
|
|
|
removeAlert dstatus i = updateAlert dstatus i (const Nothing)
|
|
|
|
|
|
|
|
updateAlert :: DaemonStatusHandle -> AlertId -> (Alert -> Maybe Alert) -> IO ()
|
|
|
|
updateAlert dstatus i a = updateAlertMap dstatus $ \m -> M.update a i m
|
|
|
|
|
|
|
|
updateAlertMap :: DaemonStatusHandle -> (AlertMap -> AlertMap) -> IO ()
|
|
|
|
updateAlertMap dstatus a = notifyAlert dstatus `after` modifyDaemonStatus_ dstatus go
|
2012-07-29 13:35:01 +00:00
|
|
|
where
|
2012-07-30 06:07:02 +00:00
|
|
|
go s = s { alertMap = a (alertMap s) }
|
2012-07-29 13:35:01 +00:00
|
|
|
|
avoid unnecessary transfer scans when syncing a disconnected remote
Found a very cheap way to determine when a disconnected remote has
diverged, and has new content that needs to be transferred: Piggyback on
the git-annex branch update, which already checks for divergence.
However, this does not check if new content has appeared locally while
disconnected, that should be transferred to the remote.
Also, this does not handle cases where the two git repos are in sync,
but their content syncing has not caught up yet.
This code could have its efficiency improved:
* When multiple remotes are synced, if any one has diverged, they're
all queued for transfer scans.
* The transfer scanner could be told whether the remote has new content,
the local repo has new content, or both, and could optimise its scan
accordingly.
2012-08-22 18:51:11 +00:00
|
|
|
{- Displays an alert while performing an activity that returns True on
|
|
|
|
- success.
|
2012-07-30 06:07:02 +00:00
|
|
|
-
|
|
|
|
- The alert is left visible afterwards, as filler.
|
|
|
|
- Old filler is pruned, to prevent the map growing too large. -}
|
|
|
|
alertWhile :: DaemonStatusHandle -> Alert -> IO Bool -> IO Bool
|
2012-07-30 20:32:32 +00:00
|
|
|
alertWhile dstatus alert a = alertWhile' dstatus alert $ do
|
|
|
|
r <- a
|
2012-08-23 19:48:14 +00:00
|
|
|
return (r, r)
|
2012-07-30 20:32:32 +00:00
|
|
|
|
avoid unnecessary transfer scans when syncing a disconnected remote
Found a very cheap way to determine when a disconnected remote has
diverged, and has new content that needs to be transferred: Piggyback on
the git-annex branch update, which already checks for divergence.
However, this does not check if new content has appeared locally while
disconnected, that should be transferred to the remote.
Also, this does not handle cases where the two git repos are in sync,
but their content syncing has not caught up yet.
This code could have its efficiency improved:
* When multiple remotes are synced, if any one has diverged, they're
all queued for transfer scans.
* The transfer scanner could be told whether the remote has new content,
the local repo has new content, or both, and could optimise its scan
accordingly.
2012-08-22 18:51:11 +00:00
|
|
|
{- Like alertWhile, but allows the activity to return a value too. -}
|
2012-07-30 20:32:32 +00:00
|
|
|
alertWhile' :: DaemonStatusHandle -> Alert -> IO (Bool, a) -> IO a
|
|
|
|
alertWhile' dstatus alert a = do
|
2012-07-29 13:35:01 +00:00
|
|
|
let alert' = alert { alertClass = Activity }
|
2012-07-30 06:07:02 +00:00
|
|
|
i <- addAlert dstatus alert'
|
2012-08-23 19:48:14 +00:00
|
|
|
(ok, r) <- a
|
2012-08-06 21:09:23 +00:00
|
|
|
updateAlertMap dstatus $ mergeAlert i $ makeAlertFiller ok alert'
|
2012-07-30 06:07:02 +00:00
|
|
|
return r
|