ЛР 5 — БВТ2301 — Курлова — на 5
RabbitMQ + transactional outbox, отдельный worker (thumbnail/crop/WebP), lease и heartbeat, retry с backoff+jitter, DLQ и replay, admission control; тесты отказов, трек 1 и отчёт.
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
-- lab5: upload -> staging -> job/outbox -> broker -> worker -> final prefix + variants.
|
||||
ALTER TABLE attachments DROP CONSTRAINT attachments_status;
|
||||
ALTER TABLE attachments ADD CONSTRAINT attachments_status
|
||||
CHECK (status IN ('uploading', 'queued', 'processing', 'ready', 'failed'));
|
||||
-- staging_key: private S3 prefix written by the API before 202; object_key: final original written by the worker.
|
||||
ALTER TABLE attachments ADD COLUMN staging_key text;
|
||||
ALTER TABLE attachments ADD COLUMN pipeline_version integer NOT NULL DEFAULT 1;
|
||||
CREATE UNIQUE INDEX attachments_staging_key_idx ON attachments (staging_key) WHERE staging_key IS NOT NULL;
|
||||
|
||||
-- Effect key of the pipeline: (attachment_id, pipeline_version, name). A repeated job cannot add a second set.
|
||||
CREATE TABLE attachment_variants (
|
||||
attachment_id uuid NOT NULL REFERENCES attachments (id) ON DELETE CASCADE,
|
||||
pipeline_version integer NOT NULL,
|
||||
name text NOT NULL CONSTRAINT variants_name CHECK (name IN ('thumbnail', 'crop', 'optimized')),
|
||||
object_key text NOT NULL UNIQUE,
|
||||
content_type text NOT NULL CONSTRAINT variants_ct CHECK (content_type IN ('image/jpeg', 'image/png', 'image/webp')),
|
||||
size_bytes integer NOT NULL CONSTRAINT variants_size CHECK (size_bytes > 0),
|
||||
width integer NOT NULL CONSTRAINT variants_width CHECK (width > 0),
|
||||
height integer NOT NULL CONSTRAINT variants_height CHECK (height > 0),
|
||||
sha256 text NOT NULL CONSTRAINT variants_sha CHECK (sha256 ~ '^[a-f0-9]{64}$'),
|
||||
created_at timestamptz NOT NULL,
|
||||
PRIMARY KEY (attachment_id, pipeline_version, name)
|
||||
);
|
||||
|
||||
-- One logical job per (attachment, pipeline_version); job_id never changes on redelivery or retry.
|
||||
CREATE TABLE attachment_jobs (
|
||||
id uuid PRIMARY KEY,
|
||||
attachment_id uuid NOT NULL REFERENCES attachments (id) ON DELETE CASCADE,
|
||||
pipeline_version integer NOT NULL,
|
||||
state text NOT NULL
|
||||
CONSTRAINT jobs_state CHECK (state IN ('queued', 'processing', 'succeeded', 'failed')),
|
||||
attempt integer NOT NULL DEFAULT 1 CONSTRAINT jobs_attempt CHECK (attempt >= 1),
|
||||
max_attempts integer NOT NULL CONSTRAINT jobs_max_attempts CHECK (max_attempts >= 1),
|
||||
next_attempt_at timestamptz NOT NULL,
|
||||
lease_owner text,
|
||||
lease_until timestamptz,
|
||||
error_code text,
|
||||
last_error text,
|
||||
created_at timestamptz NOT NULL,
|
||||
first_started_at timestamptz,
|
||||
started_at timestamptz,
|
||||
finished_at timestamptz,
|
||||
updated_at timestamptz NOT NULL,
|
||||
UNIQUE (attachment_id, pipeline_version)
|
||||
);
|
||||
CREATE INDEX attachment_jobs_lease_idx ON attachment_jobs (lease_until) WHERE state = 'processing';
|
||||
CREATE INDEX attachment_jobs_failed_idx ON attachment_jobs (updated_at) WHERE state = 'failed';
|
||||
CREATE INDEX attachment_jobs_pending_idx ON attachment_jobs (state) WHERE state IN ('queued', 'processing');
|
||||
|
||||
-- Transactional outbox: written in the same transaction as the job; the relay publishes with confirms.
|
||||
CREATE TABLE outbox (
|
||||
id bigserial PRIMARY KEY,
|
||||
job_id uuid NOT NULL REFERENCES attachment_jobs (id) ON DELETE CASCADE,
|
||||
payload jsonb NOT NULL,
|
||||
available_at timestamptz NOT NULL,
|
||||
published_at timestamptz,
|
||||
publish_attempts integer NOT NULL DEFAULT 0,
|
||||
last_error text,
|
||||
created_at timestamptz NOT NULL
|
||||
);
|
||||
CREATE INDEX outbox_pending_idx ON outbox (available_at, id) WHERE published_at IS NULL;
|
||||
CREATE INDEX outbox_published_idx ON outbox (published_at) WHERE published_at IS NOT NULL;
|
||||
|
||||
-- Operator-visible history of every state change of a job (including explicit replay).
|
||||
CREATE TABLE job_audit (
|
||||
id bigserial PRIMARY KEY,
|
||||
job_id uuid NOT NULL REFERENCES attachment_jobs (id) ON DELETE CASCADE,
|
||||
attachment_id uuid NOT NULL,
|
||||
action text NOT NULL,
|
||||
actor text NOT NULL,
|
||||
attempt integer NOT NULL,
|
||||
detail jsonb NOT NULL DEFAULT '{}'::jsonb,
|
||||
created_at timestamptz NOT NULL
|
||||
);
|
||||
CREATE INDEX job_audit_job_idx ON job_audit (job_id, id);
|
||||
Reference in New Issue
Block a user