2013-10-07 20:06:34 +00:00
|
|
|
{- git-annex scheduled activities
|
|
|
|
-
|
|
|
|
- Copyright 2013 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Types.ScheduledActivity where
|
|
|
|
|
|
|
|
import Common
|
|
|
|
import Utility.Scheduled
|
2013-10-08 21:44:20 +00:00
|
|
|
import Utility.HumanTime
|
2013-10-07 20:06:34 +00:00
|
|
|
import Types.UUID
|
|
|
|
|
2013-10-13 19:40:38 +00:00
|
|
|
import Data.Either
|
|
|
|
|
2013-10-07 20:06:34 +00:00
|
|
|
data ScheduledActivity
|
2013-10-08 21:44:20 +00:00
|
|
|
= ScheduledSelfFsck Schedule Duration
|
|
|
|
| ScheduledRemoteFsck UUID Schedule Duration
|
2014-10-09 19:09:26 +00:00
|
|
|
deriving (Eq, Read, Show, Ord)
|
2013-10-07 20:06:34 +00:00
|
|
|
|
2013-10-13 21:14:56 +00:00
|
|
|
{- Activities that run on a remote, within a time window, so
|
|
|
|
- should be run when the remote gets connected. -}
|
|
|
|
connectActivityUUID :: ScheduledActivity -> Maybe UUID
|
|
|
|
connectActivityUUID (ScheduledRemoteFsck u (Schedule _ AnyTime) _) = Just u
|
|
|
|
connectActivityUUID _ = Nothing
|
|
|
|
|
2013-10-08 21:44:20 +00:00
|
|
|
getSchedule :: ScheduledActivity -> Schedule
|
|
|
|
getSchedule (ScheduledSelfFsck s _) = s
|
|
|
|
getSchedule (ScheduledRemoteFsck _ s _) = s
|
|
|
|
|
|
|
|
getDuration :: ScheduledActivity -> Duration
|
|
|
|
getDuration (ScheduledSelfFsck _ d) = d
|
|
|
|
getDuration (ScheduledRemoteFsck _ _ d) = d
|
|
|
|
|
2013-10-07 20:06:34 +00:00
|
|
|
fromScheduledActivity :: ScheduledActivity -> String
|
2013-10-08 21:44:20 +00:00
|
|
|
fromScheduledActivity (ScheduledSelfFsck s d) = unwords
|
|
|
|
[ "fsck self", fromDuration d, fromSchedule s ]
|
|
|
|
fromScheduledActivity (ScheduledRemoteFsck u s d) = unwords
|
|
|
|
[ "fsck", fromUUID u, fromDuration d, fromSchedule s ]
|
2013-10-07 20:06:34 +00:00
|
|
|
|
|
|
|
toScheduledActivity :: String -> Maybe ScheduledActivity
|
2013-10-07 21:11:13 +00:00
|
|
|
toScheduledActivity = eitherToMaybe . parseScheduledActivity
|
|
|
|
|
|
|
|
parseScheduledActivity :: String -> Either String ScheduledActivity
|
|
|
|
parseScheduledActivity s = case words s of
|
2013-10-08 21:44:20 +00:00
|
|
|
("fsck":"self":d:rest) -> qualified $ ScheduledSelfFsck
|
2013-10-07 21:11:13 +00:00
|
|
|
<$> parseSchedule (unwords rest)
|
2013-10-08 21:44:20 +00:00
|
|
|
<*> getduration d
|
|
|
|
("fsck":u:d:rest) -> qualified $ ScheduledRemoteFsck
|
2013-10-07 20:06:34 +00:00
|
|
|
<$> pure (toUUID u)
|
2013-10-07 21:11:13 +00:00
|
|
|
<*> parseSchedule (unwords rest)
|
2013-10-08 21:44:20 +00:00
|
|
|
<*> getduration d
|
2013-10-07 21:11:13 +00:00
|
|
|
_ -> qualified $ Left "unknown activity"
|
|
|
|
where
|
|
|
|
qualified (Left e) = Left $ e ++ " in \"" ++ s ++ "\""
|
|
|
|
qualified v = v
|
2013-10-08 21:44:20 +00:00
|
|
|
getduration d = maybe (Left $ "failed to parse duration \""++d++"\"") Right (parseDuration d)
|
2013-10-13 19:40:38 +00:00
|
|
|
|
|
|
|
fromScheduledActivities :: [ScheduledActivity] -> String
|
|
|
|
fromScheduledActivities = intercalate "; " . map fromScheduledActivity
|
|
|
|
|
|
|
|
parseScheduledActivities :: String -> Either String [ScheduledActivity]
|
|
|
|
parseScheduledActivities s
|
|
|
|
| null bad = Right good
|
|
|
|
| otherwise = Left $ intercalate "; " bad
|
|
|
|
where
|
|
|
|
(bad, good) = partitionEithers $
|
|
|
|
map parseScheduledActivity $ split "; " s
|