From b2a4e26946f676fd0c6214f226f3bfa4b04e2cb6 Mon Sep 17 00:00:00 2001 From: Sam Lewis Date: Mon, 31 Oct 2022 02:29:48 -0400 Subject: [PATCH] add Exceptions to schedules --- schedule.go | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/schedule.go b/schedule.go index 825bdad..97ada92 100644 --- a/schedule.go +++ b/schedule.go @@ -21,6 +21,9 @@ type Schedule struct { isSunrise bool isSunset bool sunOffset DurationString + + exceptionDays []time.Time + exceptionRanges []timeRange } func (s Schedule) Hash() string { @@ -125,10 +128,30 @@ func (sb scheduleBuilderCustom) Offset(s DurationString) scheduleBuilderEnd { return scheduleBuilderEnd(sb) } +func (sb scheduleBuilderCustom) ExceptionDay(t time.Time) scheduleBuilderCustom { + sb.schedule.exceptionDays = append(sb.schedule.exceptionDays, t) + return sb +} + +func (sb scheduleBuilderCustom) ExceptionRange(start, end time.Time) scheduleBuilderCustom { + sb.schedule.exceptionRanges = append(sb.schedule.exceptionRanges, timeRange{start, end}) + return sb +} + func (sb scheduleBuilderCustom) Build() Schedule { return sb.schedule } +func (sb scheduleBuilderEnd) ExceptionDay(t time.Time) scheduleBuilderEnd { + sb.schedule.exceptionDays = append(sb.schedule.exceptionDays, t) + return sb +} + +func (sb scheduleBuilderEnd) ExceptionRange(start, end time.Time) scheduleBuilderEnd { + sb.schedule.exceptionRanges = append(sb.schedule.exceptionRanges, timeRange{start, end}) + return sb +} + func (sb scheduleBuilderEnd) Build() Schedule { return sb.schedule } @@ -149,18 +172,28 @@ func runSchedules(a *app) { // run callback for all schedules before now in case they overlap for sched.realStartTime.Before(time.Now()) { - go sched.callback(a.service, a.state) + maybeRunCallback(a, sched) requeueSchedule(a, sched) sched = popSchedule(a) } time.Sleep(time.Until(sched.realStartTime)) - go sched.callback(a.service, a.state) + maybeRunCallback(a, sched) requeueSchedule(a, sched) } } +func maybeRunCallback(a *app, s Schedule) { + if c := checkExceptionDays(s.exceptionDays); c.fail { + return + } + if c := checkExceptionRanges(s.exceptionRanges); c.fail { + return + } + go s.callback(a.service, a.state) +} + func popSchedule(a *app) Schedule { _sched, _ := a.schedules.Pop() return _sched.(Schedule)