aboutsummaryrefslogtreecommitdiff
path: root/src/server/Job/Model.hs
blob: cd7297a3afeff39b92a87234473eec5a26d9a588 (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
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]