This commit is contained in:
2026-06-20 19:16:44 +08:00
parent db3d030169
commit dbb87823e8
26 changed files with 676 additions and 115 deletions
+39 -15
View File
@@ -11,35 +11,49 @@ import (
"github.com/jackc/pgx/v5/pgtype"
)
const completeJob = `-- name: CompleteJob :exec
const completeJob = `-- name: CompleteJob :execrows
UPDATE jobs
SET status='completed', completed_at=now(),
leased_at=NULL, leased_by=NULL, last_error=NULL,
updated_at=now()
WHERE id=$1
WHERE id=$1 AND leased_by=$2 AND status='running'
`
func (q *Queries) CompleteJob(ctx context.Context, id pgtype.UUID) error {
_, err := q.db.Exec(ctx, completeJob, id)
return err
type CompleteJobParams struct {
ID pgtype.UUID `json:"id"`
LeasedBy *string `json:"leased_by"`
}
const deadLetterJob = `-- name: DeadLetterJob :exec
// 围栏谓词:只有持有当前租约的 worker 才能写回,防止被 reaper 重新租出后旧 worker 覆盖新一轮状态。
func (q *Queries) CompleteJob(ctx context.Context, arg CompleteJobParams) (int64, error) {
result, err := q.db.Exec(ctx, completeJob, arg.ID, arg.LeasedBy)
if err != nil {
return 0, err
}
return result.RowsAffected(), nil
}
const deadLetterJob = `-- name: DeadLetterJob :execrows
UPDATE jobs
SET status='dead', completed_at=now(),
leased_at=NULL, leased_by=NULL, last_error=$2,
updated_at=now()
WHERE id=$1
WHERE id=$1 AND leased_by=$3 AND status='running'
`
type DeadLetterJobParams struct {
ID pgtype.UUID `json:"id"`
LastError *string `json:"last_error"`
LeasedBy *string `json:"leased_by"`
}
func (q *Queries) DeadLetterJob(ctx context.Context, arg DeadLetterJobParams) error {
_, err := q.db.Exec(ctx, deadLetterJob, arg.ID, arg.LastError)
return err
// 围栏谓词同上。
func (q *Queries) DeadLetterJob(ctx context.Context, arg DeadLetterJobParams) (int64, error) {
result, err := q.db.Exec(ctx, deadLetterJob, arg.ID, arg.LastError, arg.LeasedBy)
if err != nil {
return 0, err
}
return result.RowsAffected(), nil
}
const enqueueJob = `-- name: EnqueueJob :one
@@ -83,25 +97,35 @@ func (q *Queries) EnqueueJob(ctx context.Context, arg EnqueueJobParams) (Job, er
return i, err
}
const failJobWithRetry = `-- name: FailJobWithRetry :exec
const failJobWithRetry = `-- name: FailJobWithRetry :execrows
UPDATE jobs
SET status='pending',
scheduled_at=$2,
leased_at=NULL, leased_by=NULL,
last_error=$3,
updated_at=now()
WHERE id=$1
WHERE id=$1 AND leased_by=$4 AND status='running'
`
type FailJobWithRetryParams struct {
ID pgtype.UUID `json:"id"`
ScheduledAt pgtype.Timestamptz `json:"scheduled_at"`
LastError *string `json:"last_error"`
LeasedBy *string `json:"leased_by"`
}
func (q *Queries) FailJobWithRetry(ctx context.Context, arg FailJobWithRetryParams) error {
_, err := q.db.Exec(ctx, failJobWithRetry, arg.ID, arg.ScheduledAt, arg.LastError)
return err
// 围栏谓词同上。
func (q *Queries) FailJobWithRetry(ctx context.Context, arg FailJobWithRetryParams) (int64, error) {
result, err := q.db.Exec(ctx, failJobWithRetry,
arg.ID,
arg.ScheduledAt,
arg.LastError,
arg.LeasedBy,
)
if err != nil {
return 0, err
}
return result.RowsAffected(), nil
}
const getJob = `-- name: GetJob :one