refactor(queue): single-statement complete + dead-letter retirement
This commit is contained in:
@@ -87,24 +87,15 @@ func (s *Store) List(ctx context.Context, limit int) ([]jobs.Job, error) {
|
||||
}
|
||||
|
||||
func (s *Store) Complete(ctx context.Context, id, owner string, now time.Time) error {
|
||||
tx, err := s.db.Begin(ctx)
|
||||
if err != nil {
|
||||
// completeJobSQL is one statement: mark the job completed AND retire any
|
||||
// open dead-letter rows for it. The EXISTS projection is the lease check.
|
||||
var completed bool
|
||||
if err := s.db.QueryRow(ctx, completeJobSQL, id, owner, now).Scan(&completed); err != nil {
|
||||
return fmt.Errorf("complete job: %w", err)
|
||||
}
|
||||
defer func() { _ = tx.Rollback(ctx) }()
|
||||
tag, err := tx.Exec(ctx, completeJobSQL, id, owner, now)
|
||||
if err != nil {
|
||||
return fmt.Errorf("complete job: %w", err)
|
||||
}
|
||||
if tag.RowsAffected() != 1 {
|
||||
if !completed {
|
||||
return ErrLeaseLost
|
||||
}
|
||||
if _, err := tx.Exec(ctx, retireOpenFailuresSQL, id, now); err != nil {
|
||||
return fmt.Errorf("complete job: retire failures: %w", err)
|
||||
}
|
||||
if err := tx.Commit(ctx); err != nil {
|
||||
return fmt.Errorf("complete job: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user