Skip to content

Commit

Permalink
fix: saving of event outside of tx
Browse files Browse the repository at this point in the history
  • Loading branch information
adityathebe committed Nov 15, 2024
1 parent d812505 commit 7c3110a
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions notification/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}).
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 7c3110a

Please sign in to comment.