feat(worker): prepare replay queries and audit schema

This commit is contained in:
kk10
2026-11-16 16:18:54 +03:00
parent 97b43a9c98
commit f2000a7883
7 changed files with 239 additions and 10 deletions
@@ -6,6 +6,7 @@ ALTER TABLE attachments
ADD COLUMN job_id uuid UNIQUE,
ADD COLUMN pipeline_version int NOT NULL DEFAULT 1 CHECK (pipeline_version > 0),
ADD COLUMN attempts int NOT NULL DEFAULT 0 CHECK (attempts >= 0),
ADD COLUMN retry_base int NOT NULL DEFAULT 0 CHECK (retry_base >= 0 AND retry_base <= attempts),
ADD COLUMN width int CHECK (width > 0),
ADD COLUMN height int CHECK (height > 0),
ADD COLUMN lease_until timestamptz;
@@ -39,7 +40,18 @@ CREATE TABLE outbox (
CREATE INDEX idx_outbox_pending ON outbox (available_at, id) WHERE published_at IS NULL;
CREATE TABLE attachment_replays (
attachment_id uuid NOT NULL REFERENCES attachments (id) ON DELETE CASCADE,
attempt int NOT NULL CHECK (attempt > 0),
previous_error_code text,
operator text NOT NULL,
reason text NOT NULL,
created_at timestamptz NOT NULL DEFAULT now(),
PRIMARY KEY (attachment_id, attempt)
);
-- +goose Down
DROP TABLE attachment_replays;
DROP TABLE outbox;
DROP TABLE attachment_variants;
DROP TYPE attachment_variant_name;
@@ -48,6 +60,7 @@ ALTER TABLE attachments
DROP COLUMN height,
DROP COLUMN width,
DROP COLUMN lease_until,
DROP COLUMN retry_base,
DROP COLUMN attempts,
DROP COLUMN pipeline_version,
DROP COLUMN job_id,
+14
View File
@@ -0,0 +1,14 @@
-- name: CreateAttachmentReplay :exec
INSERT INTO attachment_replays (
attachment_id,
attempt,
previous_error_code,
operator,
reason
) VALUES (
sqlc.arg(attachment_id),
sqlc.arg(attempt),
sqlc.narg(previous_error_code),
sqlc.arg(operator),
sqlc.arg(reason)
);
+33
View File
@@ -77,6 +77,13 @@ ORDER BY message_id, position;
SELECT id, s3_key, staging_key, status, size_bytes, updated_at
FROM attachments;
-- name: ListFailedAttachments :many
SELECT id, job_id, error_code, attempts, updated_at
FROM attachments
WHERE status = 'failed'
ORDER BY updated_at, id
LIMIT sqlc.arg(batch_size)::int;
-- name: LockAttachmentJob :one
SELECT * FROM attachments
WHERE id = sqlc.arg(id)::uuid
@@ -95,6 +102,17 @@ ORDER BY lease_until, id
LIMIT sqlc.arg(batch_size)::int
FOR UPDATE SKIP LOCKED;
-- name: LockFailedAttachmentForReplay :one
SELECT * FROM attachments
WHERE id = sqlc.arg(id)::uuid
AND status = 'failed'
AND staging_key IS NOT NULL
AND NOT EXISTS (
SELECT 1 FROM attachment_variants
WHERE attachment_id = attachments.id
)
FOR UPDATE;
-- name: ClearAttachmentStagingKey :exec
UPDATE attachments
SET staging_key = NULL
@@ -142,6 +160,21 @@ WHERE id = sqlc.arg(id)::uuid
AND attempts = sqlc.arg(attempt)::int
AND lease_until > now();
-- name: ReplayAttachmentJob :execrows
UPDATE attachments
SET status = 'queued',
retry_base = attempts,
error_code = NULL,
lease_until = NULL,
updated_at = now()
WHERE id = sqlc.arg(id)::uuid
AND status = 'failed'
AND staging_key IS NOT NULL
AND NOT EXISTS (
SELECT 1 FROM attachment_variants
WHERE attachment_id = attachments.id
);
-- name: FailAttachmentJob :execrows
UPDATE attachments
SET status = 'failed',