Skip to content

Commit d8fa007

Browse files
authored
Always create event next 3rd Tuesday (#17799)
Fix to always create events for next 3rd Tuesday #17441
1 parent 794a322 commit d8fa007

File tree

2 files changed

+75
-66
lines changed

2 files changed

+75
-66
lines changed

cmd/fleet/calendar_cron.go

+10-22
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,6 @@ func processCalendarFailingHosts(
198198
hostCalendarEvent, calendarEvent, err := ds.GetHostCalendarEventByEmail(ctx, host.Email)
199199

200200
expiredEvent := false
201-
webhookAlreadyFiredThisMonth := false
202201
if err == nil {
203202
if hostCalendarEvent.HostID != host.HostID {
204203
// This calendar event belongs to another host with this associated email,
@@ -217,7 +216,6 @@ func processCalendarFailingHosts(
217216
// we give a grace period of one day for the host before we schedule a new event.
218217
continue // continue with next host
219218
}
220-
webhookAlreadyFiredThisMonth = webhookAlreadyFired && sameMonth(now, calendarEvent.StartTime)
221219
if calendarEvent.EndTime.Before(now) {
222220
expiredEvent = true
223221
}
@@ -237,7 +235,7 @@ func processCalendarFailingHosts(
237235
}
238236
case fleet.IsNotFound(err) || expiredEvent:
239237
if err := processFailingHostCreateCalendarEvent(
240-
ctx, ds, userCalendar, orgName, host, webhookAlreadyFiredThisMonth,
238+
ctx, ds, userCalendar, orgName, host,
241239
); err != nil {
242240
level.Info(logger).Log("msg", "process failing host create calendar event", "err", err)
243241
continue // continue with next host
@@ -357,21 +355,14 @@ func sameDate(t1 time.Time, t2 time.Time) bool {
357355
return y1 == y2 && m1 == m2 && d1 == d2
358356
}
359357

360-
func sameMonth(t1 time.Time, t2 time.Time) bool {
361-
y1, m1, _ := t1.Date()
362-
y2, m2, _ := t2.Date()
363-
return y1 == y2 && m1 == m2
364-
}
365-
366358
func processFailingHostCreateCalendarEvent(
367359
ctx context.Context,
368360
ds fleet.Datastore,
369361
userCalendar fleet.UserCalendar,
370362
orgName string,
371363
host fleet.HostPolicyMembershipData,
372-
webhookAlreadyFiredThisMonth bool,
373364
) error {
374-
calendarEvent, err := attemptCreatingEventOnUserCalendar(orgName, host, userCalendar, webhookAlreadyFiredThisMonth)
365+
calendarEvent, err := attemptCreatingEventOnUserCalendar(orgName, host, userCalendar)
375366
if err != nil {
376367
return fmt.Errorf("create event on user calendar: %w", err)
377368
}
@@ -385,10 +376,9 @@ func attemptCreatingEventOnUserCalendar(
385376
orgName string,
386377
host fleet.HostPolicyMembershipData,
387378
userCalendar fleet.UserCalendar,
388-
webhookAlreadyFiredThisMonth bool,
389379
) (*fleet.CalendarEvent, error) {
390380
year, month, today := time.Now().Date()
391-
preferredDate := getPreferredCalendarEventDate(year, month, today, webhookAlreadyFiredThisMonth)
381+
preferredDate := getPreferredCalendarEventDate(year, month, today)
392382
for {
393383
calendarEvent, err := userCalendar.CreateEvent(
394384
preferredDate, func(conflict bool) string {
@@ -408,10 +398,7 @@ func attemptCreatingEventOnUserCalendar(
408398
}
409399
}
410400

411-
func getPreferredCalendarEventDate(
412-
year int, month time.Month, today int,
413-
webhookAlreadyFired bool,
414-
) time.Time {
401+
func getPreferredCalendarEventDate(year int, month time.Month, today int) time.Time {
415402
const (
416403
// 3rd Tuesday of Month
417404
preferredWeekDay = time.Tuesday
@@ -425,12 +412,13 @@ func getPreferredCalendarEventDate(
425412
}
426413
preferredDate := firstDayOfMonth.AddDate(0, 0, offset+(7*(preferredOrdinal-1)))
427414
if today > preferredDate.Day() {
428-
today_ := time.Date(year, month, today, 0, 0, 0, 0, time.UTC)
429-
if webhookAlreadyFired {
430-
nextMonth := today_.AddDate(0, 1, 0) // move to next month
431-
return getPreferredCalendarEventDate(nextMonth.Year(), nextMonth.Month(), 1, false)
415+
// We are past the preferred date, so we move to next month and calculate again.
416+
month := month + 1
417+
if month == 13 {
418+
month = 1
419+
year += 1
432420
}
433-
preferredDate = addBusinessDay(today_)
421+
return getPreferredCalendarEventDate(year, month, 1)
434422
}
435423
return preferredDate
436424
}

cmd/fleet/calendar_cron_test.go

+65-44
Original file line numberDiff line numberDiff line change
@@ -12,72 +12,93 @@ func TestGetPreferredCalendarEventDate(t *testing.T) {
1212
return time.Date(year, month, day, 0, 0, 0, 0, time.UTC)
1313
}
1414
for _, tc := range []struct {
15-
name string
16-
year int
17-
month time.Month
18-
daysStart int
19-
daysEnd int
20-
webhookFiredThisMonth bool
15+
name string
16+
year int
17+
month time.Month
18+
daysStart int
19+
daysEnd int
2120

2221
expected time.Time
2322
}{
2423
{
25-
name: "March 2024 (webhook hasn't fired)",
26-
year: 2024,
27-
month: 3,
28-
daysStart: 1,
29-
daysEnd: 31,
30-
webhookFiredThisMonth: false,
24+
name: "March 2024 (before 3rd Tuesday)",
25+
year: 2024,
26+
month: 3,
27+
daysStart: 1,
28+
daysEnd: 19,
3129

3230
expected: date(2024, 3, 19),
3331
},
3432
{
35-
name: "March 2024 (webhook has fired, days before 3rd Tuesday)",
36-
year: 2024,
37-
month: 3,
38-
daysStart: 1,
39-
daysEnd: 18,
40-
webhookFiredThisMonth: true,
33+
name: "March 2024 (past 3rd Tuesday)",
34+
year: 2024,
35+
month: 3,
36+
daysStart: 20,
37+
daysEnd: 31,
4138

42-
expected: date(2024, 3, 19),
39+
expected: date(2024, 4, 16),
4340
},
4441
{
45-
name: "March 2024 (webhook has fired, days after 3rd Tuesday)",
46-
year: 2024,
47-
month: 3,
48-
daysStart: 20,
49-
daysEnd: 30,
50-
webhookFiredThisMonth: true,
42+
name: "April 2024 (before 3rd Tuesday)",
43+
year: 2024,
44+
month: 4,
45+
daysStart: 1,
46+
daysEnd: 16,
5147

5248
expected: date(2024, 4, 16),
5349
},
5450
{
55-
name: "April 2024 (webhook hasn't fired)",
56-
year: 2024,
57-
month: 4,
58-
daysEnd: 30,
59-
webhookFiredThisMonth: false,
51+
name: "April 2024 (after 3rd Tuesday)",
52+
year: 2024,
53+
month: 4,
54+
daysStart: 17,
55+
daysEnd: 30,
6056

61-
expected: date(2024, 4, 16),
57+
expected: date(2024, 5, 21),
58+
},
59+
{
60+
name: "May 2024 (before 3rd Tuesday)",
61+
year: 2024,
62+
month: 5,
63+
daysStart: 1,
64+
daysEnd: 21,
65+
66+
expected: date(2024, 5, 21),
67+
},
68+
{
69+
name: "May 2024 (after 3rd Tuesday)",
70+
year: 2024,
71+
month: 5,
72+
daysStart: 22,
73+
daysEnd: 31,
74+
75+
expected: date(2024, 6, 18),
76+
},
77+
{
78+
name: "Dec 2024 (before 3rd Tuesday)",
79+
year: 2024,
80+
month: 12,
81+
daysStart: 1,
82+
daysEnd: 17,
83+
84+
expected: date(2024, 12, 17),
85+
},
86+
{
87+
name: "Dec 2024 (after 3rd Tuesday)",
88+
year: 2024,
89+
month: 12,
90+
daysStart: 18,
91+
daysEnd: 31,
92+
93+
expected: date(2025, 1, 21),
6294
},
6395
} {
6496
t.Run(tc.name, func(t *testing.T) {
6597
for day := tc.daysStart; day <= tc.daysEnd; day++ {
66-
actual := getPreferredCalendarEventDate(tc.year, tc.month, day, tc.webhookFiredThisMonth)
98+
actual := getPreferredCalendarEventDate(tc.year, tc.month, day)
6799
require.NotEqual(t, actual.Weekday(), time.Saturday)
68100
require.NotEqual(t, actual.Weekday(), time.Sunday)
69-
if day <= tc.expected.Day() || tc.webhookFiredThisMonth {
70-
require.Equal(t, tc.expected, actual)
71-
} else {
72-
today := date(tc.year, tc.month, day)
73-
if weekday := today.Weekday(); weekday == time.Friday {
74-
require.Equal(t, today.AddDate(0, 0, +3), actual)
75-
} else if weekday == time.Saturday {
76-
require.Equal(t, today.AddDate(0, 0, +2), actual)
77-
} else {
78-
require.Equal(t, today.AddDate(0, 0, +1), actual)
79-
}
80-
}
101+
require.Equal(t, tc.expected, actual)
81102
}
82103
})
83104
}

0 commit comments

Comments
 (0)