From bc74c9ef10d8d5e115679f9b1ac43ea69030dd8e Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Tue, 19 Nov 2019 15:51:20 +0100 Subject: [PATCH] fix(cron): disallow jobs executing twice within scheduling precision --- src/Cron.hs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/Cron.hs b/src/Cron.hs index 5017e71d1..36a063321 100644 --- a/src/Cron.hs +++ b/src/Cron.hs @@ -155,7 +155,7 @@ nextCronMatch :: TZ -- ^ Timezone of the `Cron`-Entry -> UTCTime -- ^ Current time, used only for `CronCalendar` -> Cron -> CronNextMatch UTCTime -nextCronMatch tz mPrev prec now c@Cron{..} = case notAfter of +nextCronMatch tz mPrev prec now c@Cron{..} = onlyOnceWithinPrec $ case notAfter of MatchAsap -> MatchNone MatchAt ts | MatchAt ts' <- nextMatch @@ -165,6 +165,16 @@ nextCronMatch tz mPrev prec now c@Cron{..} = case notAfter of | otherwise -> MatchNone MatchNone -> nextMatch where + onlyOnceWithinPrec sched = case mPrev of + Nothing -> sched + Just prevT -> case sched of + MatchAsap + | now >= addUTCTime prec prevT -> MatchAsap + | otherwise -> MatchAt $ addUTCTime prec prevT + MatchAt ts -> let ts' = max ts $ addUTCTime prec prevT + in if | ts' <= addUTCTime prec now -> MatchAsap + | otherwise -> MatchAt ts' + MatchNone -> MatchNone notAfter | Right c' <- cronNotAfter , Just ref <- notAfterRef