diff --git a/notification/job.go b/notification/job.go index 067d424c..b1d085c1 100644 --- a/notification/job.go +++ b/notification/job.go @@ -83,11 +83,11 @@ func ScrapeAndGetHealth(ctx context.Context, configID string) (models.Health, er } } -func ProcessPendingNotification(ctx context.Context) (bool, error) { +func ProcessPendingNotification(parentCtx context.Context) (bool, error) { var noMorePending bool - err := ctx.DB().Transaction(func(tx *gorm.DB) error { - ctx = ctx.WithDB(tx, ctx.Pool()) + err := parentCtx.DB().Transaction(func(tx *gorm.DB) error { + ctx := parentCtx.WithDB(tx, parentCtx.Pool()) var pending []models.NotificationSendHistory if err := ctx.DB().Clauses(clause.Locking{Strength: clause.LockingStrengthUpdate, Options: clause.LockingOptionsSkipLocked}). @@ -132,7 +132,8 @@ func ProcessPendingNotification(ctx context.Context) (bool, error) { var currentHealth *models.Health if celEnv.ConfigItem != nil { - if ch, err := ScrapeAndGetHealth(ctx, celEnv.ConfigItem.ID.String()); err != nil { + // This must be done outside of transaction as we want to immediately save the event. + if ch, err := ScrapeAndGetHealth(parentCtx, celEnv.ConfigItem.ID.String()); err != nil { return fmt.Errorf("failed to get resource health from cel env: %w", err) } else { currentHealth = &ch