forked from mtusi2026/mtusi2026lab5
feat(attachments): add pipeline and outbox schema and queries
This commit is contained in:
@@ -4,10 +4,14 @@ INSERT INTO attachments (
|
||||
chat_id,
|
||||
owner_id,
|
||||
s3_key,
|
||||
staging_key,
|
||||
job_id,
|
||||
filename,
|
||||
content_type,
|
||||
size_bytes,
|
||||
sha256,
|
||||
width,
|
||||
height,
|
||||
error_code
|
||||
)
|
||||
SELECT
|
||||
@@ -15,10 +19,14 @@ SELECT
|
||||
sqlc.arg(chat_id)::uuid,
|
||||
sqlc.arg(owner_id)::uuid,
|
||||
sqlc.arg(s3_key)::text,
|
||||
sqlc.narg(staging_key)::text,
|
||||
sqlc.narg(job_id)::uuid,
|
||||
sqlc.arg(filename)::text,
|
||||
sqlc.arg(content_type)::attachment_content_type,
|
||||
sqlc.arg(size_bytes)::int,
|
||||
sqlc.arg(sha256)::text,
|
||||
sqlc.arg(width)::int,
|
||||
sqlc.arg(height)::int,
|
||||
NULLIF(sqlc.arg(error_code)::text, '')
|
||||
WHERE EXISTS (
|
||||
SELECT 1 FROM chat_members
|
||||
@@ -77,6 +85,30 @@ SELECT
|
||||
) AS referenced
|
||||
FROM attachments a;
|
||||
|
||||
-- name: LockAttachmentJob :one
|
||||
SELECT * FROM attachments
|
||||
WHERE id = sqlc.arg(id)::uuid
|
||||
AND status = 'processing'
|
||||
AND job_id = sqlc.arg(job_id)::uuid
|
||||
AND pipeline_version = sqlc.arg(pipeline_version)::int
|
||||
AND attempts = sqlc.arg(attempt)::int
|
||||
AND lease_until > now()
|
||||
FOR UPDATE;
|
||||
|
||||
-- name: LockExpiredAttachmentJobs :many
|
||||
SELECT * FROM attachments
|
||||
WHERE status = 'processing'
|
||||
AND lease_until <= now()
|
||||
ORDER BY lease_until, id
|
||||
LIMIT sqlc.arg(batch_size)::int
|
||||
FOR UPDATE SKIP LOCKED;
|
||||
|
||||
-- name: MarkAttachmentReady :one
|
||||
UPDATE attachments
|
||||
SET status = 'ready', updated_at = now()
|
||||
WHERE id = sqlc.arg(id)::uuid
|
||||
RETURNING *;
|
||||
|
||||
-- name: MarkAttachmentFailedIfReady :execrows
|
||||
UPDATE attachments
|
||||
SET status = 'failed',
|
||||
@@ -84,12 +116,73 @@ SET status = 'failed',
|
||||
updated_at = now()
|
||||
WHERE id = sqlc.arg(id)::uuid AND status = 'ready';
|
||||
|
||||
-- name: MarkAttachmentReady :one
|
||||
-- name: ClaimAttachmentJob :one
|
||||
UPDATE attachments
|
||||
SET status = 'ready', updated_at = now()
|
||||
WHERE id = $1::uuid
|
||||
SET status = 'processing',
|
||||
attempts = attempts + 1,
|
||||
lease_until = sqlc.arg(lease_until)::timestamptz,
|
||||
updated_at = now()
|
||||
WHERE id = sqlc.arg(id)::uuid
|
||||
AND job_id = sqlc.arg(job_id)::uuid
|
||||
AND pipeline_version = sqlc.arg(pipeline_version)::int
|
||||
AND status = 'queued'
|
||||
AND attempts + 1 = sqlc.arg(attempt)::int
|
||||
RETURNING *;
|
||||
|
||||
-- name: CompleteAttachmentJob :execrows
|
||||
UPDATE attachments
|
||||
SET status = 'ready',
|
||||
error_code = NULL,
|
||||
lease_until = NULL,
|
||||
width = sqlc.arg(width)::int,
|
||||
height = sqlc.arg(height)::int,
|
||||
updated_at = now()
|
||||
WHERE id = sqlc.arg(id)::uuid
|
||||
AND status = 'processing'
|
||||
AND attempts = sqlc.arg(attempt)::int
|
||||
AND lease_until > now();
|
||||
|
||||
-- name: RetryAttachmentJob :execrows
|
||||
UPDATE attachments
|
||||
SET status = 'queued',
|
||||
error_code = sqlc.arg(error_code)::text,
|
||||
lease_until = NULL,
|
||||
updated_at = now()
|
||||
WHERE id = sqlc.arg(id)::uuid
|
||||
AND status = 'processing'
|
||||
AND attempts = sqlc.arg(attempt)::int
|
||||
AND lease_until > now();
|
||||
|
||||
-- name: FailAttachmentJob :execrows
|
||||
UPDATE attachments
|
||||
SET status = 'failed',
|
||||
error_code = sqlc.arg(error_code)::text,
|
||||
lease_until = NULL,
|
||||
updated_at = now()
|
||||
WHERE id = sqlc.arg(id)::uuid
|
||||
AND status = 'processing'
|
||||
AND attempts = sqlc.arg(attempt)::int
|
||||
AND lease_until > now();
|
||||
|
||||
-- name: RecoverExpiredAttachmentJob :execrows
|
||||
UPDATE attachments
|
||||
SET status = 'queued',
|
||||
lease_until = NULL,
|
||||
updated_at = now()
|
||||
WHERE id = sqlc.arg(id)::uuid
|
||||
AND status = 'processing'
|
||||
AND lease_until <= now();
|
||||
|
||||
-- name: FailExpiredAttachmentJob :execrows
|
||||
UPDATE attachments
|
||||
SET status = 'failed',
|
||||
error_code = 'retry_exhausted',
|
||||
lease_until = NULL,
|
||||
updated_at = now()
|
||||
WHERE id = sqlc.arg(id)::uuid
|
||||
AND status = 'processing'
|
||||
AND lease_until <= now();
|
||||
|
||||
-- name: DeleteQueuedAttachment :execrows
|
||||
DELETE FROM attachments
|
||||
WHERE id = sqlc.arg(id)::uuid AND status = 'queued';
|
||||
|
||||
Reference in New Issue
Block a user