summaryrefslogtreecommitdiff
path: root/Types/ScheduledActivity.hs
blob: 5cdbe29e82b2c353ebfcef401d5a053f144febfe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
{- 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
import Utility.HumanTime
import Types.UUID

import Data.Either

data ScheduledActivity 
	= ScheduledSelfFsck Schedule Duration
	| ScheduledRemoteFsck UUID Schedule Duration
	deriving (Eq, Read, Show, Ord)

{- 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

getSchedule :: ScheduledActivity -> Schedule
getSchedule (ScheduledSelfFsck s _) = s
getSchedule (ScheduledRemoteFsck _ s _) = s

getDuration :: ScheduledActivity -> Duration
getDuration (ScheduledSelfFsck _ d) = d
getDuration (ScheduledRemoteFsck _ _ d) = d

fromScheduledActivity :: ScheduledActivity -> String
fromScheduledActivity (ScheduledSelfFsck s d) = unwords
	[ "fsck self", fromDuration d, fromSchedule s ]
fromScheduledActivity (ScheduledRemoteFsck u s d) = unwords
	[ "fsck", fromUUID u, fromDuration d, fromSchedule s ]

toScheduledActivity :: String -> Maybe ScheduledActivity
toScheduledActivity = eitherToMaybe . parseScheduledActivity

parseScheduledActivity :: String -> Either String ScheduledActivity
parseScheduledActivity s = case words s of
	("fsck":"self":d:rest) -> qualified $ ScheduledSelfFsck
		<$> parseSchedule (unwords rest)
		<*> getduration d
	("fsck":u:d:rest) -> qualified $ ScheduledRemoteFsck
		<$> pure (toUUID u)
		<*> parseSchedule (unwords rest)
		<*> getduration d
	_ -> qualified $ Left "unknown activity"
  where
	qualified (Left e) = Left $ e ++ " in \"" ++ s ++ "\""
	qualified v = v
	getduration d = maybe (Left $ "failed to parse duration \""++d++"\"") Right (parseDuration d)

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