From 7c3110a9ff02334555a0274164cc03c6c47ed638 Mon Sep 17 00:00:00 2001 From: Aditya Thebe Date: Fri, 15 Nov 2024 09:42:11 +0545 Subject: [PATCH] fix: saving of event outside of tx --- notification/job.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) 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