aboutsummaryrefslogtreecommitdiff
path: root/src/server/Job.hs
diff options
context:
space:
mode:
authorJoris2015-10-01 14:10:45 +0200
committerJoris2015-10-01 14:10:45 +0200
commitfff7336e06ab4c98adda3fea8a86c7d4d4b9b9bb (patch)
tree702cec84587d18e692e6877557a05f15cbd5fc4f /src/server/Job.hs
parentd7f737db7329acfedb87c5ad02a56023a9670fe4 (diff)
downloadbudget-fff7336e06ab4c98adda3fea8a86c7d4d4b9b9bb.tar.gz
budget-fff7336e06ab4c98adda3fea8a86c7d4d4b9b9bb.tar.bz2
budget-fff7336e06ab4c98adda3fea8a86c7d4d4b9b9bb.zip
Factor job listener
Diffstat (limited to 'src/server/Job.hs')
-rw-r--r--src/server/Job.hs25
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