Разделите настройки команд worker и сохраняйте причину сбоя

This commit is contained in:
2026-11-20 10:54:34 +03:00
parent 42a9bf2071
commit 8f90c78b98
8 changed files with 303 additions and 141 deletions
+19 -7
View File
@@ -11,11 +11,23 @@ import (
var errorCodePattern = regexp.MustCompile("^[a-z][a-z0-9_]{0,63}$")
func (s *Store) Fail(ctx context.Context, c *Claim, code string, permanent bool, policy RetryPolicy) (bool, error) {
type Failure struct {
Code string
Permanent bool
}
type leaseState bool
const (
activeLease leaseState = false
expiredLease leaseState = true
)
func (s *Store) Fail(ctx context.Context, c *Claim, failure Failure, policy RetryPolicy) (bool, error) {
if err := policy.validate(); err != nil {
return false, err
}
if !errorCodePattern.MatchString(code) {
if !errorCodePattern.MatchString(failure.Code) {
return false, errors.New("invalid failure code")
}
var changed bool
@@ -31,7 +43,7 @@ func (s *Store) Fail(ctx context.Context, c *Claim, code string, permanent bool,
if err != nil {
return err
}
changed, err = finishAttempt(ctx, tx, c.JobID, budget, code, permanent, false, policy)
changed, err = finishAttempt(ctx, tx, c.JobID, budget, failure, activeLease, policy)
return err
})
return changed && err == nil, err
@@ -54,7 +66,7 @@ func (s *Store) RecoverOne(ctx context.Context, policy RetryPolicy) (bool, error
if err != nil {
return err
}
changed, err = finishAttempt(ctx, tx, id, budget, "lease_expired", false, true, policy)
changed, err = finishAttempt(ctx, tx, id, budget, Failure{Code: "lease_expired"}, expiredLease, policy)
return err
})
return changed && err == nil, err
@@ -62,13 +74,13 @@ func (s *Store) RecoverOne(ctx context.Context, policy RetryPolicy) (bool, error
// The caller holds the job row. Only the recovery path may advance an expired
// attempt. A worker whose lease elapsed during its SQL call has lost ownership.
func finishAttempt(ctx context.Context, tx pgx.Tx, id uuid.UUID, budget int, code string, permanent, expired bool, p RetryPolicy) (bool, error) {
retry := !permanent && budget < p.MaxAttempts
func finishAttempt(ctx context.Context, tx pgx.Tx, id uuid.UUID, budget int, failure Failure, lease leaseState, p RetryPolicy) (bool, error) {
retry := !failure.Permanent && budget < p.MaxAttempts
tag, err := tx.Exec(ctx, `UPDATE attachment_jobs
SET state=CASE WHEN $3 THEN 'queued' ELSE 'failed' END,
attempt=attempt+$3::boolean::int, budget_attempt=budget_attempt+$3::boolean::int,
available_at=clock_timestamp()+$4::bigint*interval '1 microsecond',
published_at=NULL, lease_token=NULL, lease_until=NULL, error_code=$2, updated_at=clock_timestamp()
WHERE job_id=$1 AND (lease_until<=clock_timestamp())=$5`, id, code, retry, p.delay(budget).Microseconds(), expired)
WHERE job_id=$1 AND (lease_until<=clock_timestamp())=$5`, id, failure.Code, retry, p.delay(budget).Microseconds(), bool(lease))
return tag.RowsAffected() == 1, err
}
+8 -8
View File
@@ -101,7 +101,7 @@ func TestPipelineTransitions(t *testing.T) {
newJob(t)
first := claim(t, publish(t))
policy := RetryPolicy{MaxAttempts: 3, BaseDelay: 4 * time.Second, MaxDelay: 5 * time.Second}
if ok, err := s.Fail(ctx, first, "storage_timeout", false, policy); !ok || err != nil {
if ok, err := s.Fail(ctx, first, Failure{Code: "storage_timeout"}, policy); !ok || err != nil {
t.Fatalf("failed %v %v", ok, err)
}
var seconds float64
@@ -121,7 +121,7 @@ func TestPipelineTransitions(t *testing.T) {
}
exec(t, `UPDATE attachment_jobs SET available_at=clock_timestamp()`)
second := claim(t, publish(t))
if ok, err := s.Fail(ctx, second, "storage_timeout", false, policy); !ok || err != nil {
if ok, err := s.Fail(ctx, second, Failure{Code: "storage_timeout"}, policy); !ok || err != nil {
t.Fatalf("second failure %v %v", ok, err)
}
if err := db.Pool.QueryRow(ctx, `SELECT extract(epoch FROM available_at-updated_at)::float8 FROM attachment_jobs`).Scan(&seconds); err != nil {
@@ -177,7 +177,7 @@ func TestPipelineTransitions(t *testing.T) {
if ok, err := s.Complete(ctx, old, variants(old)); err != nil || ok {
t.Fatalf("stale commit %v %v", ok, err)
}
if ok, err := s.Fail(ctx, old, "source_missing", true, policy); err != nil || ok {
if ok, err := s.Fail(ctx, old, Failure{Code: "source_missing", Permanent: true}, policy); err != nil || ok {
t.Fatalf("stale failure %v %v", ok, err)
}
for _, dup := range []Job{current.Job, j, current.Job} {
@@ -208,7 +208,7 @@ func TestPipelineTransitions(t *testing.T) {
if ok, err := s.Complete(ctx, first, variants(first)); err != nil || ok {
t.Fatalf("expired commit %v %v", ok, err)
}
if ok, err := s.Fail(ctx, first, "backend_unavailable", false, policy); err != nil || ok {
if ok, err := s.Fail(ctx, first, Failure{Code: "backend_unavailable"}, policy); err != nil || ok {
t.Fatalf("expired fail %v %v", ok, err)
}
if ok, err := s.RecoverOne(ctx, policy); err != nil || !ok {
@@ -222,7 +222,7 @@ func TestPipelineTransitions(t *testing.T) {
if ok, err := s.Complete(ctx, first, variants(first)); err != nil || ok {
t.Fatalf("lost lease commit %v %v", ok, err)
}
if ok, err := s.Fail(ctx, second, "backend_unavailable", false, policy); err != nil || !ok {
if ok, err := s.Fail(ctx, second, Failure{Code: "backend_unavailable"}, policy); err != nil || !ok {
t.Fatalf("second error %v %v", ok, err)
}
failed, err := s.Failed(ctx)
@@ -247,12 +247,12 @@ func TestPipelineTransitions(t *testing.T) {
if replay.Attempt != 3 || replay.JobID != first.JobID {
t.Fatalf("replay identity %+v", replay)
}
if ok, err := s.Fail(ctx, replay, "backend_unavailable", false, policy); !ok || err != nil {
if ok, err := s.Fail(ctx, replay, Failure{Code: "backend_unavailable"}, policy); !ok || err != nil {
t.Fatalf("fresh retry budget %v %v", ok, err)
}
exec(t, `UPDATE attachment_jobs SET available_at=clock_timestamp()`)
last := claim(t, publish(t))
if ok, err := s.Fail(ctx, last, "source_missing", true, policy); !ok || err != nil {
if ok, err := s.Fail(ctx, last, Failure{Code: "source_missing", Permanent: true}, policy); !ok || err != nil {
t.Fatalf("permanent fail %v %v", ok, err)
}
var n int
@@ -289,7 +289,7 @@ func TestPipelineTransitions(t *testing.T) {
if version != 1 || status != "ready" || resultCount != 0 {
t.Fatalf("partial publication v%d status=%s rows=%d", version, status, resultCount)
}
if ok, err := s.Fail(ctx, next, "invalid_image", true, policy); err != nil || !ok {
if ok, err := s.Fail(ctx, next, Failure{Code: "invalid_image", Permanent: true}, policy); err != nil || !ok {
t.Fatalf("permanent error %v %v", ok, err)
}
failed, err := s.Failed(ctx)