@@ -198,7 +198,6 @@ func processCalendarFailingHosts(
198
198
hostCalendarEvent , calendarEvent , err := ds .GetHostCalendarEventByEmail (ctx , host .Email )
199
199
200
200
expiredEvent := false
201
- webhookAlreadyFiredThisMonth := false
202
201
if err == nil {
203
202
if hostCalendarEvent .HostID != host .HostID {
204
203
// This calendar event belongs to another host with this associated email,
@@ -217,7 +216,6 @@ func processCalendarFailingHosts(
217
216
// we give a grace period of one day for the host before we schedule a new event.
218
217
continue // continue with next host
219
218
}
220
- webhookAlreadyFiredThisMonth = webhookAlreadyFired && sameMonth (now , calendarEvent .StartTime )
221
219
if calendarEvent .EndTime .Before (now ) {
222
220
expiredEvent = true
223
221
}
@@ -237,7 +235,7 @@ func processCalendarFailingHosts(
237
235
}
238
236
case fleet .IsNotFound (err ) || expiredEvent :
239
237
if err := processFailingHostCreateCalendarEvent (
240
- ctx , ds , userCalendar , orgName , host , webhookAlreadyFiredThisMonth ,
238
+ ctx , ds , userCalendar , orgName , host ,
241
239
); err != nil {
242
240
level .Info (logger ).Log ("msg" , "process failing host create calendar event" , "err" , err )
243
241
continue // continue with next host
@@ -357,21 +355,14 @@ func sameDate(t1 time.Time, t2 time.Time) bool {
357
355
return y1 == y2 && m1 == m2 && d1 == d2
358
356
}
359
357
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
-
366
358
func processFailingHostCreateCalendarEvent (
367
359
ctx context.Context ,
368
360
ds fleet.Datastore ,
369
361
userCalendar fleet.UserCalendar ,
370
362
orgName string ,
371
363
host fleet.HostPolicyMembershipData ,
372
- webhookAlreadyFiredThisMonth bool ,
373
364
) error {
374
- calendarEvent , err := attemptCreatingEventOnUserCalendar (orgName , host , userCalendar , webhookAlreadyFiredThisMonth )
365
+ calendarEvent , err := attemptCreatingEventOnUserCalendar (orgName , host , userCalendar )
375
366
if err != nil {
376
367
return fmt .Errorf ("create event on user calendar: %w" , err )
377
368
}
@@ -385,10 +376,9 @@ func attemptCreatingEventOnUserCalendar(
385
376
orgName string ,
386
377
host fleet.HostPolicyMembershipData ,
387
378
userCalendar fleet.UserCalendar ,
388
- webhookAlreadyFiredThisMonth bool ,
389
379
) (* fleet.CalendarEvent , error ) {
390
380
year , month , today := time .Now ().Date ()
391
- preferredDate := getPreferredCalendarEventDate (year , month , today , webhookAlreadyFiredThisMonth )
381
+ preferredDate := getPreferredCalendarEventDate (year , month , today )
392
382
for {
393
383
calendarEvent , err := userCalendar .CreateEvent (
394
384
preferredDate , func (conflict bool ) string {
@@ -408,10 +398,7 @@ func attemptCreatingEventOnUserCalendar(
408
398
}
409
399
}
410
400
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 {
415
402
const (
416
403
// 3rd Tuesday of Month
417
404
preferredWeekDay = time .Tuesday
@@ -425,12 +412,13 @@ func getPreferredCalendarEventDate(
425
412
}
426
413
preferredDate := firstDayOfMonth .AddDate (0 , 0 , offset + (7 * (preferredOrdinal - 1 )))
427
414
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
432
420
}
433
- preferredDate = addBusinessDay ( today_ )
421
+ return getPreferredCalendarEventDate ( year , month , 1 )
434
422
}
435
423
return preferredDate
436
424
}
0 commit comments