Отделить обработку вложений от приёма файла.

Upload отвечает 202 после staging и записи задания, worker через RabbitMQ собирает thumbnail, crop и optimized и переживает разрыв до publish.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-11-09 01:08:24 +03:00
co-authored by Cursor
parent 9046d5206d
commit f2a3698480
24 changed files with 1579 additions and 82 deletions
+95 -9
View File
@@ -8,7 +8,7 @@ import logging
from datetime import datetime, timezone
from uuid import UUID, uuid4
from app import config, db, s3
from app import config, db, jobs, s3
from app.errors import AppError
from app.store import format_ts
@@ -16,7 +16,8 @@ logger = logging.getLogger("messenger.attachments")
_sweep_task: asyncio.Task | None = None
def _public(row) -> dict:
def _public(row, variants=None) -> dict:
error_code = row["error_code"] if "error_code" in row.keys() else None
return {
"id": str(row["id"]),
"chat_id": str(row["chat_id"]),
@@ -26,13 +27,47 @@ def _public(row) -> dict:
"size_bytes": row["size_bytes"],
"sha256": row["sha256"],
"status": row["status"],
"variants": [],
"error_code": None,
"variants": variants or [],
"error_code": error_code if row["status"] == "failed" else None,
"created_at": format_ts(row["created_at"]),
"updated_at": format_ts(row["updated_at"]),
}
async def _variants(attachment_id) -> list:
async with db.require().acquire() as conn:
rows = await conn.fetch(
"""
SELECT name, content_type, size_bytes, width, height, sha256
FROM attachment_variants
WHERE attachment_id = $1 AND pipeline_version = 1
ORDER BY CASE name
WHEN 'thumbnail' THEN 1
WHEN 'crop' THEN 2
WHEN 'optimized' THEN 3
ELSE 4
END
""",
attachment_id,
)
return [
{
"name": row["name"],
"content_type": row["content_type"],
"size_bytes": row["size_bytes"],
"width": row["width"],
"height": row["height"],
"sha256": row["sha256"],
}
for row in rows
]
async def present(row) -> dict:
variants = await _variants(row["id"]) if row["status"] == "ready" else []
return _public(row, variants)
async def _drop_intent(key: str) -> None:
async with db.require().acquire() as conn:
await conn.execute(
@@ -61,7 +96,7 @@ async def create_attachment(
data: bytes,
) -> dict:
attachment_id = uuid4()
key = f"originals/{attachment_id}"
key = f"staging/{attachment_id}"
created = datetime.now(timezone.utc)
digest = hashlib.sha256(data).hexdigest()
async with db.require().acquire() as conn:
@@ -93,7 +128,7 @@ async def create_attachment(
id, chat_id, owner_id, filename, content_type, size_bytes,
sha256, object_key, status, error_code, created_at, updated_at
)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, 'ready', NULL, $9, $9)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, 'queued', NULL, $9, $9)
""",
attachment_id,
UUID(chat_id),
@@ -105,6 +140,7 @@ async def create_attachment(
key,
created,
)
await jobs.insert_job(conn, attachment_id, created)
await conn.execute(
"UPDATE blob_writes SET committed = TRUE WHERE object_key = $1",
key,
@@ -115,6 +151,7 @@ async def create_attachment(
except Exception:
await _compensate(key)
raise AppError(503, "service unavailable") from None
jobs.kick_publish()
return _public(
{
"id": attachment_id,
@@ -124,7 +161,8 @@ async def create_attachment(
"content_type": content_type,
"size_bytes": len(data),
"sha256": digest,
"status": "ready",
"status": "queued",
"error_code": None,
"created_at": created,
"updated_at": created,
}
@@ -136,7 +174,8 @@ async def get_for_member(attachment_id: str, user_id: str):
row = await conn.fetchrow(
"""
SELECT a.id, a.chat_id, a.owner_id, a.filename, a.content_type,
a.size_bytes, a.sha256, a.object_key, a.status, a.created_at, a.updated_at
a.size_bytes, a.sha256, a.object_key, a.status, a.error_code,
a.created_at, a.updated_at
FROM attachments a
JOIN chat_members m ON m.chat_id = a.chat_id AND m.user_id = $2
WHERE a.id = $1
@@ -167,9 +206,31 @@ async def sweep_orphans() -> int:
for row in rows:
key = row["object_key"]
linked = await conn.fetchval(
"SELECT 1 FROM attachments WHERE object_key = $1",
"""
SELECT 1 FROM attachments WHERE object_key = $1
UNION ALL
SELECT 1 FROM attachment_variants WHERE object_key = $1
LIMIT 1
""",
key,
)
busy = False
parts = key.split("/")
if len(parts) >= 2:
try:
owner = UUID(parts[1])
except ValueError:
owner = None
if owner is not None:
busy = await conn.fetchval(
"""
SELECT 1 FROM attachment_jobs
WHERE attachment_id = $1 AND status IN ('queued', 'processing')
""",
owner,
)
if busy:
continue
if linked:
await conn.execute(
"UPDATE blob_writes SET committed = TRUE WHERE object_key = $1",
@@ -192,10 +253,35 @@ async def sweep_orphans() -> int:
return deleted
async def sweep_failed_staging() -> int:
"""Drop staging bytes of failed jobs after the retention window. Metadata stays."""
deleted = 0
async with db.require().acquire() as conn:
rows = await conn.fetch(
"""
SELECT id, object_key
FROM attachments
WHERE status = 'failed'
AND object_key LIKE 'staging/%'
AND updated_at < now() - ($1::int * interval '1 second')
ORDER BY updated_at
LIMIT 20
""",
config.STAGING_RETENTION_SECONDS,
)
for row in rows:
await s3.delete_key(row["object_key"])
async with db.require().acquire() as conn:
await conn.execute("DELETE FROM blob_writes WHERE object_key = $1", row["object_key"])
deleted += 1
return deleted
async def _sweep_loop() -> None:
while True:
try:
await sweep_orphans()
await sweep_failed_staging()
except asyncio.CancelledError:
raise
except Exception:
+124
View File
@@ -0,0 +1,124 @@
"""Durable RabbitMQ topology. Messages contain job ids, never file bytes."""
from __future__ import annotations
import asyncio
import json
import logging
import aio_pika
from app import config
logger = logging.getLogger("messenger.broker")
EXCHANGE = "attachment.jobs"
QUEUE = "attachment.jobs"
RETRY_QUEUE = "attachment.jobs.retry"
DLQ = "attachment.jobs.dlq"
ROUTING_KEY = "process"
_lock = asyncio.Lock()
_connection: aio_pika.RobustConnection | None = None
_channel: aio_pika.RobustChannel | None = None
_exchange: aio_pika.Exchange | None = None
async def _ensure() -> tuple[aio_pika.RobustChannel, aio_pika.Exchange]:
global _connection, _channel, _exchange
async with _lock:
if _channel is not None and not _channel.is_closed:
assert _exchange is not None
return _channel, _exchange
if not config.BROKER_URL:
raise RuntimeError("BROKER_URL is not set")
_connection = await aio_pika.connect_robust(config.BROKER_URL)
_channel = await _connection.channel(publisher_confirms=True)
_exchange = await _channel.declare_exchange(EXCHANGE, aio_pika.ExchangeType.DIRECT, durable=True)
await _channel.declare_queue(
RETRY_QUEUE,
durable=True,
arguments={
"x-dead-letter-exchange": EXCHANGE,
"x-dead-letter-routing-key": ROUTING_KEY,
},
)
await _channel.declare_queue(DLQ, durable=True)
queue = await _channel.declare_queue(QUEUE, durable=True)
await queue.bind(_exchange, routing_key=ROUTING_KEY)
return _channel, _exchange
async def publish_job(payload: dict) -> None:
channel, exchange = await _ensure()
body = json.dumps(payload, separators=(",", ":")).encode("utf-8")
await exchange.publish(
aio_pika.Message(
body=body,
content_type="application/json",
delivery_mode=aio_pika.DeliveryMode.PERSISTENT,
message_id=payload["job_id"],
),
routing_key=ROUTING_KEY,
mandatory=True,
)
async def publish_delay(payload: dict, delay_seconds: float) -> None:
channel, _exchange = await _ensure()
body = json.dumps(payload, separators=(",", ":")).encode("utf-8")
milliseconds = max(1, int(delay_seconds * 1000))
await channel.default_exchange.publish(
aio_pika.Message(
body=body,
content_type="application/json",
delivery_mode=aio_pika.DeliveryMode.PERSISTENT,
expiration=str(milliseconds),
message_id=payload["job_id"],
),
routing_key=RETRY_QUEUE,
mandatory=True,
)
async def publish_dlq(payload: dict) -> None:
channel, _exchange = await _ensure()
body = json.dumps(payload, separators=(",", ":")).encode("utf-8")
await channel.default_exchange.publish(
aio_pika.Message(
body=body,
content_type="application/json",
delivery_mode=aio_pika.DeliveryMode.PERSISTENT,
message_id=payload["job_id"],
),
routing_key=DLQ,
mandatory=True,
)
async def consume(handler, prefetch: int):
channel, _exchange = await _ensure()
await channel.set_qos(prefetch_count=max(1, prefetch))
queue = await channel.declare_queue(QUEUE, durable=True)
tag = await queue.consume(handler)
return queue, tag
async def close() -> None:
global _connection, _channel, _exchange
async with _lock:
if _connection is not None:
await _connection.close()
_connection = None
_channel = None
_exchange = None
def payload_from(raw: bytes) -> dict:
parsed = json.loads(raw.decode("utf-8"))
required = ("schema_version", "job_id", "type", "attachment_id", "pipeline_version", "attempt", "created_at")
if not isinstance(parsed, dict) or any(key not in parsed for key in required):
raise ValueError("job payload is incomplete")
if parsed["schema_version"] != 1 or parsed["type"] != "attachment.process.v1":
raise ValueError("job payload has the wrong type")
return parsed
+9
View File
@@ -47,6 +47,15 @@ MAX_UPLOAD_BYTES = min(int(_env("MAX_UPLOAD_BYTES", "10485760")), 10_485_760)
MAX_IMAGE_PIXELS = min(int(_env("MAX_IMAGE_PIXELS", "40000000")), 40_000_000)
ORPHAN_GRACE_SECONDS = int(_env("ORPHAN_GRACE_SECONDS", "120"))
S3_SWEEP_INTERVAL_SECONDS = int(_env("S3_SWEEP_INTERVAL_SECONDS", "30"))
BROKER_URL = _file_or_env("BROKER_URL")
WORKER_CONCURRENCY = max(1, int(_env("WORKER_CONCURRENCY", "2")))
JOB_MAX_ATTEMPTS = max(1, int(_env("JOB_MAX_ATTEMPTS", "5")))
JOB_TIMEOUT_SECONDS = max(1, int(_env("JOB_TIMEOUT_SECONDS", "60")))
JOB_LEASE_SECONDS = JOB_TIMEOUT_SECONDS + 15
JOB_BACKOFF_BASE_SECONDS = float(_env("JOB_BACKOFF_BASE_SECONDS", "0.5"))
JOB_BACKOFF_CAP_SECONDS = float(_env("JOB_BACKOFF_CAP_SECONDS", "30"))
STAGING_RETENTION_SECONDS = int(_env("STAGING_RETENTION_SECONDS", "86400"))
WORKER_CRASH_POINT = _env("WORKER_CRASH_POINT", "")
def lab_mode() -> bool:
+6 -2
View File
@@ -9,6 +9,7 @@ from uuid import UUID, uuid4
from app import config, db, images, s3
from app.attachments import _compensate, _public
from app import jobs
from app.errors import AppError
from app.store import format_ts
@@ -159,7 +160,7 @@ async def complete_upload(upload_id: str, owner_id: str, claimed_sha: str) -> di
id, chat_id, owner_id, filename, content_type, size_bytes,
sha256, object_key, status, error_code, created_at, updated_at
)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, 'ready', NULL, $9, $9)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, 'queued', NULL, $9, $9)
""",
row["id"],
row["chat_id"],
@@ -171,6 +172,7 @@ async def complete_upload(upload_id: str, owner_id: str, claimed_sha: str) -> di
row["object_key"],
created,
)
await jobs.insert_job(conn, row["id"], created)
await conn.execute(
"UPDATE blob_writes SET committed = TRUE WHERE object_key = $1",
row["object_key"],
@@ -183,6 +185,7 @@ async def complete_upload(upload_id: str, owner_id: str, claimed_sha: str) -> di
raise
except Exception:
raise AppError(503, "service unavailable") from None
jobs.kick_publish()
return _public(
{
"id": row["id"],
@@ -192,7 +195,8 @@ async def complete_upload(upload_id: str, owner_id: str, claimed_sha: str) -> di
"content_type": row["content_type"],
"size_bytes": len(data),
"sha256": digest,
"status": "ready",
"status": "queued",
"error_code": None,
"created_at": created,
"updated_at": created,
}
+619
View File
@@ -0,0 +1,619 @@
"""Job rows are the transactional outbox. The broker never stores file bytes."""
from __future__ import annotations
import asyncio
import hashlib
import logging
import os
import random
from datetime import datetime, timezone
from uuid import UUID, uuid4
from app import broker, config, db, pipeline, s3
from app.errors import AppError
from app.pipeline import PermanentJobError, TransientJobError
from app.store import format_ts
logger = logging.getLogger("messenger.jobs")
_outbox_task: asyncio.Task | None = None
_publish_lock = asyncio.Lock()
def message_of(row) -> dict:
return {
"schema_version": 1,
"job_id": str(row["job_id"]),
"type": "attachment.process.v1",
"attachment_id": str(row["attachment_id"]),
"pipeline_version": int(row["pipeline_version"]),
"attempt": int(row["attempt"]),
"created_at": format_ts(row["created_at"]),
}
def backoff_seconds(attempt: int) -> float:
base = min(config.JOB_BACKOFF_CAP_SECONDS, config.JOB_BACKOFF_BASE_SECONDS * (2 ** max(0, attempt - 1)))
return base * random.uniform(0.5, 1.5)
async def insert_job(conn, attachment_id: UUID, created: datetime) -> UUID:
job_id = uuid4()
await conn.execute(
"""
INSERT INTO attachment_jobs (
job_id, attachment_id, pipeline_version, attempt, status,
next_attempt_at, created_at, updated_at
)
VALUES ($1, $2, 1, 1, 'queued', $3, $3, $3)
""",
job_id,
attachment_id,
created,
)
await conn.execute(
"""
INSERT INTO attachment_job_events (job_id, attachment_id, kind, detail)
VALUES ($1, $2, 'queued', 'upload committed')
""",
job_id,
attachment_id,
)
return job_id
def kick_publish() -> None:
try:
asyncio.get_running_loop().create_task(publish_pending())
except RuntimeError:
return
async def publish_pending(limit: int = 20) -> int:
if not config.BROKER_URL:
return 0
async with _publish_lock:
try:
async with db.require().acquire() as conn:
async with conn.transaction():
rows = await conn.fetch(
"""
SELECT job_id, attachment_id, pipeline_version, attempt, created_at
FROM attachment_jobs
WHERE published_at IS NULL
AND status = 'queued'
AND (next_attempt_at IS NULL OR next_attempt_at <= now())
ORDER BY created_at
LIMIT $1
FOR UPDATE SKIP LOCKED
""",
limit,
)
for row in rows:
await broker.publish_job(message_of(row))
await conn.execute(
"UPDATE attachment_jobs SET published_at = now() WHERE job_id = $1",
row["job_id"],
)
return len(rows)
except Exception:
logger.warning("outbox publish failed; the job row stays unpublished")
return 0
async def _outbox_loop() -> None:
while True:
try:
await publish_pending()
except asyncio.CancelledError:
raise
except Exception:
logger.warning("outbox loop failed")
await asyncio.sleep(0.25)
def start_outbox() -> None:
global _outbox_task
if _outbox_task is None or _outbox_task.done():
_outbox_task = asyncio.create_task(_outbox_loop())
async def stop_outbox() -> None:
global _outbox_task
task = _outbox_task
_outbox_task = None
if task is None:
return
task.cancel()
try:
await task
except asyncio.CancelledError:
pass
await broker.close()
def _aware(value: datetime | None) -> datetime | None:
if value is None:
return None
if value.tzinfo is None:
return value.replace(tzinfo=timezone.utc)
return value
async def claim(job_id: UUID, pipeline_version: int):
token = uuid4()
async with db.require().acquire() as conn:
async with conn.transaction():
row = await conn.fetchrow(
"SELECT * FROM attachment_jobs WHERE job_id = $1 FOR UPDATE",
job_id,
)
if row is None:
return "missing", None
if int(row["pipeline_version"]) != pipeline_version:
return "stale", row
if row["status"] == "ready":
return "ready", row
attachment = await conn.fetchrow(
"SELECT status FROM attachments WHERE id = $1",
row["attachment_id"],
)
if attachment is not None and attachment["status"] == "ready":
await conn.execute(
"""
UPDATE attachment_jobs
SET status = 'ready', error_code = NULL, lease_token = NULL,
lease_until = NULL, updated_at = now()
WHERE job_id = $1 AND status <> 'ready'
""",
job_id,
)
return "ready", row
if row["status"] == "failed":
return "failed", row
lease = _aware(row["lease_until"])
if row["status"] == "processing" and lease is not None and lease > datetime.now(timezone.utc):
return "busy", row
if int(row["attempt"]) > config.JOB_MAX_ATTEMPTS:
await _fail(conn, row, "retry_exhausted")
return "failed", row
await conn.execute(
"""
UPDATE attachment_jobs
SET status = 'processing',
lease_token = $2,
lease_until = now() + ($3::int * interval '1 second'),
error_code = NULL,
updated_at = now()
WHERE job_id = $1
""",
job_id,
token,
config.JOB_LEASE_SECONDS,
)
await conn.execute(
"""
UPDATE attachments
SET status = 'processing', error_code = NULL, updated_at = now()
WHERE id = $1 AND status <> 'ready'
""",
row["attachment_id"],
)
claimed = dict(row)
claimed["lease_token"] = token
claimed["status"] = "processing"
return "claimed", claimed
async def _fail(conn, row, code: str) -> None:
await conn.execute(
"""
UPDATE attachment_jobs
SET status = 'failed', error_code = $2, lease_token = NULL, lease_until = NULL, updated_at = now()
WHERE job_id = $1 AND status <> 'ready'
""",
row["job_id"],
code,
)
await conn.execute(
"""
UPDATE attachments
SET status = 'failed', error_code = $2, updated_at = now()
WHERE id = $1 AND status <> 'ready'
""",
row["attachment_id"],
code,
)
await conn.execute(
"""
INSERT INTO attachment_job_events (job_id, attachment_id, kind, detail)
VALUES ($1, $2, 'failed', $3)
""",
row["job_id"],
row["attachment_id"],
code,
)
async def fail_job(job_id: UUID, lease_token: UUID, code: str) -> bool:
async with db.require().acquire() as conn:
async with conn.transaction():
row = await conn.fetchrow(
"""
SELECT * FROM attachment_jobs
WHERE job_id = $1 AND lease_token = $2 AND status = 'processing'
FOR UPDATE
""",
job_id,
lease_token,
)
if row is None:
return False
await _fail(conn, row, code)
failed = dict(row)
try:
await broker.publish_dlq(message_of(failed))
except Exception:
logger.warning("failed job is stored; DLQ publish will be retried by an operator replay")
return True
async def schedule_retry(job_id: UUID, lease_token: UUID, code: str) -> str:
async with db.require().acquire() as conn:
async with conn.transaction():
row = await conn.fetchrow(
"""
SELECT * FROM attachment_jobs
WHERE job_id = $1 AND lease_token = $2 AND status = 'processing'
FOR UPDATE
""",
job_id,
lease_token,
)
if row is None:
return "lost"
nxt = int(row["attempt"]) + 1
if nxt > config.JOB_MAX_ATTEMPTS:
await _fail(conn, row, "retry_exhausted")
failed = True
else:
delay = backoff_seconds(int(row["attempt"]))
await conn.execute(
"""
UPDATE attachment_jobs
SET status = 'queued',
attempt = $2,
error_code = $3,
lease_token = NULL,
lease_until = NULL,
published_at = NULL,
next_attempt_at = now() + ($4::double precision * interval '1 second'),
updated_at = now()
WHERE job_id = $1
""",
job_id,
nxt,
code,
delay,
)
await conn.execute(
"""
UPDATE attachments
SET status = 'queued', error_code = NULL, updated_at = now()
WHERE id = $1 AND status <> 'ready'
""",
row["attachment_id"],
)
await conn.execute(
"""
INSERT INTO attachment_job_events (job_id, attachment_id, kind, detail)
VALUES ($1, $2, 'retry', $3)
""",
job_id,
row["attachment_id"],
code,
)
failed = False
failed_row = None
if failed:
failed_row = row
if failed:
try:
await broker.publish_dlq(message_of(failed_row))
except Exception:
logger.warning("retry exhausted; DLQ publish failed")
return "failed"
return "retry"
async def _intent(key: str) -> None:
async with db.require().acquire() as conn:
await conn.execute(
"""
INSERT INTO blob_writes (object_key, committed)
VALUES ($1, FALSE)
ON CONFLICT (object_key) DO NOTHING
""",
key,
)
async def complete_job(job_id: UUID, lease_token: UUID, attachment_id: UUID, final_key: str, variants: list) -> bool:
async with db.require().acquire() as conn:
async with conn.transaction():
owned = await conn.fetchval(
"""
UPDATE attachment_jobs
SET status = 'ready', error_code = NULL, lease_token = NULL, lease_until = NULL, updated_at = now()
WHERE job_id = $1 AND lease_token = $2 AND status = 'processing'
RETURNING job_id
""",
job_id,
lease_token,
)
if owned is None:
return False
current = await conn.fetchrow(
"SELECT status FROM attachments WHERE id = $1 FOR UPDATE",
attachment_id,
)
if current is None:
raise PermanentJobError("invalid_image")
if current["status"] != "ready":
await conn.execute(
"""
UPDATE attachments
SET status = 'ready', object_key = $2, error_code = NULL, updated_at = now()
WHERE id = $1 AND status <> 'ready'
""",
attachment_id,
final_key,
)
for variant in variants:
await conn.execute(
"""
INSERT INTO attachment_variants (
attachment_id, pipeline_version, name, content_type, size_bytes,
width, height, sha256, object_key
)
VALUES ($1, 1, $2, $3, $4, $5, $6, $7, $8)
ON CONFLICT (attachment_id, pipeline_version, name) DO NOTHING
""",
attachment_id,
variant["name"],
variant["content_type"],
variant["size_bytes"],
variant["width"],
variant["height"],
variant["sha256"],
variant["object_key"],
)
await conn.execute(
"UPDATE blob_writes SET committed = TRUE WHERE object_key = $1",
variant["object_key"],
)
await conn.execute(
"UPDATE blob_writes SET committed = TRUE WHERE object_key = $1",
final_key,
)
await conn.execute(
"""
INSERT INTO attachment_job_events (job_id, attachment_id, kind, detail)
VALUES ($1, $2, 'ready', 'variants committed')
""",
job_id,
attachment_id,
)
return True
async def load_attachment(attachment_id: UUID):
async with db.require().acquire() as conn:
return await conn.fetchrow(
"""
SELECT id, filename, content_type, size_bytes, sha256, object_key, status
FROM attachments
WHERE id = $1
""",
attachment_id,
)
async def process(job_id: UUID, pipeline_version: int) -> str:
state, row = await claim(job_id, pipeline_version)
if state != "claimed":
return state
assert row is not None
lease = row["lease_token"]
try:
await asyncio.wait_for(_effect(row, lease), config.JOB_TIMEOUT_SECONDS)
except PermanentJobError as exc:
await fail_job(job_id, lease, exc.code)
return "failed"
except (TransientJobError, asyncio.TimeoutError, AppError):
return await schedule_retry(job_id, lease, "processing_failed")
except Exception:
logger.warning("job processing failed job_id=%s", job_id)
return await schedule_retry(job_id, lease, "processing_failed")
return "ready"
async def _effect(row, lease: UUID) -> None:
attachment = await load_attachment(row["attachment_id"])
if attachment is None:
raise PermanentJobError("invalid_image")
if attachment["status"] == "ready":
committed = await complete_job(row["job_id"], lease, attachment["id"], attachment["object_key"], [])
if not committed:
raise TransientJobError("processing_failed")
return
data = await s3.get_bytes(attachment["object_key"])
if len(data) != attachment["size_bytes"] or hashlib.sha256(data).hexdigest() != attachment["sha256"]:
raise PermanentJobError("invalid_image")
final_key = f"originals/{attachment['id']}"
if attachment["object_key"] != final_key:
await _intent(final_key)
await s3.put_bytes(final_key, data, attachment["content_type"])
rendered = await asyncio.to_thread(pipeline.render, data, attachment["content_type"])
stored = []
for name, (payload, content_type, width, height) in rendered.items():
suffix = "webp" if content_type == "image/webp" else "jpg"
key = f"variants/{attachment['id']}/{name}.{suffix}"
await _intent(key)
await s3.put_bytes(key, payload, content_type)
stored.append(
{
"name": name,
"content_type": content_type,
"size_bytes": len(payload),
"width": width,
"height": height,
"sha256": hashlib.sha256(payload).hexdigest(),
"object_key": key,
}
)
if config.WORKER_CRASH_POINT == "after_s3":
logger.warning("injected crash after object write before result commit job_id=%s", row["job_id"])
os._exit(1)
committed = await complete_job(row["job_id"], lease, attachment["id"], final_key, stored)
if not committed:
raise TransientJobError("processing_failed")
if attachment["object_key"] != final_key:
try:
await s3.delete_key(attachment["object_key"])
async with db.require().acquire() as conn:
await conn.execute("DELETE FROM blob_writes WHERE object_key = $1", attachment["object_key"])
except Exception:
logger.warning("staging object left for retention attachment_id=%s", attachment["id"])
async def replay_attachment(attachment_id: str) -> dict:
async with db.require().acquire() as conn:
job_id = await conn.fetchval(
"""
SELECT job_id FROM attachment_jobs
WHERE attachment_id = $1 AND pipeline_version = 1
""",
UUID(attachment_id),
)
if job_id is None:
return {"result": "missing"}
return await replay(job_id)
async def replay(job_id: UUID) -> dict:
async with db.require().acquire() as conn:
async with conn.transaction():
row = await conn.fetchrow(
"SELECT * FROM attachment_jobs WHERE job_id = $1 FOR UPDATE",
job_id,
)
if row is None:
return {"result": "missing"}
if row["status"] == "ready":
await conn.execute(
"""
INSERT INTO attachment_job_events (job_id, attachment_id, kind, detail)
VALUES ($1, $2, 'replay', 'noop ready')
""",
row["job_id"],
row["attachment_id"],
)
return {"result": "noop", "job_id": str(row["job_id"])}
lease = _aware(row["lease_until"])
if row["status"] == "processing" and lease is not None and lease > datetime.now(timezone.utc):
return {"result": "busy", "job_id": str(row["job_id"])}
await conn.execute(
"""
UPDATE attachment_jobs
SET status = 'queued',
attempt = 1,
error_code = NULL,
lease_token = NULL,
lease_until = NULL,
published_at = NULL,
next_attempt_at = now(),
updated_at = now()
WHERE job_id = $1 AND status <> 'ready'
""",
job_id,
)
await conn.execute(
"""
UPDATE attachments
SET status = 'queued', error_code = NULL, updated_at = now()
WHERE id = $1 AND status <> 'ready'
""",
row["attachment_id"],
)
await conn.execute(
"""
INSERT INTO attachment_job_events (job_id, attachment_id, kind, detail)
VALUES ($1, $2, 'replay', 'operator replay')
""",
row["job_id"],
row["attachment_id"],
)
await publish_pending()
return {"result": "queued", "job_id": str(job_id)}
async def reap_expired() -> int:
async with db.require().acquire() as conn:
async with conn.transaction():
rows = await conn.fetch(
"""
SELECT job_id, attachment_id, attempt
FROM attachment_jobs
WHERE status = 'processing' AND lease_until < now()
FOR UPDATE SKIP LOCKED
"""
)
changed = 0
for row in rows:
nxt = int(row["attempt"]) + 1
if nxt > config.JOB_MAX_ATTEMPTS:
await conn.execute(
"""
UPDATE attachment_jobs
SET status = 'failed', error_code = 'retry_exhausted',
lease_token = NULL, lease_until = NULL, updated_at = now()
WHERE job_id = $1 AND status = 'processing'
""",
row["job_id"],
)
await conn.execute(
"""
UPDATE attachments
SET status = 'failed', error_code = 'retry_exhausted', updated_at = now()
WHERE id = $1 AND status <> 'ready'
""",
row["attachment_id"],
)
else:
await conn.execute(
"""
UPDATE attachment_jobs
SET status = 'queued',
attempt = $2,
error_code = 'lease_expired',
lease_token = NULL,
lease_until = NULL,
published_at = NULL,
next_attempt_at = now(),
updated_at = now()
WHERE job_id = $1 AND status = 'processing'
""",
row["job_id"],
nxt,
)
await conn.execute(
"""
UPDATE attachments
SET status = 'queued', error_code = NULL, updated_at = now()
WHERE id = $1 AND status <> 'ready'
""",
row["attachment_id"],
)
changed += 1
return changed
+5 -1
View File
@@ -12,7 +12,7 @@ from starlette.requests import Request
from starlette.responses import Response
from starlette.routing import Route
from app import attachments, config, db, rediscli
from app import attachments, config, db, jobs, rediscli
from app.docs import openapi_json, swagger_ui
from app.errors import AppError, error_response, from_app_error
from app.middleware import RequestContextMiddleware
@@ -30,6 +30,7 @@ from app.routes import (
health_ready,
lab_flags,
lab_metrics,
lab_replay,
lab_sweep,
list_chats,
list_messages,
@@ -70,10 +71,12 @@ async def startup() -> None:
await db.apply_migrations()
await rediscli.connect()
attachments.start_sweeper()
jobs.start_outbox()
async def shutdown() -> None:
await attachments.stop_sweeper()
await jobs.stop_outbox()
await rediscli.close()
await db.close()
@@ -102,6 +105,7 @@ def build_app() -> Starlette:
Route("/api/v1/_lab/metrics", lab_metrics, methods=["GET"]),
Route("/api/v1/_lab/flags", lab_flags, methods=["POST"]),
Route("/api/v1/_lab/sweep", lab_sweep, methods=["POST"]),
Route("/api/v1/_lab/attachments/{attachment_id}/replay", lab_replay, methods=["POST"]),
],
exception_handlers={404: not_found_handler, AppError: app_error_handler},
on_startup=[startup],
+95
View File
@@ -0,0 +1,95 @@
"""Image variants. The worker calls this; the API process does not resize."""
from __future__ import annotations
import io
import math
from PIL import Image, ImageOps, UnidentifiedImageError
from app import config
from app.images import ImageRejected, inspect_image
class PermanentJobError(Exception):
def __init__(self, code: str) -> None:
super().__init__(code)
self.code = code
class TransientJobError(Exception):
def __init__(self, code: str = "processing_failed") -> None:
super().__init__(code)
self.code = code
def _flatten(image: Image.Image) -> Image.Image:
if image.mode == "RGB":
return image
rgba = image.convert("RGBA")
background = Image.new("RGB", rgba.size, (255, 255, 255))
background.paste(rgba, mask=rgba.getchannel("A"))
return background
def _open_rgb(data: bytes, content_type: str) -> Image.Image:
try:
inspect_image(data, content_type)
except ImageRejected as exc:
raise PermanentJobError("invalid_image") from exc
Image.MAX_IMAGE_PIXELS = config.MAX_IMAGE_PIXELS
try:
with Image.open(io.BytesIO(data)) as image:
image.load()
oriented = ImageOps.exif_transpose(image) or image
return _flatten(oriented).copy()
except Image.DecompressionBombError as exc:
raise PermanentJobError("invalid_image") from exc
except (UnidentifiedImageError, OSError, ValueError, SyntaxError) as exc:
raise PermanentJobError("invalid_image") from exc
def _fit_down(image: Image.Image, box: int) -> Image.Image:
width, height = image.size
scale = min(box / width, box / height, 1.0)
target = (max(1, math.floor(width * scale)), max(1, math.floor(height * scale)))
if target == (width, height):
return image
return image.resize(target, Image.Resampling.LANCZOS)
def _crop_square(image: Image.Image, side: int) -> Image.Image:
width, height = image.size
edge = min(width, height)
left = (width - edge) // 2
top = (height - edge) // 2
cropped = image.crop((left, top, left + edge, top + edge))
if cropped.size != (side, side):
cropped = cropped.resize((side, side), Image.Resampling.LANCZOS)
return cropped
def _jpeg(image: Image.Image) -> bytes:
buffer = io.BytesIO()
image.save(buffer, format="JPEG", quality=90, optimize=True)
return buffer.getvalue()
def _webp(image: Image.Image) -> bytes:
# quality=80, method=6. EXIF is omitted because the saved image is a new RGB buffer.
buffer = io.BytesIO()
image.save(buffer, format="WEBP", quality=80, method=6)
return buffer.getvalue()
def render(data: bytes, content_type: str) -> dict:
"""Return thumbnail, crop and optimized payloads. Does not touch storage."""
image = _open_rgb(data, content_type)
thumb = _fit_down(image, 256)
crop = _crop_square(image, 128)
optimized = _fit_down(image, 1280)
return {
"thumbnail": ( _jpeg(thumb), "image/jpeg", thumb.size[0], thumb.size[1]),
"crop": (_jpeg(crop), "image/jpeg", crop.size[0], crop.size[1]),
"optimized": (_webp(optimized), "image/webp", optimized.size[0], optimized.size[1]),
}
+30
View File
@@ -0,0 +1,30 @@
"""Operator replay of one failed job. A ready job is left unchanged."""
from __future__ import annotations
import asyncio
import sys
from uuid import UUID
from app import db, jobs
async def _run(job_id: UUID) -> int:
await db.connect()
try:
result = await jobs.replay(job_id)
finally:
await db.close()
print(result["result"])
return 0 if result["result"] in {"noop", "queued"} else 1
def main() -> None:
if len(sys.argv) != 2:
print("usage: python -m app.replay JOB_ID", file=sys.stderr)
raise SystemExit(2)
raise SystemExit(asyncio.run(_run(UUID(sys.argv[1]))))
if __name__ == "__main__":
main()
+40 -8
View File
@@ -5,7 +5,7 @@ import re
from starlette.requests import Request
from starlette.responses import JSONResponse, Response
from app import attachments, cache, config, db, direct, images, rediscli, s3, sessions, store
from app import attachments, cache, config, db, direct, images, jobs, rediscli, s3, sessions, store
from app.cursor_codec import decode_cursor, encode_cursor
from app.errors import BASIC_CHALLENGE, BEARER_CHALLENGE, AppError, error_response
from app.passwords import dummy_hash, hash_password, verify_password
@@ -309,7 +309,7 @@ async def upload_attachment(request: Request) -> Response:
except images.ImageRejected as exc:
return error_response(exc.status, exc.message, rid)
created = await attachments.create_attachment(chat_id, user["id"], filename, content_type, data)
return _json(created, rid, 201)
return _json(created, rid, 202)
async def begin_direct_upload(request: Request) -> Response:
@@ -360,7 +360,7 @@ async def complete_direct_upload(request: Request) -> Response:
if not isinstance(digest, str) or re.fullmatch(r"[a-f0-9]{64}", digest) is None:
return error_response(400, "invalid checksum", rid)
created = await direct.complete_upload(upload_id, user["id"], digest)
return _json(created, rid, 201)
return _json(created, rid, 202)
async def get_attachment(request: Request) -> Response:
@@ -372,7 +372,7 @@ async def get_attachment(request: Request) -> Response:
row = await attachments.get_for_member(attachment_id, user["id"])
if row is None:
return error_response(404, "attachment not found", rid)
return _json(attachments._public(row), rid)
return _json(await attachments.present(row), rid)
async def download_attachment(request: Request) -> Response:
@@ -381,14 +381,46 @@ async def download_attachment(request: Request) -> Response:
attachment_id = request.path_params["attachment_id"]
if validate_uuid(attachment_id):
return error_response(400, "invalid attachment id", rid)
variant = request.query_params.get("variant")
if variant not in (None, "original"):
variant = request.query_params.get("variant") or "original"
if variant not in ("original", "thumbnail", "crop", "optimized"):
return error_response(400, "invalid variant", rid)
row = await attachments.get_for_member(attachment_id, user["id"])
if row is None:
return error_response(404, "attachment not found", rid)
payload = await s3.get_bytes(row["object_key"])
return Response(payload, status_code=200, media_type=row["content_type"], headers={"X-Request-Id": rid})
if row["status"] != "ready":
return error_response(409, "attachment is not ready", rid)
if variant == "original":
payload = await s3.get_bytes(row["object_key"])
media_type = row["content_type"]
else:
async with db.require().acquire() as conn:
found = await conn.fetchrow(
"""
SELECT content_type, object_key
FROM attachment_variants
WHERE attachment_id = $1 AND pipeline_version = 1 AND name = $2
""",
row["id"],
variant,
)
if found is None:
return error_response(404, "attachment not found", rid)
payload = await s3.get_bytes(found["object_key"])
media_type = found["content_type"]
return Response(payload, status_code=200, media_type=media_type, headers={"X-Request-Id": rid})
async def lab_replay(request: Request) -> Response:
rid = request.state.request_id
if not config.lab_mode():
return error_response(404, "not found", rid)
attachment_id = request.path_params["attachment_id"]
if validate_uuid(attachment_id):
return error_response(400, "invalid attachment id", rid)
result = await jobs.replay_attachment(attachment_id)
if result["result"] == "missing":
return error_response(404, "attachment not found", rid)
return _json(result, rid)
async def lab_sweep(request: Request) -> Response:
+1 -1
View File
@@ -283,7 +283,7 @@ async def add_message(
str(found[item]["chat_id"]) != chat_id for item in attachment_ids
):
return None, "not_found"
if any(found[item]["status"] != "ready" for item in attachment_ids):
if any(found[item]["status"] == "failed" for item in attachment_ids):
return None, "not_ready"
resolved = attachment_ids
message_id = uuid4()
+97
View File
@@ -0,0 +1,97 @@
"""Separate process: consume durable jobs and publish image variants."""
from __future__ import annotations
import asyncio
import logging
import signal
import sys
from uuid import UUID
import aio_pika
from app import broker, config, db, jobs
from app.broker import payload_from
logger = logging.getLogger("messenger.worker")
def configure_logging() -> None:
logging.basicConfig(
level=getattr(logging, config.LOG_LEVEL.upper(), logging.INFO),
format="%(asctime)s %(levelname)s %(name)s %(message)s",
stream=sys.stdout,
)
async def _settle(message: aio_pika.IncomingMessage) -> None:
try:
payload = payload_from(message.body)
job_id = UUID(payload["job_id"])
pipeline_version = int(payload["pipeline_version"])
except (ValueError, KeyError, TypeError):
logger.warning("dropping a job message that does not match the schema")
await message.ack()
return
try:
waits = 0
while True:
state = await jobs.process(job_id, pipeline_version)
if state == "busy":
waits += 1
if waits > config.JOB_LEASE_SECONDS + 5:
await message.reject(requeue=True)
return
await asyncio.sleep(1)
continue
await message.ack()
return
except Exception:
logger.warning("job settle failed job_id=%s", payload.get("job_id"))
await message.reject(requeue=True)
async def _reap_loop(stop: asyncio.Event) -> None:
while not stop.is_set():
try:
count = await jobs.reap_expired()
if count:
logger.info("requeued %s expired leases", count)
except Exception:
logger.warning("lease reaper failed")
try:
await asyncio.wait_for(stop.wait(), timeout=2)
except asyncio.TimeoutError:
continue
async def serve() -> None:
await db.connect()
stop = asyncio.Event()
loop = asyncio.get_running_loop()
for sig in (signal.SIGTERM, signal.SIGINT):
try:
loop.add_signal_handler(sig, stop.set)
except NotImplementedError:
signal.signal(sig, lambda *_: stop.set())
reaper = asyncio.create_task(_reap_loop(stop))
queue, consumer = await broker.consume(_settle, config.WORKER_CONCURRENCY)
logger.info("worker consuming prefetch=%s", config.WORKER_CONCURRENCY)
await stop.wait()
await queue.cancel(consumer)
reaper.cancel()
try:
await reaper
except asyncio.CancelledError:
pass
await broker.close()
await db.close()
def main() -> None:
configure_logging()
asyncio.run(serve())
if __name__ == "__main__":
main()