Skip to content

Commit

Permalink
move gql create alert into Tx (#3776)
Browse files Browse the repository at this point in the history
  • Loading branch information
mastercactapus authored Mar 26, 2024
1 parent b0a10aa commit 4bf971e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
13 changes: 1 addition & 12 deletions alert/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ func (s *Store) UpdateManyAlertStatus(ctx context.Context, status Status, alertI
return updatedIDs, nil
}

func (s *Store) Create(ctx context.Context, a *Alert) (*Alert, error) {
func (s *Store) CreateTx(ctx context.Context, tx *sql.Tx, a *Alert) (*Alert, error) {
n, err := a.Normalize() // validation
if err != nil {
return nil, err
Expand All @@ -489,12 +489,6 @@ func (s *Store) Create(ctx context.Context, a *Alert) (*Alert, error) {
return nil, err
}

tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return nil, err
}
defer sqlutil.Rollback(ctx, "alert: create", tx)

_, err = tx.StmtContext(ctx, s.lockSvc).ExecContext(ctx, n.ServiceID)
if err != nil {
return nil, err
Expand All @@ -507,11 +501,6 @@ func (s *Store) Create(ctx context.Context, a *Alert) (*Alert, error) {

s.logDB.MustLogTx(ctx, tx, n.ID, alertlog.TypeCreated, meta)

err = tx.Commit()
if err != nil {
return nil, err
}

ctx = log.WithFields(ctx, log.Fields{"AlertID": n.ID, "ServiceID": n.ServiceID})
log.Logf(ctx, "Alert created.")
metricCreatedTotal.WithLabelValues(n.ServiceID).Inc()
Expand Down
12 changes: 11 additions & 1 deletion graphql2/graphqlapp/alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,17 @@ func (m *Mutation) CreateAlert(ctx context.Context, input graphql2.CreateAlertIn
a.Dedup = alert.NewUserDedup(*input.Dedup)
}

return m.AlertStore.Create(ctx, a)
var newAlert *alert.Alert
err := withContextTx(ctx, m.DB, func(ctx context.Context, tx *sql.Tx) error {
var err error
newAlert, err = m.AlertStore.CreateTx(ctx, tx, a)
return err
})
if err != nil {
return nil, err
}

return newAlert, nil
}

func (a *Alert) NoiseReason(ctx context.Context, raw *alert.Alert) (*string, error) {
Expand Down

0 comments on commit 4bf971e

Please sign in to comment.