Skip to content

Commit 9090d85

Browse files
lucasmrodgetvictor
andcommitted
Calendar update event if meeting occurring now (#17815)
#17441 --------- Co-authored-by: Victor Lyuboslavsky <[email protected]>
1 parent 2e56563 commit 9090d85

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

cmd/fleet/calendar_cron.go

+19-3
Original file line numberDiff line numberDiff line change
@@ -296,9 +296,7 @@ func processFailingHostExistingCalendarEvent(
296296
updated := false
297297
now := time.Now()
298298

299-
// Check the user calendar every 30 minutes (and not every time)
300-
// to reduce load on both Fleet and the calendar service.
301-
if time.Since(calendarEvent.UpdatedAt) > 30*time.Minute {
299+
if shouldReloadCalendarEvent(now, calendarEvent, hostCalendarEvent) {
302300
var err error
303301
updatedEvent, _, err = calendar.GetAndUpdateEvent(calendarEvent, func(conflict bool) string {
304302
return generateCalendarEventBody(orgName, host.HostDisplayName, conflict)
@@ -365,6 +363,24 @@ func processFailingHostExistingCalendarEvent(
365363
return nil
366364
}
367365

366+
func shouldReloadCalendarEvent(now time.Time, calendarEvent *fleet.CalendarEvent, hostCalendarEvent *fleet.HostCalendarEvent) bool {
367+
// Check the user calendar every 30 minutes (and not every cron run)
368+
// to reduce load on both Fleet and the calendar service.
369+
if time.Since(calendarEvent.UpdatedAt) > 30*time.Minute {
370+
return true
371+
}
372+
// If the event is supposed to be happening now, we want to check if the user moved/deleted the
373+
// event on the last minute.
374+
if eventHappeningNow(now, calendarEvent) && hostCalendarEvent.WebhookStatus == fleet.CalendarWebhookStatusNone {
375+
return true
376+
}
377+
return false
378+
}
379+
380+
func eventHappeningNow(now time.Time, calendarEvent *fleet.CalendarEvent) bool {
381+
return !now.Before(calendarEvent.StartTime) && now.Before(calendarEvent.EndTime)
382+
}
383+
368384
func sameDate(t1 time.Time, t2 time.Time) bool {
369385
y1, m1, d1 := t1.Date()
370386
y2, m2, d2 := t2.Date()

0 commit comments

Comments
 (0)