feat: add job outbox, relay, leases and image variants to the pipeline

Move publishing out of the upload path: a relay process publishes queued
jobs with confirms. Add lease and attempt fencing, backoff with jitter, a
per-job render process with CPU and memory limits, crop and optimized
variants, the failed registry with audited replay, and staging cleanup.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-31 15:36:15 +03:00
co-authored by Claude Opus 5.5
parent 99a222eee2
commit e2a6427c24
102 changed files with 9499 additions and 1916 deletions
@@ -0,0 +1,86 @@
"""added job replays and attempt base
Revision ID: c7e2f1a04b56
Revises: a1d4c7e90b23
Create Date: 2026-10-31 18:00:00.000000
"""
from collections.abc import Sequence
import sqlalchemy as sa
from alembic import op
# revision identifiers, used by Alembic.
revision: str = "c7e2f1a04b56"
down_revision: str | Sequence[str] | None = "a1d4c7e90b23"
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None
def upgrade() -> None:
# expand (design change pipeline-reliability, решение 10): прежняя версия
# новую колонку не пишет, значение даёт server_default. ACCESS EXCLUSIVE
# на attachment_jobs — lock_timeout (migrations.md). CHECK NOT VALID:
# у прежних строк attempt_base = 0, просмотр таблицы под блокировкой не
# нужен, а новые строки условие проверяет сразу
op.execute("SET LOCAL lock_timeout = '2s'")
op.add_column(
"attachment_jobs",
sa.Column(
"attempt_base",
sa.Integer(),
server_default=sa.text("0"),
nullable=False,
),
)
op.execute(
"ALTER TABLE attachment_jobs ADD CONSTRAINT "
"ck_attachment_jobs_attempt_base "
"CHECK (attempt_base BETWEEN 0 AND attempt) NOT VALID",
)
op.create_table(
"attachment_job_replays",
sa.Column("id", sa.Uuid(), nullable=False),
sa.Column("job_id", sa.Uuid(), nullable=False),
sa.Column("operator", sa.Text(), nullable=False),
sa.Column("error_code", sa.Text(), nullable=True),
sa.Column("reason", sa.Text(), nullable=True),
sa.Column("attempt", sa.Integer(), nullable=False),
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
sa.CheckConstraint(
"char_length(operator) BETWEEN 1 AND 64",
name=op.f("ck_attachment_job_replays_operator_length"),
),
sa.CheckConstraint(
"attempt >= 0",
name=op.f("ck_attachment_job_replays_attempt"),
),
sa.ForeignKeyConstraint(
["job_id"],
["attachment_jobs.id"],
name=op.f("fk_attachment_job_replays_job_id_attachment_jobs"),
),
sa.PrimaryKeyConstraint("id", name=op.f("pk_attachment_job_replays")),
)
op.create_index(
op.f("ix_attachment_job_replays_job_id"),
"attachment_job_replays",
["job_id"],
unique=False,
)
def downgrade() -> None:
op.execute("SET LOCAL lock_timeout = '2s'")
op.drop_index(
op.f("ix_attachment_job_replays_job_id"),
table_name="attachment_job_replays",
)
op.drop_table("attachment_job_replays")
op.drop_constraint(
op.f("ck_attachment_jobs_attempt_base"),
"attachment_jobs",
type_="check",
)
op.drop_column("attachment_jobs", "attempt_base")
@@ -0,0 +1,98 @@
"""added job outbox indexes
Revision ID: d3f9a6b2c1e7
Revises: c7e2f1a04b56
Create Date: 2026-10-31 18:01:00.000000
"""
from collections.abc import Sequence
import sqlalchemy as sa
from alembic import op
# revision identifiers, used by Alembic.
revision: str = "d3f9a6b2c1e7"
down_revision: str | Sequence[str] | None = "c7e2f1a04b56"
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None
def upgrade() -> None:
# expand (design change pipeline-reliability, решение 10): индексы
# relay и очистки staging. Отдельной ревизией: CONCURRENTLY не живёт в
# транзакции (autocommit_block, migrations.md), а ревизия с колонкой и
# таблицей атомарна. Autogenerate условие и выражение не видит
with op.get_context().autocommit_block():
op.execute("SET lock_timeout = '2s'")
op.create_index(
"ix_attachment_jobs_outbox",
"attachment_jobs",
[sa.text("COALESCE(next_attempt_at, created_at)"), "id"],
unique=False,
postgresql_where=sa.text(
"state = 'queued' AND published_at IS NULL",
),
postgresql_concurrently=True,
if_not_exists=True,
)
op.create_index(
"ix_attachment_jobs_published",
"attachment_jobs",
["published_at"],
unique=False,
postgresql_where=sa.text(
"state = 'queued' AND published_at IS NOT NULL",
),
postgresql_concurrently=True,
if_not_exists=True,
)
op.create_index(
"ix_attachment_jobs_lease",
"attachment_jobs",
["lease_expires_at"],
unique=False,
postgresql_where=sa.text("state = 'processing'"),
postgresql_concurrently=True,
if_not_exists=True,
)
op.create_index(
"ix_attachments_staging_cleanup",
"attachments",
["status", "updated_at", "id"],
unique=False,
postgresql_where=sa.text("staging_key IS NOT NULL"),
postgresql_concurrently=True,
if_not_exists=True,
)
op.execute("RESET lock_timeout")
def downgrade() -> None:
with op.get_context().autocommit_block():
op.execute("SET lock_timeout = '2s'")
op.drop_index(
"ix_attachments_staging_cleanup",
table_name="attachments",
postgresql_concurrently=True,
if_exists=True,
)
op.drop_index(
"ix_attachment_jobs_lease",
table_name="attachment_jobs",
postgresql_concurrently=True,
if_exists=True,
)
op.drop_index(
"ix_attachment_jobs_published",
table_name="attachment_jobs",
postgresql_concurrently=True,
if_exists=True,
)
op.drop_index(
"ix_attachment_jobs_outbox",
table_name="attachment_jobs",
postgresql_concurrently=True,
if_exists=True,
)
op.execute("RESET lock_timeout")