refactor(queue): single-statement complete + dead-letter retirement

This commit is contained in:
姚章浩
2026-09-15 14:40:19 +08:00
parent 767684bb1f
commit e8dae5c7f7
3 changed files with 52 additions and 37 deletions
+5 -14
View File
@@ -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
}