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:
@@ -13,9 +13,12 @@ from typing import Any
|
||||
|
||||
import httpx
|
||||
|
||||
from contract import assert_ok
|
||||
from contract import assert_accepted, assert_ok
|
||||
|
||||
API = "/api/v1"
|
||||
# срок ожидания готовности вложения — как у публичного smoke, и шаг опроса
|
||||
READY_TIMEOUT_S = 60
|
||||
READY_POLL_S = 0.2
|
||||
SESSIONS = f"{API}/auth/sessions"
|
||||
# защита от бесконечного обхода, если next_cursor никогда не станет null
|
||||
MAX_PAGES = 1000
|
||||
@@ -291,7 +294,7 @@ class Api:
|
||||
content_type: str = "image/png",
|
||||
filename: str = "image.png",
|
||||
) -> dict[str, Any]:
|
||||
"""Успешная загрузка: 201 и `Attachment` с размером и SHA-256."""
|
||||
"""Принятая загрузка: 202 и снимок `queued` с размером и SHA-256."""
|
||||
resp = self.post_attachment(
|
||||
actor,
|
||||
chat_id,
|
||||
@@ -299,7 +302,7 @@ class Api:
|
||||
content_type=content_type,
|
||||
filename=filename,
|
||||
)
|
||||
attachment = assert_ok(resp, 201, "Attachment")
|
||||
attachment = assert_accepted(resp)
|
||||
assert attachment["size_bytes"] == len(data), "size_bytes differs"
|
||||
assert attachment["sha256"] == hashlib.sha256(data).hexdigest(), (
|
||||
"sha256 differs"
|
||||
@@ -309,6 +312,57 @@ class Api:
|
||||
assert attachment["filename"] == filename, "filename changed"
|
||||
return attachment
|
||||
|
||||
def wait_attachment(
|
||||
self,
|
||||
actor: Actor,
|
||||
attachment_id: str,
|
||||
*,
|
||||
status: str = "ready",
|
||||
timeout: float = READY_TIMEOUT_S,
|
||||
) -> dict[str, Any]:
|
||||
"""Метаданные вложения, когда оно в статусе `status`: опрос раз в
|
||||
0,2 с. Истёк срок или вложение стало `failed`, а ждали другое —
|
||||
падение с последним состоянием (спек `student-tests`, «Готовность
|
||||
вложения в тестах»)."""
|
||||
deadline = time.monotonic() + timeout
|
||||
while True:
|
||||
meta = assert_ok(
|
||||
self.attachment_meta(actor, attachment_id),
|
||||
200,
|
||||
"Attachment",
|
||||
)
|
||||
if meta["status"] == status:
|
||||
return meta
|
||||
assert meta["status"] != "failed", (
|
||||
f"attachment {attachment_id} became failed "
|
||||
f"({meta['error_code']}), waited for {status}"
|
||||
)
|
||||
assert time.monotonic() < deadline, (
|
||||
f"attachment {attachment_id} did not become {status} in "
|
||||
f"{timeout} s, last status {meta['status']}: is the worker "
|
||||
"running?"
|
||||
)
|
||||
time.sleep(READY_POLL_S)
|
||||
|
||||
def upload_ready(
|
||||
self,
|
||||
actor: Actor,
|
||||
chat_id: str,
|
||||
data: bytes,
|
||||
*,
|
||||
content_type: str = "image/png",
|
||||
filename: str = "image.png",
|
||||
) -> dict[str, Any]:
|
||||
"""Загрузка и ожидание `ready`: метаданные готового вложения."""
|
||||
accepted = self.upload(
|
||||
actor,
|
||||
chat_id,
|
||||
data,
|
||||
content_type=content_type,
|
||||
filename=filename,
|
||||
)
|
||||
return self.wait_attachment(actor, accepted["id"])
|
||||
|
||||
def attachment_meta(
|
||||
self,
|
||||
actor: Actor | None,
|
||||
|
||||
Reference in New Issue
Block a user