-- name: CreateAttachmentJob :one INSERT INTO attachment_jobs (attachment_id, pipeline_version, next_attempt_at, max_attempts) VALUES (sqlc.arg(attachment_id), sqlc.arg(pipeline_version), sqlc.arg(available_at)::timestamptz, sqlc.arg(max_attempts)) RETURNING *; -- name: ClaimJob :one WITH claimed AS ( UPDATE attachment_jobs AS j SET lease_generation = j.lease_generation + 1, lease_until = sqlc.arg(new_lease_until)::timestamptz, attempts_started = j.attempts_started + 1, next_attempt_at = NULL, updated_at = clock_timestamp() FROM attachments AS a WHERE j.id = sqlc.arg(job_id) AND a.id = j.attachment_id AND a.pipeline_version = j.pipeline_version AND j.attempts_started < j.max_attempts AND sqlc.arg(new_lease_until)::timestamptz > clock_timestamp() AND ( (a.status = 'queued' AND j.lease_until IS NULL AND j.next_attempt_at <= clock_timestamp()) OR (a.status = 'processing' AND j.lease_until <= clock_timestamp()) ) RETURNING j.* ), expired AS ( UPDATE attachment_attempts AS attempt SET outcome = 'lease_expired', error_code = 'lease_expired', finished_at = claimed.updated_at FROM claimed WHERE attempt.job_id = claimed.id AND attempt.lease_generation = claimed.lease_generation - 1 AND attempt.finished_at IS NULL ), recorded AS ( INSERT INTO attachment_attempts (job_id, lease_generation, attempt, started_at) SELECT id, lease_generation, attempts_started, updated_at FROM claimed RETURNING job_id ), processing AS ( UPDATE attachments AS a SET status = 'processing', updated_at = claimed.updated_at FROM claimed WHERE a.id = claimed.attachment_id RETURNING a.id ) SELECT claimed.* FROM claimed JOIN recorded ON recorded.job_id = claimed.id JOIN processing ON processing.id = claimed.attachment_id; -- name: LockActiveAttachmentJob :one -- Hold this lock through variant insertion and CompleteJob. Roll back on a zero-row completion. SELECT j.* FROM attachment_jobs AS j JOIN attachments AS a ON a.id = j.attachment_id WHERE j.id = sqlc.arg(job_id) AND j.lease_generation = sqlc.arg(lease_generation) AND j.lease_until > clock_timestamp() AND a.status = 'processing' AND a.pipeline_version = j.pipeline_version FOR UPDATE OF j; -- name: CompleteJob :execrows WITH completed AS ( UPDATE attachment_jobs AS j SET lease_until = NULL, next_attempt_at = NULL, updated_at = clock_timestamp(), published_original_key = sqlc.arg(object_key)::text FROM attachments AS a WHERE j.id = sqlc.arg(job_id) AND j.lease_generation = sqlc.arg(lease_generation) AND j.lease_until > clock_timestamp() AND a.id = j.attachment_id AND a.pipeline_version = j.pipeline_version AND a.status = 'processing' AND (SELECT count(*) FROM attachment_variants AS v WHERE v.attachment_id = a.id AND v.pipeline_version = j.pipeline_version) = 3 RETURNING j.* ), recorded AS ( UPDATE attachment_attempts AS attempt SET outcome = 'ready', finished_at = completed.updated_at FROM completed WHERE attempt.job_id = completed.id AND attempt.lease_generation = completed.lease_generation ) UPDATE attachments AS a SET status = 'ready', object_key = sqlc.arg(object_key)::text, updated_at = completed.updated_at FROM completed WHERE a.id = completed.attachment_id; -- name: ScheduleRetry :execrows WITH scheduled AS ( UPDATE attachment_jobs AS j SET lease_until = NULL, next_attempt_at = sqlc.arg(available_at)::timestamptz, updated_at = clock_timestamp() FROM attachments AS a WHERE j.id = sqlc.arg(job_id) AND j.lease_generation = sqlc.arg(lease_generation) AND j.lease_until > clock_timestamp() AND a.id = j.attachment_id AND a.pipeline_version = j.pipeline_version AND a.status = 'processing' AND j.attempts_started < j.max_attempts AND sqlc.arg(available_at)::timestamptz > clock_timestamp() RETURNING j.* ), recorded AS ( UPDATE attachment_attempts AS attempt SET outcome = 'retry', error_code = sqlc.arg(error_code)::text, finished_at = scheduled.updated_at FROM scheduled WHERE attempt.job_id = scheduled.id AND attempt.lease_generation = scheduled.lease_generation ), event AS ( INSERT INTO outbox (job_id, payload, available_at, dedup_key) SELECT id, attachment_job_payload(id, attachment_id, pipeline_version, attempts_started + 1, updated_at), next_attempt_at, 'retry/' || id || '/' || (attempts_started + 1) FROM scheduled ) UPDATE attachments AS a SET status = 'queued', updated_at = scheduled.updated_at FROM scheduled WHERE a.id = scheduled.attachment_id; -- name: FailJob :execrows WITH failed AS ( UPDATE attachment_jobs AS j SET lease_until = NULL, next_attempt_at = NULL, updated_at = clock_timestamp() FROM attachments AS a WHERE j.id = sqlc.arg(job_id) AND j.lease_generation = sqlc.arg(lease_generation) AND ( j.lease_until > clock_timestamp() OR (j.lease_until <= clock_timestamp() AND j.attempts_started = j.max_attempts AND sqlc.arg(error_code)::text = 'retry_exhausted') ) AND a.id = j.attachment_id AND a.pipeline_version = j.pipeline_version AND a.status = 'processing' RETURNING j.* ), recorded AS ( UPDATE attachment_attempts AS attempt SET outcome = 'failed', error_code = sqlc.arg(error_code)::text, finished_at = failed.updated_at FROM failed WHERE attempt.job_id = failed.id AND attempt.lease_generation = failed.lease_generation ) UPDATE attachments AS a SET status = 'failed', error_code = sqlc.arg(error_code)::text, updated_at = failed.updated_at FROM failed WHERE a.id = failed.attachment_id; -- name: ReplayFailed :execrows WITH replayed AS ( UPDATE attachment_jobs AS j SET max_attempts = j.attempts_started + sqlc.arg(extra_attempts)::int, next_attempt_at = clock_timestamp(), updated_at = clock_timestamp() FROM attachments AS a WHERE j.id = sqlc.arg(job_id) AND a.id = j.attachment_id AND a.pipeline_version = j.pipeline_version AND a.status = 'failed' AND a.staging_key IS NOT NULL AND j.lease_until IS NULL AND j.next_attempt_at IS NULL AND NOT EXISTS ( SELECT 1 FROM attachment_replays AS r WHERE r.request_id = sqlc.arg(request_id)::uuid ) RETURNING j.* ), recorded AS ( INSERT INTO attachment_replays (request_id, job_id, operator_id, reason, extra_attempts) SELECT sqlc.arg(request_id)::uuid, id, sqlc.arg(operator_id), sqlc.arg(reason), sqlc.arg(extra_attempts) FROM replayed ), event AS ( INSERT INTO outbox (job_id, payload, available_at, dedup_key) SELECT id, attachment_job_payload(id, attachment_id, pipeline_version, attempts_started + 1, updated_at), next_attempt_at, 'replay/' || id || '/' || sqlc.arg(request_id)::uuid FROM replayed ) UPDATE attachments AS a SET status = 'queued', error_code = NULL, updated_at = replayed.updated_at FROM replayed WHERE a.id = replayed.attachment_id; -- name: GetAttachmentJobState :one SELECT j.id, j.attachment_id, j.pipeline_version, CASE WHEN a.pipeline_version = j.pipeline_version THEN a.status ELSE 'superseded' END::text AS status, j.lease_generation, j.lease_until, coalesce(j.lease_until <= clock_timestamp(), false)::boolean AS lease_expired, j.attempts_started, j.max_attempts, j.next_attempt_at FROM attachment_jobs AS j JOIN attachments AS a ON a.id = j.attachment_id WHERE j.id = sqlc.arg(job_id)::uuid; -- name: ListFailedAttachmentJobs :many SELECT j.id, j.attachment_id, j.attempts_started, a.error_code, a.updated_at FROM attachment_jobs j JOIN attachments a ON a.id = j.attachment_id AND a.pipeline_version = j.pipeline_version WHERE a.status = 'failed' ORDER BY a.updated_at DESC, j.id LIMIT sqlc.arg(batch_size); -- name: LockAttachmentJobForPipeline :one SELECT j.* FROM attachment_jobs j WHERE j.attachment_id = $1 AND j.pipeline_version = $2 FOR UPDATE; -- name: LockAttachmentJobForReplay :one SELECT j.* FROM attachment_jobs j WHERE j.id = $1 FOR UPDATE; -- name: ClearStagingReference :execrows UPDATE attachments SET object_key = CASE WHEN status = 'failed' AND object_key = staging_key THEN NULL ELSE object_key END, staging_key = NULL WHERE id = sqlc.arg(id) AND staging_key = sqlc.arg(staging_key); -- name: GetAttachmentByStagingKey :one SELECT * FROM attachments WHERE staging_key = sqlc.arg(staging_key)::text;