{-# LANGUAGE ScopedTypeVariables, PatternGuards #-}
{-# OPTIONS -fno-warn-orphans #-}
module BuildBox.Control.Cron
( module BuildBox.Data.Schedule
, cronLoop )
where
import BuildBox.Build
import BuildBox.Data.Schedule
import BuildBox.Command.Sleep
import Data.Time
cronLoop :: Schedule (Build ())-> Build ()
cronLoop schedule
= do startTime <- io $ getCurrentTime
case earliestEventToStartAt startTime $ eventsOfSchedule schedule of
Nothing
-> do sleep 1
cronLoop schedule
Just event
-> do let Just build = lookupCommandOfSchedule (eventName event) schedule
build
endTime <- io $ getCurrentTime
let event' = event
{ eventLastStarted = Just startTime
, eventLastEnded = Just endTime }
let schedule' = adjustEventOfSchedule event' schedule
cronLoop schedule'