feat: compensate failed uploads and clean stale pending attachments

- upload: delete the stored object when ready fails before COMMIT or the
  pending row is gone (503 instead of 500); unknown COMMIT outcome keeps it
- cleaner service (python -m src.cleaner): stale pending rows older than
  grace (900 s, must exceed 17 s live-upload bound) under FOR UPDATE SKIP
  LOCKED, object before row, own S3 user with read/delete only
- shared session_scope for API and cleaner; early_stop process name
- partial index on pending attachments, CREATE INDEX CONCURRENTLY
- s3-bootstrap: cleaner user, policy and checks; make env_setup appends
  missing template variables to an existing .env
- failure checks: cleaner on the failure stand, id-list hook actions,
  five new and three updated tests; driver upload-consistency
- sync upload-consistency deltas into main specs (storage-consistency)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-21 06:10:37 +03:00
co-authored by Claude Opus 5.5
parent 450b091ae3
commit 5b42bd2891
54 changed files with 2958 additions and 411 deletions
@@ -0,0 +1,52 @@
"""added pending attachments index
Revision ID: 739b8f04d5fb
Revises: ef25d50b01a8
Create Date: 2026-10-21 05:20:02.659615
"""
from collections.abc import Sequence
import sqlalchemy as sa
from alembic import op
# revision identifiers, used by Alembic.
revision: str = "739b8f04d5fb"
down_revision: str | Sequence[str] | None = "ef25d50b01a8"
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None
def upgrade() -> None:
# expand (design change upload-consistency, решение 6): частичный
# индекс кандидатов уборщика. CONCURRENTLY не берёт блокировку,
# запрещающую запись загрузок, но не работает в транзакции — отсюда
# autocommit_block. В нём SET LOCAL не действует: lock_timeout на
# уровне сессии и явный сброс. IF NOT EXISTS — повтор после сбоя
# проходит; INVALID-индекс прерванной сборки он не пересоздаст
# (починка — migrations.md). Autogenerate условие не видит
with op.get_context().autocommit_block():
op.execute("SET lock_timeout = '2s'")
op.create_index(
"ix_attachments_pending_created_at",
"attachments",
["created_at", "id"],
unique=False,
postgresql_where=sa.text("status = 'pending'"),
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_pending_created_at",
table_name="attachments",
postgresql_concurrently=True,
if_exists=True,
)
op.execute("RESET lock_timeout")