mirror of
https://github.com/warmbly/warmbly.git
synced 2026-11-17 08:18:51 +00:00
feat: walk campaign sending windows by calendar date in nextScheduleSlot so 23- and 25-hour DST days are stepped correctly, fail closed when no window is found, resolve window starts in skipped or repeated hours, step the other scheduler 'tomorrow' helpers by calendar day, and add time zone sweep tests
This commit is contained in:
@@ -998,7 +998,7 @@ func (s *schedulerService) deferToNextDay(campaign *models.Campaign, preview boo
|
||||
midnight := time.Date(local.Year(), local.Month(), local.Day(), 0, 0, 0, 0, tz).AddDate(0, 0, 1)
|
||||
return nextScheduleSlot(midnight, windows, tz)
|
||||
}
|
||||
t := nextScheduleSlot(time.Now().Add(24*time.Hour), windows, tz)
|
||||
t := nextScheduleSlot(time.Now().In(tz).AddDate(0, 0, 1), windows, tz)
|
||||
// Add a small jitter so deferred tasks don't all wake at the same instant.
|
||||
return t.Add(time.Minute * time.Duration(randomJitter(0, 30)))
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/warmbly/warmbly/internal/app/behavior"
|
||||
"github.com/warmbly/warmbly/internal/bitmask"
|
||||
"github.com/warmbly/warmbly/internal/models"
|
||||
@@ -29,7 +30,7 @@ func findNextValidDay(from time.Time, daysBitmask uint8, tz *time.Location) time
|
||||
if bitmask.HasWeekday(daysBitmask, candidate.Weekday()) {
|
||||
return candidate
|
||||
}
|
||||
candidate = candidate.Add(24 * time.Hour)
|
||||
candidate = candidate.AddDate(0, 0, 1)
|
||||
}
|
||||
|
||||
// If no valid day found in 7 days, just return the input
|
||||
@@ -57,8 +58,7 @@ func ensureTimeWindow(t time.Time, startTime, endTime string, tz *time.Location)
|
||||
|
||||
if minutesOfDay > end {
|
||||
// Too late, move to tomorrow's start time
|
||||
next := tLocal.Add(24 * time.Hour)
|
||||
return time.Date(next.Year(), next.Month(), next.Day(),
|
||||
return time.Date(tLocal.Year(), tLocal.Month(), tLocal.Day()+1,
|
||||
start/60, start%60, 0, 0, tz)
|
||||
}
|
||||
|
||||
@@ -98,33 +98,67 @@ func effectiveWindows(c *models.Campaign) models.ScheduleWindows {
|
||||
// empty schedule means "unconstrained" and returns from unchanged. When from is
|
||||
// already inside a window its exact instant is preserved (so jitter/min-wait
|
||||
// adjustments survive); otherwise it advances to the next interval's start.
|
||||
//
|
||||
// Days are walked by calendar date, so a 23- or 25-hour day is stepped like any
|
||||
// other, and a search that finds no window never answers "now".
|
||||
func nextScheduleSlot(from time.Time, sw models.ScheduleWindows, tz *time.Location) time.Time {
|
||||
if sw.IsEmpty() {
|
||||
return from
|
||||
}
|
||||
cur := from.In(tz)
|
||||
local := from.In(tz)
|
||||
y, m, d := local.Date()
|
||||
nowMin := local.Hour()*60 + local.Minute()
|
||||
for i := 0; i < 8; i++ {
|
||||
y, m, d := cur.Date()
|
||||
nowMin := cur.Hour()*60 + cur.Minute()
|
||||
// Noon exists on every date, so it names the day even where midnight does not.
|
||||
day := time.Date(y, m, d+i, 12, 0, 0, 0, tz)
|
||||
dy, dm, dd := day.Date()
|
||||
|
||||
ivs := append([]models.TimeInterval(nil), sw[int(cur.Weekday())]...)
|
||||
ivs := append([]models.TimeInterval(nil), sw[int(day.Weekday())]...)
|
||||
sort.Slice(ivs, func(a, b int) bool { return ivs[a].Start < ivs[b].Start })
|
||||
|
||||
for _, iv := range ivs {
|
||||
if nowMin < iv.Start {
|
||||
return time.Date(y, m, d, iv.Start/60, iv.Start%60, 0, 0, tz)
|
||||
if i == 0 && nowMin >= iv.End {
|
||||
continue
|
||||
}
|
||||
if nowMin < iv.End {
|
||||
if i == 0 {
|
||||
return from // already inside a window — keep the exact instant
|
||||
}
|
||||
return cur
|
||||
if i == 0 && nowMin >= iv.Start {
|
||||
return from // already inside a window — keep the exact instant
|
||||
}
|
||||
at, ok := wallClockAt(dy, dm, dd, iv.Start, tz)
|
||||
if ok && at.Before(from) {
|
||||
// from is in the repeated hour after the clocks went back: take the second pass.
|
||||
_, before := at.Zone()
|
||||
_, end := at.ZoneBounds()
|
||||
_, after := end.Zone()
|
||||
at = at.Add(time.Duration(before-after) * time.Second)
|
||||
}
|
||||
if !ok || at.Before(from) || at.In(tz).Hour()*60+at.In(tz).Minute() >= iv.End {
|
||||
continue // the whole interval falls in time the clocks skipped
|
||||
}
|
||||
return at
|
||||
}
|
||||
// No interval left today — jump to the start of the next day.
|
||||
cur = time.Date(y, m, d, 0, 0, 0, 0, tz).Add(24 * time.Hour)
|
||||
}
|
||||
return from
|
||||
log.Warn().Str("tz", tz.String()).Time("from", from).Msg("scheduler: no sending window within 8 days of a non-empty schedule")
|
||||
return from.AddDate(0, 0, 8)
|
||||
}
|
||||
|
||||
// wallClockAt is the first instant on the local date y-m-d whose wall clock
|
||||
// reads minute or later; when the clocks skip over minute, that is the instant
|
||||
// they resume. ok is false when no such instant is left on that date.
|
||||
func wallClockAt(y int, m time.Month, d, minute int, tz *time.Location) (time.Time, bool) {
|
||||
t := time.Date(y, m, d, minute/60, minute%60, 0, 0, tz)
|
||||
ly, lm, ld := t.In(tz).Date()
|
||||
got := time.Date(ly, lm, ld, t.In(tz).Hour(), t.In(tz).Minute(), 0, 0, time.UTC)
|
||||
want := time.Date(y, m, d, minute/60, minute%60, 0, 0, time.UTC)
|
||||
switch start, end := t.ZoneBounds(); {
|
||||
case got.After(want): // resolved past the gap: the gap ends where this zone begins
|
||||
t = start
|
||||
case got.Before(want): // resolved before the gap: it ends where this zone ends
|
||||
t = end
|
||||
}
|
||||
if ty, tm, td := t.In(tz).Date(); ty != y || tm != m || td != d {
|
||||
return time.Time{}, false
|
||||
}
|
||||
return t, true
|
||||
}
|
||||
|
||||
// ensureBusinessHours ensures time is within business hours (8am-8pm)
|
||||
@@ -168,8 +202,7 @@ func calculateFirstSlotTomorrowAt(timezone, startTime string) time.Time {
|
||||
if startMinutes == 0 {
|
||||
startMinutes = 8 * 60 // fallback to 8am
|
||||
}
|
||||
tomorrow := now.Add(24 * time.Hour)
|
||||
firstSlot := time.Date(tomorrow.Year(), tomorrow.Month(), tomorrow.Day(),
|
||||
firstSlot := time.Date(now.Year(), now.Month(), now.Day()+1,
|
||||
startMinutes/60, startMinutes%60, 0, 0, loc)
|
||||
jitter := randomJitter(0, 60)
|
||||
return finalSlot(firstSlot.Add(time.Minute * time.Duration(jitter)))
|
||||
|
||||
@@ -0,0 +1,103 @@
|
||||
package scheduler
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/warmbly/warmbly/internal/models"
|
||||
)
|
||||
|
||||
func weekdaysWindows(start, end int) models.ScheduleWindows {
|
||||
var sw models.ScheduleWindows
|
||||
for wd := time.Monday; wd <= time.Friday; wd++ {
|
||||
sw[wd] = []models.TimeInterval{{Start: start, End: end}}
|
||||
}
|
||||
return sw
|
||||
}
|
||||
|
||||
// inWindow reports whether t falls inside one of sw's intervals on its own local day.
|
||||
func inWindow(t time.Time, sw models.ScheduleWindows, tz *time.Location) bool {
|
||||
local := t.In(tz)
|
||||
minute := local.Hour()*60 + local.Minute()
|
||||
for _, iv := range sw[local.Weekday()] {
|
||||
if minute >= iv.Start && minute < iv.End {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// The weekend a zone leaves daylight saving time has a 25-hour day, which the
|
||||
// slot search must walk past like any other day (issue #1044).
|
||||
func TestNextScheduleSlotAcrossDaylightSavingChanges(t *testing.T) {
|
||||
sw := weekdaysWindows(8*60, 18*60)
|
||||
tests := []struct {
|
||||
zone string
|
||||
from time.Time
|
||||
want string // local wall time of the slot
|
||||
}{
|
||||
{"Europe/Madrid", time.Date(2026, 10, 23, 17, 30, 0, 0, time.UTC), "2026-10-26 08:00"},
|
||||
{"Europe/Madrid", time.Date(2026, 10, 24, 0, 10, 0, 0, time.UTC), "2026-10-26 08:00"},
|
||||
{"Europe/Madrid", time.Date(2026, 10, 25, 12, 0, 0, 0, time.UTC), "2026-10-26 08:00"},
|
||||
{"Europe/Bucharest", time.Date(2026, 10, 24, 1, 30, 0, 0, time.UTC), "2026-10-26 08:00"},
|
||||
{"America/Detroit", time.Date(2026, 10, 31, 3, 0, 0, 0, time.UTC), "2026-11-02 08:00"},
|
||||
{"America/Chicago", time.Date(2026, 11, 1, 15, 0, 0, 0, time.UTC), "2026-11-02 08:00"},
|
||||
{"Europe/Madrid", time.Date(2026, 3, 28, 12, 0, 0, 0, time.UTC), "2026-03-30 08:00"},
|
||||
{"Europe/Madrid", time.Date(2026, 10, 17, 0, 10, 0, 0, time.UTC), "2026-10-19 08:00"},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
tz, err := time.LoadLocation(tt.zone)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got := nextScheduleSlot(tt.from, sw, tz).In(tz).Format("2006-01-02 15:04")
|
||||
if got != tt.want {
|
||||
t.Errorf("%s from %s: got %s, want %s", tt.zone, tt.from.Format(time.RFC3339), got, tt.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// A window that opens at midnight, or inside the hour a zone skips, still
|
||||
// resolves to an instant inside that window and never to one before from.
|
||||
func TestNextScheduleSlotWindowStartInSkippedTime(t *testing.T) {
|
||||
var sw models.ScheduleWindows
|
||||
for wd := range sw {
|
||||
sw[wd] = []models.TimeInterval{{Start: 0, End: 60}, {Start: 60 + 45, End: 2 * 60}, {Start: 2*60 + 30, End: 4 * 60}}
|
||||
}
|
||||
for _, zone := range []string{"America/Santiago", "America/Havana", "Atlantic/Azores", "America/New_York", "Europe/Madrid", "Australia/Lord_Howe"} {
|
||||
tz, err := time.LoadLocation(zone)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for from := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC); from.Year() == 2026; from = from.Add(17 * time.Minute) {
|
||||
got := nextScheduleSlot(from, sw, tz)
|
||||
if got.Before(from) || got.Sub(from) > 26*time.Hour || !inWindow(got, sw, tz) {
|
||||
t.Fatalf("%s from %s: got %s", zone, from.In(tz).Format(time.RFC3339), got.In(tz).Format(time.RFC3339))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Every daylight-saving transition in a year, in zones that change at
|
||||
// midnight, in the early hours, by half an hour, and in both hemispheres.
|
||||
func TestNextScheduleSlotSweepsTimeZones(t *testing.T) {
|
||||
sw := weekdaysWindows(8*60, 18*60)
|
||||
zones := []string{
|
||||
"UTC", "Europe/Madrid", "Europe/London", "Europe/Bucharest", "America/New_York",
|
||||
"America/Chicago", "America/Santiago", "America/Havana", "America/Asuncion",
|
||||
"Atlantic/Azores", "Australia/Sydney", "Australia/Lord_Howe", "Pacific/Auckland",
|
||||
"Asia/Tehran", "Africa/Cairo",
|
||||
}
|
||||
for _, zone := range zones {
|
||||
tz, err := time.LoadLocation(zone)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for from := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC); from.Year() == 2026; from = from.Add(29 * time.Minute) {
|
||||
got := nextScheduleSlot(from, sw, tz)
|
||||
if got.Before(from) || got.Sub(from) > 4*24*time.Hour || !inWindow(got, sw, tz) {
|
||||
t.Fatalf("%s from %s: got %s", zone, from.In(tz).Format(time.RFC3339), got.In(tz).Format(time.RFC3339))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -288,8 +288,7 @@ func calculateFirstSlotTomorrow(timezone string) time.Time {
|
||||
now := time.Now().In(loc)
|
||||
|
||||
// Tomorrow at 8am + random jitter (0-60 minutes)
|
||||
tomorrow := now.Add(24 * time.Hour)
|
||||
firstSlot := time.Date(tomorrow.Year(), tomorrow.Month(), tomorrow.Day(), 8, 0, 0, 0, loc)
|
||||
firstSlot := time.Date(now.Year(), now.Month(), now.Day()+1, 8, 0, 0, 0, loc)
|
||||
|
||||
// Add random jitter
|
||||
jitter := randomJitter(0, 60)
|
||||
|
||||
Reference in New Issue
Block a user