feat: process attachments asynchronously with RabbitMQ and a worker
Upload answers 202 after staging, a queued job in PostgreSQL and a broker confirm; a separate worker publishes the original and a thumbnail with bounded retries. Adds the job and variant tables, the broker service, the worker S3 user, cleanup and reconcile support for staging and variants, the failure stand with broker and worker, bench and driver updates. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+187
@@ -0,0 +1,187 @@
|
||||
"""added attachment jobs and variants
|
||||
|
||||
Revision ID: a1d4c7e90b23
|
||||
Revises: 5c3e1f0a9b7d
|
||||
Create Date: 2026-10-31 12:00:00.000000
|
||||
|
||||
"""
|
||||
|
||||
from collections.abc import Sequence
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision: str = "a1d4c7e90b23"
|
||||
down_revision: str | Sequence[str] | None = "5c3e1f0a9b7d"
|
||||
branch_labels: str | Sequence[str] | None = None
|
||||
depends_on: str | Sequence[str] | None = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
# expand (design change async-attachments, решение 1): колонки NULL без
|
||||
# значения по умолчанию и новые таблицы; прежняя версия новых колонок не
|
||||
# читает и не пишет. ACCESS EXCLUSIVE на attachments — lock_timeout,
|
||||
# чтобы чужая долгая транзакция не остановила загрузки всех реплик в
|
||||
# очереди за миграцией (migrations.md)
|
||||
op.execute("SET LOCAL lock_timeout = '2s'")
|
||||
op.add_column(
|
||||
"attachments",
|
||||
sa.Column("staging_key", sa.Text(), nullable=True),
|
||||
)
|
||||
op.add_column(
|
||||
"attachments",
|
||||
sa.Column("error_code", sa.Text(), nullable=True),
|
||||
)
|
||||
# ослабление статуса: прежние строки строже нового условия, поэтому
|
||||
# NOT VALID в той же транзакции — VALIDATE держал бы блокировку на время
|
||||
# просмотра таблицы, как ck_messages_text_length ревизии ef25d50b01a8.
|
||||
# op.f: имя готовое, шаблон имён к нему не применяется
|
||||
op.drop_constraint(
|
||||
op.f("ck_attachments_status"),
|
||||
"attachments",
|
||||
type_="check",
|
||||
)
|
||||
op.execute(
|
||||
"ALTER TABLE attachments ADD CONSTRAINT ck_attachments_status "
|
||||
"CHECK (status IN "
|
||||
"('pending', 'queued', 'processing', 'ready', 'failed')) NOT VALID",
|
||||
)
|
||||
op.execute(
|
||||
"ALTER TABLE attachments ADD CONSTRAINT "
|
||||
"ck_attachments_error_code_failed "
|
||||
"CHECK ((status = 'failed') = (error_code IS NOT NULL)) NOT VALID",
|
||||
)
|
||||
op.create_table(
|
||||
"attachment_jobs",
|
||||
sa.Column("id", sa.Uuid(), nullable=False),
|
||||
sa.Column("attachment_id", sa.Uuid(), nullable=False),
|
||||
sa.Column("pipeline_version", sa.Integer(), nullable=False),
|
||||
sa.Column("state", sa.Text(), nullable=False),
|
||||
sa.Column(
|
||||
"attempt",
|
||||
sa.Integer(),
|
||||
server_default=sa.text("0"),
|
||||
nullable=False,
|
||||
),
|
||||
sa.Column("last_error_code", sa.Text(), nullable=True),
|
||||
sa.Column(
|
||||
"next_attempt_at",
|
||||
sa.DateTime(timezone=True),
|
||||
nullable=True,
|
||||
),
|
||||
sa.Column(
|
||||
"lease_expires_at",
|
||||
sa.DateTime(timezone=True),
|
||||
nullable=True,
|
||||
),
|
||||
sa.Column(
|
||||
"published_at",
|
||||
sa.DateTime(timezone=True),
|
||||
nullable=True,
|
||||
),
|
||||
sa.Column("updated_at", sa.DateTime(timezone=True), nullable=False),
|
||||
sa.Column("finished_at", sa.DateTime(timezone=True), nullable=True),
|
||||
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
|
||||
sa.CheckConstraint(
|
||||
"pipeline_version >= 1",
|
||||
name=op.f("ck_attachment_jobs_pipeline_version"),
|
||||
),
|
||||
sa.CheckConstraint(
|
||||
"state IN ('queued', 'processing', 'succeeded', 'failed')",
|
||||
name=op.f("ck_attachment_jobs_state"),
|
||||
),
|
||||
sa.CheckConstraint(
|
||||
"attempt >= 0",
|
||||
name=op.f("ck_attachment_jobs_attempt"),
|
||||
),
|
||||
sa.ForeignKeyConstraint(
|
||||
["attachment_id"],
|
||||
["attachments.id"],
|
||||
name=op.f("fk_attachment_jobs_attachment_id_attachments"),
|
||||
),
|
||||
sa.PrimaryKeyConstraint("id", name=op.f("pk_attachment_jobs")),
|
||||
sa.UniqueConstraint(
|
||||
"attachment_id",
|
||||
"pipeline_version",
|
||||
name=op.f("uq_attachment_jobs_attachment_id_pipeline_version"),
|
||||
),
|
||||
)
|
||||
op.create_table(
|
||||
"attachment_variants",
|
||||
sa.Column("attachment_id", sa.Uuid(), nullable=False),
|
||||
sa.Column("pipeline_version", sa.Integer(), nullable=False),
|
||||
sa.Column("variant", sa.Text(), nullable=False),
|
||||
sa.Column("object_key", sa.Text(), nullable=False),
|
||||
sa.Column("content_type", sa.Text(), nullable=False),
|
||||
sa.Column("size_bytes", sa.Integer(), nullable=False),
|
||||
sa.Column("width", sa.Integer(), nullable=False),
|
||||
sa.Column("height", sa.Integer(), nullable=False),
|
||||
sa.Column("sha256", sa.LargeBinary(), nullable=False),
|
||||
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
|
||||
sa.CheckConstraint(
|
||||
"variant IN ('thumbnail', 'crop', 'optimized')",
|
||||
name=op.f("ck_attachment_variants_variant"),
|
||||
),
|
||||
sa.CheckConstraint(
|
||||
"octet_length(object_key) BETWEEN 1 AND 1024",
|
||||
name=op.f("ck_attachment_variants_object_key_length"),
|
||||
),
|
||||
sa.CheckConstraint(
|
||||
"content_type IN ('image/jpeg', 'image/png', 'image/webp')",
|
||||
name=op.f("ck_attachment_variants_content_type"),
|
||||
),
|
||||
sa.CheckConstraint(
|
||||
"size_bytes > 0",
|
||||
name=op.f("ck_attachment_variants_size_bytes"),
|
||||
),
|
||||
sa.CheckConstraint(
|
||||
"width > 0",
|
||||
name=op.f("ck_attachment_variants_width"),
|
||||
),
|
||||
sa.CheckConstraint(
|
||||
"height > 0",
|
||||
name=op.f("ck_attachment_variants_height"),
|
||||
),
|
||||
sa.CheckConstraint(
|
||||
"octet_length(sha256) = 32",
|
||||
name=op.f("ck_attachment_variants_sha256_length"),
|
||||
),
|
||||
sa.ForeignKeyConstraint(
|
||||
["attachment_id"],
|
||||
["attachments.id"],
|
||||
name=op.f("fk_attachment_variants_attachment_id_attachments"),
|
||||
),
|
||||
sa.PrimaryKeyConstraint(
|
||||
"attachment_id",
|
||||
"pipeline_version",
|
||||
"variant",
|
||||
name=op.f("pk_attachment_variants"),
|
||||
),
|
||||
sa.UniqueConstraint(
|
||||
"object_key",
|
||||
name=op.f("uq_attachment_variants_object_key"),
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
op.execute("SET LOCAL lock_timeout = '2s'")
|
||||
op.drop_table("attachment_variants")
|
||||
op.drop_table("attachment_jobs")
|
||||
op.drop_constraint(
|
||||
op.f("ck_attachments_error_code_failed"),
|
||||
"attachments",
|
||||
type_="check",
|
||||
)
|
||||
op.drop_constraint(
|
||||
op.f("ck_attachments_status"),
|
||||
"attachments",
|
||||
type_="check",
|
||||
)
|
||||
op.execute(
|
||||
"ALTER TABLE attachments ADD CONSTRAINT ck_attachments_status "
|
||||
"CHECK (status IN ('pending', 'ready')) NOT VALID",
|
||||
)
|
||||
op.drop_column("attachments", "error_code")
|
||||
op.drop_column("attachments", "staging_key")
|
||||
Reference in New Issue
Block a user