2013-10-07 20:06:34 +00:00
|
|
|
{- git-annex scheduled activities log
|
|
|
|
-
|
2015-01-21 16:50:09 +00:00
|
|
|
- Copyright 2013 Joey Hess <id@joeyh.name>
|
2013-10-07 20:06:34 +00:00
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Logs.Schedule (
|
|
|
|
scheduleLog,
|
|
|
|
scheduleSet,
|
2013-10-11 06:57:36 +00:00
|
|
|
scheduleAdd,
|
|
|
|
scheduleRemove,
|
|
|
|
scheduleChange,
|
2013-10-07 20:06:34 +00:00
|
|
|
scheduleGet,
|
2013-10-07 21:11:13 +00:00
|
|
|
scheduleMap,
|
2013-10-08 15:48:28 +00:00
|
|
|
getLastRunTimes,
|
|
|
|
setLastRunTime,
|
2013-10-07 20:06:34 +00:00
|
|
|
) where
|
|
|
|
|
|
|
|
import qualified Data.Map as M
|
2013-10-08 15:48:28 +00:00
|
|
|
import qualified Data.Set as S
|
2013-10-07 20:06:34 +00:00
|
|
|
import Data.Time.Clock.POSIX
|
2013-10-08 15:48:28 +00:00
|
|
|
import Data.Time.LocalTime
|
2013-10-07 20:06:34 +00:00
|
|
|
|
|
|
|
import Common.Annex
|
|
|
|
import Types.ScheduledActivity
|
|
|
|
import qualified Annex.Branch
|
|
|
|
import Logs
|
|
|
|
import Logs.UUIDBased
|
2013-10-14 20:15:38 +00:00
|
|
|
import Utility.Tmp
|
2013-10-07 20:06:34 +00:00
|
|
|
|
|
|
|
scheduleSet :: UUID -> [ScheduledActivity] -> Annex ()
|
|
|
|
scheduleSet uuid@(UUID _) activities = do
|
|
|
|
ts <- liftIO getPOSIXTime
|
|
|
|
Annex.Branch.change scheduleLog $
|
|
|
|
showLog id . changeLog ts uuid val . parseLog Just
|
|
|
|
where
|
2014-10-09 18:53:13 +00:00
|
|
|
val = fromScheduledActivities activities
|
2013-10-07 20:06:34 +00:00
|
|
|
scheduleSet NoUUID _ = error "unknown UUID; cannot modify"
|
|
|
|
|
|
|
|
scheduleMap :: Annex (M.Map UUID [ScheduledActivity])
|
|
|
|
scheduleMap = simpleMap
|
|
|
|
. parseLogWithUUID parser
|
|
|
|
<$> Annex.Branch.get scheduleLog
|
|
|
|
where
|
2013-10-13 19:40:38 +00:00
|
|
|
parser _uuid = eitherToMaybe . parseScheduledActivities
|
2013-10-07 20:06:34 +00:00
|
|
|
|
2013-10-08 15:48:28 +00:00
|
|
|
scheduleGet :: UUID -> Annex (S.Set ScheduledActivity)
|
2013-10-07 20:06:34 +00:00
|
|
|
scheduleGet u = do
|
|
|
|
m <- scheduleMap
|
2013-10-08 15:48:28 +00:00
|
|
|
return $ maybe S.empty S.fromList (M.lookup u m)
|
|
|
|
|
2013-10-11 06:57:36 +00:00
|
|
|
scheduleRemove :: UUID -> ScheduledActivity -> Annex ()
|
|
|
|
scheduleRemove u activity = scheduleChange u $ S.delete activity
|
|
|
|
|
|
|
|
scheduleAdd :: UUID -> ScheduledActivity -> Annex ()
|
|
|
|
scheduleAdd u activity = scheduleChange u $ S.insert activity
|
|
|
|
|
|
|
|
scheduleChange :: UUID -> (S.Set ScheduledActivity -> S.Set ScheduledActivity) -> Annex ()
|
|
|
|
scheduleChange u a = scheduleSet u . S.toList . a =<< scheduleGet u
|
|
|
|
|
2013-10-08 15:48:28 +00:00
|
|
|
getLastRunTimes :: Annex (M.Map ScheduledActivity LocalTime)
|
|
|
|
getLastRunTimes = do
|
|
|
|
f <- fromRepo gitAnnexScheduleState
|
|
|
|
liftIO $ fromMaybe M.empty
|
|
|
|
<$> catchDefaultIO Nothing (readish <$> readFile f)
|
|
|
|
|
|
|
|
setLastRunTime :: ScheduledActivity -> LocalTime -> Annex ()
|
|
|
|
setLastRunTime activity lastrun = do
|
|
|
|
f <- fromRepo gitAnnexScheduleState
|
2013-10-14 20:15:38 +00:00
|
|
|
liftIO . viaTmp writeFile f . show . M.insert activity lastrun
|
2013-10-08 15:48:28 +00:00
|
|
|
=<< getLastRunTimes
|