e31277d38a
The fun part was making it move things from TransferQueue to currentTransfers entirely atomically. Which will avoid inconsistent display if the WebApp renders the current status at just the wrong time. STM to the rescue!
196 lines
6.8 KiB
Haskell
196 lines
6.8 KiB
Haskell
{- git-annex assistant daemon status
|
|
-
|
|
- Copyright 2012 Joey Hess <joey@kitenet.net>
|
|
-
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
-}
|
|
|
|
module Assistant.DaemonStatus where
|
|
|
|
import Common.Annex
|
|
import Assistant.ThreadedMonad
|
|
import Utility.ThreadScheduler
|
|
import Utility.TempFile
|
|
import Utility.NotificationBroadcaster
|
|
import Logs.Transfer
|
|
import qualified Command.Sync
|
|
|
|
import Control.Concurrent.STM
|
|
import System.Posix.Types
|
|
import Data.Time.Clock.POSIX
|
|
import Data.Time
|
|
import System.Locale
|
|
import qualified Data.Map as M
|
|
|
|
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
|
|
, lastRunning :: Maybe POSIXTime
|
|
-- True when the sanity checker is running
|
|
, sanityCheckRunning :: Bool
|
|
-- Last time the sanity checker ran
|
|
, lastSanityCheck :: Maybe POSIXTime
|
|
-- Currently running file content transfers
|
|
, currentTransfers :: TransferMap
|
|
-- Ordered list of remotes to talk to.
|
|
, knownRemotes :: [Remote]
|
|
-- Clients can use this to wait on changes to the DaemonStatus
|
|
-- and other related things like the TransferQueue.
|
|
, notificationBroadcaster :: NotificationBroadcaster
|
|
}
|
|
|
|
type TransferMap = M.Map Transfer TransferInfo
|
|
|
|
{- This TMVar is never left empty, so accessing it will never block. -}
|
|
type DaemonStatusHandle = TMVar DaemonStatus
|
|
|
|
newDaemonStatus :: IO DaemonStatus
|
|
newDaemonStatus = do
|
|
nb <- newNotificationBroadcaster
|
|
return $ DaemonStatus
|
|
{ scanComplete = False
|
|
, lastRunning = Nothing
|
|
, sanityCheckRunning = False
|
|
, lastSanityCheck = Nothing
|
|
, currentTransfers = M.empty
|
|
, knownRemotes = []
|
|
, notificationBroadcaster = nb
|
|
}
|
|
|
|
getDaemonStatus :: DaemonStatusHandle -> IO DaemonStatus
|
|
getDaemonStatus = atomically . readTMVar
|
|
|
|
modifyDaemonStatus_ :: DaemonStatusHandle -> (DaemonStatus -> DaemonStatus) -> IO ()
|
|
modifyDaemonStatus_ handle a = modifyDaemonStatus handle $ \s -> (a s, ())
|
|
|
|
modifyDaemonStatus :: DaemonStatusHandle -> (DaemonStatus -> (DaemonStatus, b)) -> IO b
|
|
modifyDaemonStatus handle a = do
|
|
(b, nb) <- atomically $ do
|
|
(s, b) <- a <$> takeTMVar handle
|
|
putTMVar handle s
|
|
return $ (b, notificationBroadcaster s)
|
|
sendNotification nb
|
|
return b
|
|
|
|
{- Can be used to send a notification that the daemon status, or other
|
|
- associated thing, like the TransferQueue, has changed. -}
|
|
notifyDaemonStatusChange :: DaemonStatusHandle -> IO ()
|
|
notifyDaemonStatusChange handle = sendNotification
|
|
=<< notificationBroadcaster <$> atomically (readTMVar handle)
|
|
|
|
{- Updates the cached ordered list of remotes from the list in Annex
|
|
- state. -}
|
|
updateKnownRemotes :: DaemonStatusHandle -> Annex ()
|
|
updateKnownRemotes dstatus = do
|
|
remotes <- Command.Sync.syncRemotes []
|
|
liftIO $ modifyDaemonStatus_ dstatus $
|
|
\s -> s { knownRemotes = remotes }
|
|
|
|
{- Load any previous daemon status file, and store it in a MVar for this
|
|
- process to use as its DaemonStatus. Also gets current transfer status. -}
|
|
startDaemonStatus :: Annex DaemonStatusHandle
|
|
startDaemonStatus = do
|
|
file <- fromRepo gitAnnexDaemonStatusFile
|
|
status <- liftIO $
|
|
catchDefaultIO (readDaemonStatusFile file) =<< newDaemonStatus
|
|
transfers <- M.fromList <$> getTransfers
|
|
remotes <- Command.Sync.syncRemotes []
|
|
liftIO $ atomically $ newTMVar status
|
|
{ scanComplete = False
|
|
, sanityCheckRunning = False
|
|
, currentTransfers = transfers
|
|
, knownRemotes = remotes
|
|
}
|
|
|
|
{- This writes the daemon status to disk, when it changes, but no more
|
|
- frequently than once every ten minutes.
|
|
-}
|
|
daemonStatusThread :: ThreadState -> DaemonStatusHandle -> IO ()
|
|
daemonStatusThread st handle = do
|
|
bhandle <- newNotificationHandle
|
|
=<< notificationBroadcaster <$> getDaemonStatus handle
|
|
checkpoint
|
|
runEvery (Seconds tenMinutes) $ do
|
|
waitNotification bhandle
|
|
checkpoint
|
|
where
|
|
checkpoint = do
|
|
status <- getDaemonStatus handle
|
|
file <- runThreadState st $ fromRepo gitAnnexDaemonStatusFile
|
|
writeDaemonStatusFile file status
|
|
|
|
{- 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)
|
|
, "sanityCheckRunning:" ++ show (sanityCheckRunning status)
|
|
, "lastSanityCheck:" ++ maybe "" show (lastSanityCheck status)
|
|
]
|
|
|
|
readDaemonStatusFile :: FilePath -> IO DaemonStatus
|
|
readDaemonStatusFile file = parse <$> newDaemonStatus <*> readFile file
|
|
where
|
|
parse status = foldr parseline status . lines
|
|
parseline line status
|
|
| key == "lastRunning" = parseval readtime $ \v ->
|
|
status { lastRunning = Just v }
|
|
| key == "scanComplete" = parseval readish $ \v ->
|
|
status { scanComplete = v }
|
|
| key == "sanityCheckRunning" = parseval readish $ \v ->
|
|
status { sanityCheckRunning = v }
|
|
| key == "lastSanityCheck" = parseval readtime $ \v ->
|
|
status { lastSanityCheck = Just v }
|
|
| 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
|
|
afterLastDaemonRun timestamp status = maybe False (< t) (lastRunning status)
|
|
where
|
|
t = realToFrac (timestamp + slop) :: POSIXTime
|
|
slop = fromIntegral tenMinutes
|
|
|
|
tenMinutes :: Int
|
|
tenMinutes = 10 * 60
|
|
|
|
{- 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) }
|
|
|
|
{- Variant that does send notifications. -}
|
|
adjustTransfers :: DaemonStatusHandle -> (TransferMap -> TransferMap) -> IO ()
|
|
adjustTransfers dstatus a = modifyDaemonStatus_ dstatus $
|
|
\s -> s { currentTransfers = a (currentTransfers s) }
|
|
|
|
{- Removes a transfer from the map, and returns its info. -}
|
|
removeTransfer :: DaemonStatusHandle -> Transfer -> IO (Maybe TransferInfo)
|
|
removeTransfer dstatus t = modifyDaemonStatus dstatus go
|
|
where
|
|
go s =
|
|
let (info, ts) = M.updateLookupWithKey
|
|
(\_k _v -> Nothing)
|
|
t (currentTransfers s)
|
|
in (s { currentTransfers = ts }, info)
|