diff options
Diffstat (limited to 'src/server/Job/Model.hs')
-rw-r--r-- | src/server/Job/Model.hs | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/src/server/Job/Model.hs b/src/server/Job/Model.hs new file mode 100644 index 0000000..cd7297a --- /dev/null +++ b/src/server/Job/Model.hs @@ -0,0 +1,33 @@ +module Job.Model + ( getLastExecution + , actualizeLastExecution + , actualizeLastCheck + ) where + +import Control.Monad.IO.Class (liftIO) + +import Data.Time.Clock (UTCTime, getCurrentTime) +import Data.Maybe (isJust) + +import Database.Persist + +import Model.Database + +import Job.Kind + +getLastExecution :: Kind -> Persist (Maybe UTCTime) +getLastExecution kind = do + mbJob <- fmap entityVal <$> selectFirst [JobKind ==. kind] [] + return (mbJob >>= jobLastExecution) + +actualizeLastExecution :: Kind -> UTCTime -> Persist () +actualizeLastExecution kind time = do + jobKindDefined <- isJust <$> selectFirst [JobKind ==. kind] [] + if jobKindDefined + then updateWhere [JobKind ==. kind] [JobLastExecution =. Just time] + else insert (Job kind (Just time) (Just time)) >> return () + +actualizeLastCheck :: Kind -> Persist () +actualizeLastCheck kind = do + now <- liftIO getCurrentTime + updateWhere [JobKind ==. kind] [JobLastCheck =. Just now] |