refactor(queue): single-statement complete + dead-letter retirement
This commit is contained in:
@@ -53,25 +53,25 @@ RETURNING j.id, j.type, j.payload, j.state, j.priority, j.available_at,
|
||||
`
|
||||
|
||||
const completeJobSQL = `
|
||||
UPDATE jobs
|
||||
SET state = 'completed',
|
||||
completed_at = $3,
|
||||
lease_owner = NULL,
|
||||
lease_until = NULL,
|
||||
updated_at = $3
|
||||
WHERE id = $1
|
||||
AND lease_owner = $2
|
||||
AND state = 'running'
|
||||
`
|
||||
|
||||
// retireOpenFailuresSQL keeps the dead-letter lane honest: once the job
|
||||
// itself succeeded, its terminal-attempt projection is no longer waiting on
|
||||
// a human (a later automatic retry recovered it).
|
||||
const retireOpenFailuresSQL = `
|
||||
UPDATE job_failures
|
||||
SET state = 'requeued', resolved_at = $2, resolution_actor_ref = 'system:queue',
|
||||
resolution_reason = '任务后续重试成功'
|
||||
WHERE job_id = $1 AND state = 'open'
|
||||
WITH done AS (
|
||||
UPDATE jobs
|
||||
SET state = 'completed',
|
||||
completed_at = $3,
|
||||
lease_owner = NULL,
|
||||
lease_until = NULL,
|
||||
updated_at = $3
|
||||
WHERE id = $1
|
||||
AND lease_owner = $2
|
||||
AND state = 'running'
|
||||
RETURNING id
|
||||
), retired AS (
|
||||
UPDATE job_failures
|
||||
SET state = 'requeued', resolved_at = $3, resolution_actor_ref = 'system:queue',
|
||||
resolution_reason = '任务后续重试成功'
|
||||
WHERE job_id IN (SELECT id FROM done) AND state = 'open'
|
||||
RETURNING 1
|
||||
)
|
||||
SELECT EXISTS (SELECT 1 FROM done)
|
||||
`
|
||||
|
||||
const heartbeatJobSQL = `
|
||||
|
||||
Reference in New Issue
Block a user