diff options
author | Joris | 2016-11-13 00:49:32 +0100 |
---|---|---|
committer | Joris | 2016-11-13 00:49:32 +0100 |
commit | 86a96decdb8892b10c5314eb916ef15a64204450 (patch) | |
tree | 6f41742d0466f77948680964188144fbff036902 /src/server/Job/Daemon.hs | |
parent | bf6a0a0b32a7efb88f75c2e89b84d6907aeb10bc (diff) |
Send weekly activity at start of week about previous week
Diffstat (limited to 'src/server/Job/Daemon.hs')
-rw-r--r-- | src/server/Job/Daemon.hs | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/src/server/Job/Daemon.hs b/src/server/Job/Daemon.hs new file mode 100644 index 0000000..8259b18 --- /dev/null +++ b/src/server/Job/Daemon.hs @@ -0,0 +1,40 @@ +module Job.Daemon + ( runDaemons + ) where + +import Data.Time.Clock (UTCTime) + +import Control.Concurrent (threadDelay, forkIO, ThreadId) +import Control.Monad (forever) + +import Model.Database + +import Job.Kind (Kind(..)) +import Job.Frequency (Frequency(..), microSeconds) +import Job.Model (getLastExecution, actualizeLastCheck, actualizeLastExecution) +import Job.MonthlyPayment (monthlyPayment) +import Job.WeeklyReport (weeklyReport) + +import Conf (Conf) + +import Utils.Time (belongToCurrentMonth, belongToCurrentWeek) + +runDaemons :: Conf -> IO () +runDaemons conf = do + _ <- runDaemon MonthlyPayment EveryHour (fmap not . belongToCurrentMonth) monthlyPayment + _ <- runDaemon WeeklyReport EveryHour (fmap not . belongToCurrentWeek) (weeklyReport conf) + return () + +runDaemon :: Kind -> Frequency -> (UTCTime -> IO Bool) -> (Maybe UTCTime -> IO UTCTime) -> IO ThreadId +runDaemon kind frequency isLastExecutionTooOld runJob = + forkIO . forever $ do + mbLastExecution <- runDb $ do + actualizeLastCheck kind + getLastExecution kind + hasToRun <- case mbLastExecution of + Just lastExecution -> isLastExecutionTooOld lastExecution + Nothing -> return True + if hasToRun + then runJob mbLastExecution >>= (runDb . actualizeLastExecution kind) + else return () + threadDelay . microSeconds $ frequency |