-- name: InsertJob :exec INSERT INTO attachment_jobs(id,attachment_id,pipeline_version,traceparent,tracestate) VALUES(sqlc.arg(job_id),sqlc.arg(attachment_id),sqlc.arg(pipeline_version),sqlc.arg(traceparent),sqlc.arg(tracestate)); -- name: QueueAttachment :one UPDATE attachments SET accepted_at=COALESCE(accepted_at,clock_timestamp()),status='queued',updated_at=clock_timestamp() WHERE id=$1 AND status='uploading' AND NOT staging_expired RETURNING *; -- name: PendingJobs :many SELECT j.* FROM attachment_jobs j JOIN attachments a ON a.id=j.attachment_id AND a.pipeline_version=j.pipeline_version WHERE NOT j.completed AND a.status IN ('queued','processing') AND j.next_attempt<=clock_timestamp() AND j.lease_untilclock_timestamp() AND NOT j.completed AND a.status='processing'; -- name: CompleteJob :exec UPDATE attachment_jobs SET completed=true,lease_until='epoch' WHERE id=sqlc.arg(job_id) AND lease_id=sqlc.arg(lease_id); -- name: RetryAttachment :execrows UPDATE attachments a SET status=sqlc.arg(next_status),error_code=sqlc.arg(error_code),updated_at=clock_timestamp() FROM attachment_jobs j WHERE j.id=sqlc.arg(job_id) AND j.attachment_id=a.id AND j.pipeline_version=a.pipeline_version AND j.lease_id=sqlc.arg(lease_id) AND j.lease_until>clock_timestamp() AND NOT j.completed AND a.status='processing'; -- name: RetryJob :exec UPDATE attachment_jobs SET lease_until='epoch',next_attempt=clock_timestamp()+sqlc.arg(delay_ms)::int*interval '1 millisecond',completed=sqlc.arg(completed) WHERE id=sqlc.arg(job_id) AND lease_id=sqlc.arg(lease_id); -- name: ReplayAttachment :one UPDATE attachments SET status='queued',error_code='',updated_at=clock_timestamp() WHERE id=$1 AND status='failed' AND NOT staging_expired RETURNING *; -- name: ReplayJobs :exec UPDATE attachment_jobs SET replay_count=replay_count+1,replayed_at=clock_timestamp(),completed=false,attempt=0,lease_until='epoch',next_attempt='epoch',published_at='epoch' WHERE attachment_id=$1 AND pipeline_version=$2; -- name: AdvancePipeline :one UPDATE attachments SET status='queued',error_code='',variants='[]',pipeline_version=pipeline_version+1,staging_expired=false,updated_at=clock_timestamp() WHERE id=$1 AND (status='ready' OR (status='failed' AND NOT staging_expired)) RETURNING *; -- name: StagingCleanup :many WITH candidates AS ( SELECT id FROM attachments WHERE NOT staging_cleaned AND (status='ready' OR (status IN ('failed','uploading') AND updated_at'ready') FROM candidates c WHERE a.id=c.id AND NOT a.staging_cleaned AND (a.status='ready' OR (a.status IN ('failed','uploading') AND a.updated_at