Mark calendar webhook as errored so that it doesn't get stuck in pending and keeps logging warnings. (#18021)

Mark calendar webhook as errored so that it doesn't get stuck in pending
and keeps logging warnings.

This situation only occurs when webhook fired but returned an error. It
will try to fire again, but if it tries after the event concluded, then
we'll mark it as errored.
This commit is contained in:
Victor Lyuboslavsky 2024-04-02 17:17:04 -05:00 committed by GitHub
commit 25dc6829f5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 9 additions and 2 deletions

View File

@ -18,6 +18,7 @@ const (
CalendarWebhookStatusNone CalendarWebhookStatus = iota
CalendarWebhookStatusPending
CalendarWebhookStatusSent
CalendarWebhookStatusError
)
type HostCalendarEvent struct {

View File

@ -1133,16 +1133,22 @@ func processCalendarPolicies(
now := time.Now()
if now.Before(calendarEvent.StartTime) {
level.Warn(logger).Log("msg", "results came too early", "now", now, "start_time", calendarEvent.StartTime)
if err = ds.UpdateHostCalendarWebhookStatus(context.Background(), host.ID, fleet.CalendarWebhookStatusError); err != nil {
level.Error(logger).Log("msg", "mark webhook as errored early", "err", err)
}
return nil
}
//
// TODO(lucas): Discuss.
//
const allowedTimeBeforeEndTime = 5 * time.Minute // up to 5 minutes before the end_time
const allowedTimeRelativeToEndTime = 5 * time.Minute // up to 5 minutes after the end_time to allow for short (0-time) event times
if now.After(calendarEvent.EndTime.Add(-allowedTimeBeforeEndTime)) {
if now.After(calendarEvent.EndTime.Add(allowedTimeRelativeToEndTime)) {
level.Warn(logger).Log("msg", "results came too late", "now", now, "end_time", calendarEvent.EndTime)
if err = ds.UpdateHostCalendarWebhookStatus(context.Background(), host.ID, fleet.CalendarWebhookStatusError); err != nil {
level.Error(logger).Log("msg", "mark webhook as errored late", "err", err)
}
return nil
}