diff options
author | Joris | 2015-10-01 14:10:45 +0200 |
---|---|---|
committer | Joris | 2015-10-01 14:10:45 +0200 |
commit | fff7336e06ab4c98adda3fea8a86c7d4d4b9b9bb (patch) | |
tree | 702cec84587d18e692e6877557a05f15cbd5fc4f /src/server/Job.hs | |
parent | d7f737db7329acfedb87c5ad02a56023a9670fe4 (diff) |
Factor job listener
Diffstat (limited to 'src/server/Job.hs')
-rw-r--r-- | src/server/Job.hs | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/src/server/Job.hs b/src/server/Job.hs new file mode 100644 index 0000000..bf8f15b --- /dev/null +++ b/src/server/Job.hs @@ -0,0 +1,25 @@ +module Job + ( jobListener + ) where + +import Data.Time.Clock + +import Control.Concurrent (threadDelay) + +import Model.Database +import Model.JobKind +import Model.Job + +jobListener :: JobKind -> (UTCTime -> IO Bool) -> (() -> Persist ()) -> Int -> IO () +jobListener kind lastExecutionTooOld runJob msDelay = do + mbLastExecution <- runDb $ do + actualizeLastCheck kind + getLastExecution kind + hasToRun <- case mbLastExecution of + Just lastExecution -> lastExecutionTooOld lastExecution + Nothing -> return True + if hasToRun + then runDb (runJob () >> actualizeLastExecution kind) + else return () + threadDelay msDelay + jobListener kind lastExecutionTooOld runJob msDelay |