Разделите настройки команд worker и сохраняйте причину сбоя
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user