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

getLastExecution :: JobKind -> Persist (Maybe UTCTime)
getLastExecution kind = do
  mbJob <- fmap entityVal <$> selectFirst [JobKind ==. kind] []
  return (mbJob >>= jobLastExecution)

actualizeLastExecution :: JobKind -> Persist ()
actualizeLastExecution kind = do
  now <- liftIO getCurrentTime
  jobKindDefined <- isJust <$> selectFirst [JobKind ==. kind] []
  if jobKindDefined
    then updateWhere [JobKind ==. kind] [JobLastExecution =. Just now]
    else insert (Job kind (Just now) (Just now)) >> return ()

actualizeLastCheck :: JobKind -> Persist ()
actualizeLastCheck kind = do
  now <- liftIO getCurrentTime
  updateWhere [JobKind ==. kind] [JobLastCheck =. Just now]