diff options
author | Joey Hess <joey@kitenet.net> | 2012-06-13 14:02:40 -0400 |
---|---|---|
committer | Joey Hess <joey@kitenet.net> | 2012-06-13 14:02:40 -0400 |
commit | 59a7b3a51a7cdfb8528ebc44a26a7577f28254d4 (patch) | |
tree | 3d6ce52a791122ebd0bd5c94c269140fe7df6b63 /Assistant | |
parent | ff2414427b21324722ed74b754d72307084fc6a5 (diff) |
finish daemon status thread
Diffstat (limited to 'Assistant')
-rw-r--r-- | Assistant/DaemonStatus.hs | 29 |
1 files changed, 25 insertions, 4 deletions
diff --git a/Assistant/DaemonStatus.hs b/Assistant/DaemonStatus.hs index 3615d0e5c..eb8ff256b 100644 --- a/Assistant/DaemonStatus.hs +++ b/Assistant/DaemonStatus.hs @@ -7,6 +7,7 @@ module Assistant.DaemonStatus where import Common.Annex import Utility.TempFile +import Assistant.ThreadedMonad import Control.Concurrent import System.Posix.Types @@ -30,15 +31,35 @@ newDaemonStatus = DaemonStatus , lastRunning = Nothing } -startDaemonStatus :: IO DaemonStatusHandle -startDaemonStatus = newMVar newDaemonStatus - getDaemonStatus :: DaemonStatusHandle -> Annex DaemonStatus getDaemonStatus = liftIO . readMVar modifyDaemonStatus :: DaemonStatusHandle -> (DaemonStatus -> DaemonStatus) -> Annex () modifyDaemonStatus handle a = liftIO $ modifyMVar_ handle (return . a) +{- Load any previous daemon status file, and store it in the MVar for this + - process to use as its DaemonStatus. -} +startDaemonStatus :: Annex DaemonStatusHandle +startDaemonStatus = do + file <- fromRepo gitAnnexDaemonStatusFile + status <- liftIO $ + catchDefaultIO (readDaemonStatusFile file) newDaemonStatus + liftIO $ newMVar status { scanComplete = False } + +{- This thread wakes up periodically and writes the daemon status to disk. -} +daemonStatusThread :: ThreadState -> DaemonStatusHandle -> IO () +daemonStatusThread st handle = do + checkpoint + forever $ do + threadDelay tenMinutes + checkpoint + where + checkpoint = runThreadState st $ do + file <- fromRepo gitAnnexDaemonStatusFile + status <- getDaemonStatus handle + liftIO $ writeDaemonStatusFile file status + tenMinutes = 10 * 60 * 1000000 -- microseconds + {- Don't just dump out the structure, because it will change over time, - and parts of it are not relevant. -} writeDaemonStatusFile :: FilePath -> DaemonStatus -> IO () @@ -76,7 +97,7 @@ readDaemonStatusFile file = parse <$> readFile file - If the daemon has never ran before, this always returns False. -} afterLastDaemonRun :: EpochTime -> DaemonStatus -> Bool -afterLastDaemonRun timestamp status = maybe True (< t) (lastRunning status) +afterLastDaemonRun timestamp status = maybe False (< t) (lastRunning status) where t = realToFrac (timestamp + slop) :: POSIXTime slop = 10 * 60 |