lab5: RabbitMQ, relay и worker в стенде; reconciler lab4 убран

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

broker: RabbitMQ 4.3.7 (тег+digest), quorum queue + DLX/DLQ из definitions, пользователи
relay (только publish в exchange) и worker (только consume), без management UI и портов.
relay: outbox -> publish с publisher confirm, reaper истёкших lease, страховка от потерянных
сообщений. worker: claim + heartbeat lease, варианты, публикация с fencing, ack после COMMIT,
graceful stop, уборка staging и мусора попыток. S3: два bucket и четыре identity
(admin, api, worker, auditor) с проверкой модели доступа в s3-init. Отдельная сеть queue.
Reconciler lab4 (трек прошлой лабы) удалён: его роль выполняет уборка по журналу попыток.
This commit is contained in:
Alexey Bodanyuk
2026-10-31 16:12:52 +03:00
parent 655f264104
commit bc32a50967
15 changed files with 1058 additions and 141 deletions
+15 -3
View File
@@ -1,12 +1,24 @@
"""Docker HEALTHCHECK probe without curl/wget in the image: `python -m messenger.healthcheck`."""
"""Docker HEALTHCHECK probe without curl/wget in the image: `python -m messenger.healthcheck`.
(no arguments) API: GET /health/ready on the local port
--file /tmp/heartbeat --max-age 30 relay/worker: the main loop wrote its heartbeat recently
(it writes it only while its database/broker work)
"""
from __future__ import annotations
import os
import sys
import time
import urllib.request
def main() -> int:
def main(argv: list[str]) -> int:
if len(argv) == 4 and argv[0] == "--file" and argv[2] == "--max-age":
try:
with open(argv[1], encoding="ascii") as f:
return 0 if time.time() - float(f.read().strip()) <= float(argv[3]) else 1
except (OSError, ValueError):
return 1
port = os.environ.get("APP_PORT", "8080").strip() or "8080"
try:
with urllib.request.urlopen(f"http://127.0.0.1:{port}/health/ready", timeout=2) as response:
@@ -16,4 +28,4 @@ def main() -> int:
if __name__ == "__main__":
sys.exit(main())
sys.exit(main(sys.argv[1:]))
+163
View File
@@ -0,0 +1,163 @@
"""Outbox relay: `python -m messenger.relay` (compose service `relay`, one process).
loop: SELECT due outbox rows FOR UPDATE SKIP LOCKED
-> publish each job message to RabbitMQ, wait for the publisher CONFIRM
-> UPDATE outbox SET published_at -> COMMIT
every few seconds: reaper (expired leases -> queued/failed), lost-message guard.
Why an outbox: the API commits the job and the outbox row in ONE PostgreSQL transaction and never
talks to the broker. A crash between COMMIT and publish leaves the row unpublished; the relay
publishes it when it (or the broker) is back - nobody re-creates a job by hand. While the broker
is down, uploads still get 202 and the rows wait in the outbox.
A crash after the confirm but before the COMMIT of published_at publishes the row once more after
the restart: the queue then holds a duplicate. That is at-least-once delivery; the worker turns
the duplicate into a no-op (jobs.claim). The relay never deletes or rewrites jobs.
"""
from __future__ import annotations
import asyncio
import logging
import sys
import time
import aio_pika
from aio_pika.abc import AbstractChannel, AbstractConnection, AbstractExchange
from . import broker, jobs, logging_setup, service
from .config import ConfigError, Settings
from .db import Database
from .errors import Unavailable
log = logging.getLogger("messenger.relay")
ACTOR = "relay"
MAINTENANCE_SECONDS = 5.0
PURGE_SECONDS = 600.0
class Relay:
def __init__(self, settings: Settings):
self.settings = settings
self.db = Database(settings, name="messenger-relay")
self.connection: AbstractConnection | None = None
self.channel: AbstractChannel | None = None
self.exchange: AbstractExchange | None = None
async def connect(self) -> None:
self.connection = await aio_pika.connect(
self.settings.broker_url, timeout=5,
client_properties={"connection_name": "relay@" + self.settings.instance_id})
# Confirm mode: every publish() waits for basic.ack of the broker (message in the quorum log).
self.channel = await self.connection.channel(publisher_confirms=True, on_return_raises=True)
# No declare: the topology belongs to the broker definitions; the relay may only write.
self.exchange = await self.channel.get_exchange(broker.EXCHANGE, ensure=False)
log.info("broker_connected", extra={"fields": {"exchange": broker.EXCHANGE}})
async def disconnect(self) -> None:
if self.connection is not None:
try:
await self.connection.close()
except Exception: # noqa: BLE001 - already broken
pass
self.connection = self.channel = self.exchange = None
async def publish_due(self) -> int:
settings = self.settings
exchange = self.exchange
async def work(conn) -> list[jobs.Due]:
rows = await jobs.due_outbox(conn, settings.relay_batch)
if "reverse_order" in settings.fault_points:
rows.reverse()
# Rows of jobs that are no longer queued (finished meanwhile) need no message.
sendable = [r for r in rows if r.job_state == "queued"]
copies = 2 if "duplicate_publish" in settings.fault_points else 1
for row in sendable:
message = aio_pika.Message(
broker.encode(row.job_id, row.attachment_id, row.pipeline_version, row.attempt, row.created_at),
delivery_mode=aio_pika.DeliveryMode.PERSISTENT, content_type="application/json",
message_id=f"{row.job_id}:{row.outbox_id}", type=broker.JOB_TYPE)
for _ in range(copies):
await exchange.publish(message, routing_key=broker.ROUTING_KEY, mandatory=True, timeout=5)
for row in sendable:
# Confirmed by the broker, outbox not marked yet: the window "after publish, before
# the confirm is recorded". The crash rolls this transaction back.
await service.crash_once(settings, self.db, "crash_after_publish", row.job_id, ACTOR,
outbox_id=row.outbox_id)
await jobs.mark_published(conn, [r.outbox_id for r in rows])
return sendable
sent = await self.db.run(work, deadline=10 + 5 * settings.relay_batch)
for row in sent:
log.info("job_published", extra={"fields": {"job_id": row.job_id, "attempt": row.attempt,
"outbox_id": row.outbox_id}})
return len(sent)
async def maintenance(self) -> None:
expired = await self.db.run(lambda c: jobs.expire_leases(c, ACTOR))
republished = await self.db.run(lambda c: jobs.republish_stale(c, self.settings.queued_republish_seconds, ACTOR))
if expired or republished:
log.info("maintenance", extra={"fields": {"leases_expired": expired, "republished": republished}})
async def run(self) -> int:
stop = service.stop_event()
await self.db.open()
next_maintenance = next_purge = 0.0
failures = 0
try:
while not stop.is_set():
healthy = True
now = time.monotonic()
try:
if now >= next_maintenance:
await self.maintenance()
next_maintenance = now + MAINTENANCE_SECONDS
if now >= next_purge:
purged = await self.db.run(jobs.purge_outbox)
next_purge = now + PURGE_SECONDS
if purged:
log.info("outbox_purged", extra={"fields": {"rows": purged}})
except Unavailable:
healthy = False
try:
if self.exchange is None or self.connection is None or self.connection.is_closed:
await self.disconnect()
await self.connect()
sent = await self.publish_due()
failures = 0
except Unavailable:
healthy, sent = False, 0
except (aio_pika.exceptions.AMQPError, ConnectionError, OSError, asyncio.TimeoutError) as exc:
# Broker down/unreachable or a message returned/nacked: nothing marked, the rows
# stay in the outbox (transaction rolled back) and are published later.
failures += 1
healthy, sent = False, 0
log.warning("publish_failed", extra={"fields": {"error": type(exc).__name__, "failures": failures}})
await self.disconnect()
if healthy:
service.beat()
if not sent:
await service.sleep_or_stop(stop, min(5.0, self.settings.relay_poll_seconds * (2 ** min(failures, 3))))
finally:
await self.disconnect()
await self.db.close()
log.info("relay_stopped")
return 0
def main() -> int:
try:
settings = Settings.from_env(s3_credentials=False)
if not settings.broker_url:
raise ConfigError("BROKER_URL_FILE (or BROKER_URL) must be set")
except ConfigError as exc:
print(f"configuration error: {exc}", file=sys.stderr)
return 2
logging_setup.configure(settings.log_level)
log.info("relay_starting", extra={"fields": {"instance_id": settings.instance_id,
"fault_points": sorted(settings.fault_points)}})
return asyncio.run(Relay(settings).run())
if __name__ == "__main__":
sys.exit(main())
+79 -48
View File
@@ -3,14 +3,15 @@
Idempotent, safe to run on every `docker compose up`:
1. waits until the S3 endpoint answers;
2. creates the bucket with the ADMIN key pair if it does not exist yet (an existing bucket and
its objects are left untouched);
3. writes the sentinel object `health/sentinel` that the API's readiness probe HEADs;
2. creates the final bucket (S3_BUCKET) and the staging bucket (S3_STAGING_BUCKET) with the ADMIN
key pair if they do not exist yet (existing buckets and their objects are left untouched);
3. writes the sentinel object `health/sentinel` into the final bucket (the API readiness HEADs it);
4. proves the access model and FAILS the stand (exit 1) if it is wrong:
* anonymous (unsigned) GET of an object and anonymous LIST of the bucket are refused;
* the API key pair can read/write objects of ITS bucket, but can neither list it, nor create
a bucket, nor see or read another bucket (a canary bucket created here for this check);
* the reconciler key pair can list the bucket but cannot create one.
* anonymous (unsigned) GET of an object and anonymous LIST are refused on both buckets;
* api: writes staging, reads final; cannot read staging, write final, list or create buckets,
nor read another bucket (a canary bucket created here for this check);
* worker: reads and writes both buckets; cannot list or create buckets;
* auditor: reads and lists both buckets; cannot write.
Only file names and verdicts are printed, never key values.
"""
from __future__ import annotations
@@ -57,14 +58,33 @@ def settings_with(keys: tuple[str, str]) -> Settings:
return Settings(host="", port=0, env="lab", log_level="info", shutdown_timeout=0, max_body_bytes=1024, **base)
def pair(prefix: str) -> tuple[str, str]:
return _secret(f"{prefix}ACCESS_KEY_ID", True), _secret(f"{prefix}SECRET_ACCESS_KEY", True)
async def ensure_bucket(admin_s3: Storage, bucket: str) -> bool:
code = await error_code(admin_s3.client.head_bucket(Bucket=bucket))
if code == "":
say(f"bucket {bucket}: exists, kept as is")
return True
if code in ("404", "NoSuchBucket", "NotFound"):
code = await error_code(admin_s3.client.create_bucket(Bucket=bucket))
if code not in ("", "BucketAlreadyOwnedByYou", "BucketAlreadyExists"):
say(f"bucket {bucket}: create failed ({code})")
return False
say(f"bucket {bucket}: created")
return True
say(f"bucket {bucket}: head failed ({code})")
return False
async def run() -> int:
admin = (_secret("S3_ADMIN_ACCESS_KEY_ID", True), _secret("S3_ADMIN_SECRET_ACCESS_KEY", True))
api = (_secret("S3_ACCESS_KEY_ID", True), _secret("S3_SECRET_ACCESS_KEY", True))
reconciler = (_secret("S3_RECONCILER_ACCESS_KEY_ID", True), _secret("S3_RECONCILER_SECRET_ACCESS_KEY", True))
settings = settings_with(admin)
bucket = settings.s3_bucket
admin_s3, api_s3, rec_s3 = Storage(settings), Storage(settings_with(api)), Storage(settings_with(reconciler))
for s in (admin_s3, api_s3, rec_s3):
settings = settings_with(pair("S3_ADMIN_"))
final, staging = settings.s3_bucket, settings.s3_staging_bucket
admin_s3 = Storage(settings)
clients = {"api": Storage(settings_with(pair("S3_"))), "worker": Storage(settings_with(pair("S3_WORKER_"))),
"auditor": Storage(settings_with(pair("S3_AUDITOR_")))}
for s in (admin_s3, *clients.values()):
await s.open()
try:
deadline = time.monotonic() + 60
@@ -77,64 +97,75 @@ async def run() -> int:
say("S3 endpoint does not answer within 60 s")
return 1
await asyncio.sleep(1)
code = await error_code(admin_s3.client.head_bucket(Bucket=bucket))
if code == "":
say(f"bucket {bucket}: exists, kept as is")
elif code in ("404", "NoSuchBucket", "NotFound"):
code = await error_code(admin_s3.client.create_bucket(Bucket=bucket))
if code not in ("", "BucketAlreadyOwnedByYou", "BucketAlreadyExists"):
say(f"bucket {bucket}: create failed ({code})")
for bucket in (final, staging):
if not await ensure_bucket(admin_s3, bucket):
return 1
say(f"bucket {bucket}: created")
else:
say(f"bucket {bucket}: head failed ({code})")
return 1
await admin_s3.put(SENTINEL_KEY, b"ok\n", "text/plain", {})
await admin_s3.client.put_object(Bucket=staging, Key=SENTINEL_KEY, Body=b"ok\n")
say(f"sentinel {SENTINEL_KEY}: written")
canary = bucket + "-canary"
canary = final + "-canary"
if await error_code(admin_s3.client.head_bucket(Bucket=canary)):
await admin_s3.client.create_bucket(Bucket=canary)
await admin_s3.client.put_object(Bucket=canary, Key="canary", Body=b"secret of another bucket")
failures = []
base = settings.s3_endpoint.rstrip("/")
for what, url in (("anonymous GET object", f"{base}/{bucket}/{SENTINEL_KEY}"),
("anonymous LIST bucket", f"{base}/{bucket}?list-type=2")):
status = anonymous_status(url)
verdict = "refused" if status in (401, 403) else "ALLOWED"
say(f"{what}: HTTP {status} -> {verdict}")
if verdict != "refused":
failures.append(what)
for bucket in (final, staging):
for what, url in ((f"anonymous GET object of {bucket}", f"{base}/{bucket}/{SENTINEL_KEY}"),
(f"anonymous LIST of {bucket}", f"{base}/{bucket}?list-type=2")):
status = anonymous_status(url)
verdict = "refused" if status in (401, 403) else "ALLOWED"
say(f"{what}: HTTP {status} -> {verdict}")
if verdict != "refused":
failures.append(what)
probe = "health/bootstrap-probe"
def c(who: str):
return clients[who].client
# (what, coroutine, allowed?) - every refusal must be a real AccessDenied, not an outage.
checks = [
("api: read object", await error_code(api_s3.client.head_object(Bucket=bucket, Key=SENTINEL_KEY)), True),
("api: write object", await error_code(api_s3.client.put_object(Bucket=bucket, Key=probe, Body=b"p")), True),
("api: delete object", await error_code(api_s3.client.delete_object(Bucket=bucket, Key=probe)), True),
("api: list bucket", await error_code(api_s3.client.list_objects_v2(Bucket=bucket, MaxKeys=1)), False),
("api: create bucket", await error_code(api_s3.client.create_bucket(Bucket=bucket + "-x")), False),
("api: read object of another bucket",
await error_code(api_s3.client.head_object(Bucket=canary, Key="canary")), False),
("reconciler: list bucket", await error_code(rec_s3.client.list_objects_v2(Bucket=bucket, MaxKeys=1)), True),
("reconciler: create bucket", await error_code(rec_s3.client.create_bucket(Bucket=bucket + "-x")), False),
("api: write staging", c("api").put_object(Bucket=staging, Key=probe, Body=b"p"), True),
("api: delete staging (janitor)", c("api").delete_object(Bucket=staging, Key=probe), True),
("api: read staging", c("api").head_object(Bucket=staging, Key=SENTINEL_KEY), False),
("api: read final", c("api").head_object(Bucket=final, Key=SENTINEL_KEY), True),
("api: write final", c("api").put_object(Bucket=final, Key=probe, Body=b"p"), False),
("api: list final", c("api").list_objects_v2(Bucket=final, MaxKeys=1), False),
("api: list staging", c("api").list_objects_v2(Bucket=staging, MaxKeys=1), False),
("api: create bucket", c("api").create_bucket(Bucket=final + "-x"), False),
("api: read another bucket", c("api").head_object(Bucket=canary, Key="canary"), False),
("worker: read staging", c("worker").head_object(Bucket=staging, Key=SENTINEL_KEY), True),
("worker: write final", c("worker").put_object(Bucket=final, Key=probe, Body=b"p"), True),
("worker: delete final", c("worker").delete_object(Bucket=final, Key=probe), True),
("worker: write staging", c("worker").put_object(Bucket=staging, Key=probe, Body=b"p"), True),
("worker: delete staging", c("worker").delete_object(Bucket=staging, Key=probe), True),
("worker: list final", c("worker").list_objects_v2(Bucket=final, MaxKeys=1), False),
("worker: create bucket", c("worker").create_bucket(Bucket=final + "-x"), False),
("worker: read another bucket", c("worker").head_object(Bucket=canary, Key="canary"), False),
("auditor: list final", c("auditor").list_objects_v2(Bucket=final, MaxKeys=1), True),
("auditor: list staging", c("auditor").list_objects_v2(Bucket=staging, MaxKeys=1), True),
("auditor: read final", c("auditor").head_object(Bucket=final, Key=SENTINEL_KEY), True),
("auditor: write final", c("auditor").put_object(Bucket=final, Key=probe, Body=b"p"), False),
("auditor: delete staging", c("auditor").delete_object(Bucket=staging, Key=SENTINEL_KEY), False),
]
for what, code, allowed in checks:
ok = (code == "") == allowed
for what, coro, allowed in checks:
code = await error_code(coro)
ok = (code == "") == allowed and code in ("", "AccessDenied", "403")
say(f"{what}: {'allowed' if code == '' else 'refused (' + code + ')'} -> {'OK' if ok else 'WRONG'}")
if not ok:
failures.append(what)
visible = [b["Name"] for b in (await api_s3.client.list_buckets()).get("Buckets", [])]
visible = [b["Name"] for b in (await c("api").list_buckets()).get("Buckets", [])]
say(f"api: buckets visible in ListBuckets: {visible} -> {'OK' if canary not in visible else 'WRONG'}")
if canary in visible:
failures.append("api sees another bucket")
if failures:
say("access model is WRONG: " + ", ".join(failures))
return 1
say("done: private bucket, least-privilege keys verified")
say("done: private buckets, least-privilege keys verified")
return 0
finally:
for s in (admin_s3, api_s3, rec_s3):
for s in (admin_s3, *clients.values()):
await s.close()
+67
View File
@@ -0,0 +1,67 @@
"""Common runtime of the background processes (relay, worker): signals, heartbeat file for the
Docker healthcheck, failure injection that fires once per job and leaves an audit row."""
from __future__ import annotations
import asyncio
import logging
import os
import signal
import time
from pathlib import Path
from . import jobs
from .config import Settings
from .db import Database
log = logging.getLogger("messenger.service")
HEARTBEAT_FILE = Path(os.environ.get("HEARTBEAT_FILE", "/tmp/heartbeat"))
CRASH_EXIT_CODE = 70 # os._exit: no cleanup, no ack, no COMMIT - like SIGKILL in the middle of work
def beat() -> None:
"""Healthcheck input: `python -m messenger.healthcheck --file /tmp/heartbeat --max-age 30`."""
try:
HEARTBEAT_FILE.write_text(str(time.time()), encoding="ascii")
except OSError:
pass
def stop_event() -> asyncio.Event:
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, RuntimeError):
pass # Windows unit tests: no signal handlers
return stop
async def sleep_or_stop(stop: asyncio.Event, seconds: float) -> None:
try:
await asyncio.wait_for(stop.wait(), seconds)
except asyncio.TimeoutError:
pass
async def crash_once(settings: Settings, db: Database, point: str, job_id: str, actor: str, **detail) -> None:
"""Experiments only (FAULT_POINTS): kill THIS process at `point`, once per job (and per `detail`),
across restarts and replicas. The decision is an audit row committed before the crash, so the
restarted process (or another replica) continues normally and the evidence stays in job_events."""
if point not in settings.fault_points:
return
async def record(conn) -> bool:
cur = await conn.execute(
"SELECT 1 FROM job_events WHERE job_id = %s AND event = 'fault_injected' AND detail->>'point' = %s "
"AND detail @> %s::jsonb", (job_id, point, jobs.json.dumps({k: str(v) for k, v in detail.items()})))
if await cur.fetchone():
return False
await jobs.event(conn, job_id, "fault_injected", actor, point=point, **{k: str(v) for k, v in detail.items()})
return True
if await db.run(record):
log.warning("fault_injected_crash", extra={"fields": {"point": point, "job_id": job_id, **detail}})
logging.shutdown()
os._exit(CRASH_EXIT_CODE)
+334
View File
@@ -0,0 +1,334 @@
"""Image worker: `python -m messenger.worker` (compose service `worker`, separate from the API).
One delivery of a job message:
decode + validate (schema v1; invalid -> reject -> dead-letter queue)
-> CLAIM in PostgreSQL (jobs.claim): finished/leased/not due/stale -> no-op, ack
-> lease heartbeat every JOB_LEASE_SECONDS/3 while the attempt runs
-> read the source (staging copy or published original), check size + SHA-256 against the DB
-> variants in a child process (imaging.py: pixel, memory and time limits)
-> PUT the original (first publication) and the variants under keys of THIS attempt
-> ONE transaction: variants + attachment ready, fenced by the lease token (jobs.complete)
-> ack -> delete the staging copy (marked in the DB first, then S3).
The ack comes only after the durable effect (the COMMIT). A crash anywhere before it makes
RabbitMQ deliver the message again; the job row then says what is left to do. Failures: a
permanent one (invalid_image, image_too_large, source_missing/corrupt) fails the job at once, a
transient one (S3 down, timeout, killed decoder) is retried with backoff via the outbox, at most
JOB_MAX_ATTEMPTS claims in total.
Concurrency: RabbitMQ prefetch = WORKER_CONCURRENCY unacked messages per worker, and at most
WORKER_CONCURRENCY image child processes. Graceful stop (SIGTERM): stop consuming, let running
attempts finish for WORKER_SHUTDOWN_SECONDS, give the rest back (jobs.release).
The worker also runs the cleanup of the pipeline objects (staging copies that are no longer
needed, objects of attempts that never published) - always decided under a row lock.
"""
from __future__ import annotations
import asyncio
import hashlib
import logging
import sys
import time
import aio_pika
from aio_pika.abc import AbstractChannel, AbstractConnection, AbstractIncomingMessage, AbstractQueue
from . import broker, imaging, jobs, logging_setup, service
from .config import ConfigError, Settings
from .db import Database
from .errors import Unavailable
from .imaging import ProcessingError
from .storage import ObjectMissing, Storage
log = logging.getLogger("messenger.worker")
class Worker:
def __init__(self, settings: Settings):
self.settings = settings
self.name = settings.instance_id
self.actor = "worker:" + self.name
self.db = Database(settings, name="messenger-worker")
self.staging = Storage(settings, bucket=settings.s3_staging_bucket)
self.final = Storage(settings)
self.slots = asyncio.Semaphore(settings.worker_concurrency)
self.tasks: set[asyncio.Task] = set()
self.connection: AbstractConnection | None = None
self.channel: AbstractChannel | None = None
self.queue: AbstractQueue | None = None
self.consumer_tag: str | None = None
# ---------------------------------------------------------------- broker
async def connect(self) -> None:
self.connection = await aio_pika.connect(
self.settings.broker_url, timeout=5,
client_properties={"connection_name": "worker@" + self.name})
self.channel = await self.connection.channel()
# Prefetch: the broker keeps at most this many unacked messages in flight to this worker.
await self.channel.set_qos(prefetch_count=self.settings.worker_concurrency)
self.queue = await self.channel.get_queue(broker.QUEUE, ensure=False)
self.consumer_tag = await self.queue.consume(self.on_message, no_ack=False)
log.info("consuming", extra={"fields": {"queue": broker.QUEUE, "prefetch": self.settings.worker_concurrency}})
async def disconnect(self) -> None:
if self.connection is not None:
try:
await self.connection.close()
except Exception: # noqa: BLE001 - already broken
pass
self.connection = self.channel = self.queue = None
self.consumer_tag = None
async def on_message(self, message: AbstractIncomingMessage) -> None:
task = asyncio.create_task(self.deliver(message))
self.tasks.add(task)
task.add_done_callback(self.tasks.discard)
@staticmethod
async def settle(action) -> None:
"""ack/nack/reject; the channel may be gone (broker restart): then the broker requeues the
message by itself and the redelivery is a no-op."""
try:
await action
except Exception as exc: # noqa: BLE001
log.warning("settle_failed", extra={"fields": {"error": type(exc).__name__}})
async def deliver(self, message: AbstractIncomingMessage) -> None:
try:
body = broker.decode(message.body)
except broker.BadMessage as exc:
log.error("bad_message", extra={"fields": {"reason": str(exc), "message_id": message.message_id}})
await self.settle(message.reject(requeue=False)) # -> messenger.attachment-jobs.dlq
return
try:
async with self.slots:
outcome = await self.process(body["job_id"])
except Unavailable:
# PostgreSQL (or S3 during a DB step) unavailable: nothing decided. Back to the queue
# after a pause; a claim that did happen is protected by its lease.
log.warning("job_deferred", extra={"fields": {"job_id": body["job_id"]}})
await asyncio.sleep(2)
await self.settle(message.nack(requeue=True))
return
except asyncio.CancelledError:
# Shutdown: back to the queue. If the job was claimed it has been released (new outbox
# row), and one of the two deliveries becomes a no-op.
await self.settle(asyncio.shield(message.nack(requeue=True)))
raise
log.info("job_done", extra={"fields": {"job_id": body["job_id"], "outcome": outcome,
"message_attempt": body["attempt"], "redelivered": message.redelivered}})
await self.settle(message.ack())
# ---------------------------------------------------------------- one attempt
async def process(self, job_id: str) -> str:
settings = self.settings
claim = await self.db.run(lambda c: jobs.claim(c, job_id, self.name, settings.job_lease_seconds))
if isinstance(claim, jobs.Noop):
return "noop_" + claim.reason
await service.crash_once(settings, self.db, "crash_after_claim", job_id, self.actor)
lost = asyncio.Event()
work = asyncio.create_task(self.attempt(claim))
beat = asyncio.create_task(self.heartbeat(claim, work, lost))
try:
variants = await work
except asyncio.CancelledError:
if lost.is_set() and not asyncio.current_task().cancelling():
return "fenced" # lease taken over: this attempt may write nothing more
outcome = await asyncio.shield(self.db.run(lambda c: jobs.release(c, claim, self.name)))
log.info("job_released", extra={"fields": {"job_id": job_id, "outcome": outcome}})
raise
except ProcessingError as exc:
return await self.failed(claim, exc.code, exc.permanent)
except Unavailable:
return await self.failed(claim, "storage_unavailable", False)
finally:
beat.cancel()
await service.crash_once(settings, self.db, "crash_after_s3_write", job_id, self.actor)
outcome = await self.db.run(lambda c: jobs.complete(c, claim, variants, self.name))
await service.crash_once(settings, self.db, "crash_after_commit", job_id, self.actor)
if outcome == "succeeded":
await self.delete_staging(claim.attachment_id)
return outcome
async def failed(self, claim: jobs.Claim, code: str, permanent: bool) -> str:
s = self.settings
outcome, delay = await self.db.run(
lambda c: jobs.retry_or_fail(c, claim, code, permanent, self.name, s.job_retry_base_seconds,
s.job_retry_max_seconds))
log.info("attempt_failed", extra={"fields": {"job_id": claim.job_id, "attempt": claim.attempt, "code": code,
"permanent": permanent, "outcome": outcome,
"delay_s": round(delay, 3)}})
return outcome
async def heartbeat(self, claim: jobs.Claim, work: asyncio.Task, lost: asyncio.Event) -> None:
"""Renew the lease while the attempt is alive and within JOB_TIMEOUT_SECONDS. A hung attempt
stops being renewed, so the reaper can hand the job to another worker; if the lease turns out
to be lost, the attempt is cancelled (its result would be fenced anyway)."""
s = self.settings
if "hang" in s.fault_points:
return # experiment: a hung worker that no longer heartbeats
started = time.monotonic()
while True:
await asyncio.sleep(s.job_lease_seconds / 3)
if time.monotonic() - started > s.job_timeout_seconds:
return
try:
alive = await self.db.run(lambda c: jobs.heartbeat(c, claim, s.job_lease_seconds))
except Unavailable:
continue # the lease may expire meanwhile; the fence protects the result
if not alive:
log.warning("lease_lost", extra={"fields": {"job_id": claim.job_id, "attempt": claim.attempt}})
lost.set()
work.cancel()
return
async def attempt(self, claim: jobs.Claim) -> list[imaging.Rendered]:
s = self.settings
if "hang" in s.fault_points:
await asyncio.sleep(s.fault_value("hang", 3600)) # stuck outside of the time limit
try:
return await asyncio.wait_for(self.build(claim), s.job_timeout_seconds)
except asyncio.TimeoutError:
raise ProcessingError("processing_timeout", permanent=False) from None
async def build(self, claim: jobs.Claim) -> list[imaging.Rendered]:
s = self.settings
started = time.monotonic()
if claim.size_bytes > s.max_upload_bytes:
raise ProcessingError("image_too_large", permanent=True)
source = self.staging if claim.source == "staging" else self.final
try:
data = await source.read_all(claim.source_key, limit=s.max_upload_bytes)
except ObjectMissing:
raise ProcessingError("source_missing", permanent=True) from None
if len(data) != claim.size_bytes or hashlib.sha256(data).digest() != claim.sha256:
raise ProcessingError("source_corrupt", permanent=True)
self.injected(claim)
if "slow" in s.fault_points and s.fault_value("slow_pipeline", claim.pipeline_version) == claim.pipeline_version:
await asyncio.sleep(s.fault_value("slow", 10))
remaining = max(1.0, s.job_timeout_seconds - (time.monotonic() - started))
variants = await imaging.render(data, claim.content_type, claim.pipeline_version,
max_pixels=s.max_image_pixels, timeout=remaining,
memory_mb=s.image_max_memory_mb)
meta = {"attachment-id": claim.attachment_id, "pipeline-version": str(claim.pipeline_version)}
puts = [self.final.put(claim.key(v.name), v.data, v.content_type, meta) for v in variants]
if claim.write_original:
puts.append(self.final.put(claim.key("original"), data, claim.content_type,
{**meta, "sha256": claim.sha256.hex()}))
await asyncio.gather(*puts)
return variants
def injected(self, claim: jobs.Claim) -> None:
s = self.settings
if "permanent_error" in s.fault_points:
raise ProcessingError("invalid_image", permanent=True)
if "transient_error" in s.fault_points and claim.attempt <= s.fault_value("transient_error", 1000):
raise ProcessingError("injected_transient", permanent=False)
# ---------------------------------------------------------------- cleanup
async def delete_staging(self, attachment_id: str) -> None:
"""Mark `deleting` under the row lock (re-checked there), then S3 DELETE, then `deleted`."""
try:
key = await self.db.run(
lambda c: jobs.mark_staging_deleting(c, attachment_id, self.settings.staging_retention_seconds))
if key is None:
return
await self.staging.delete(key)
await self.db.run(lambda c: jobs.mark_staging_deleted(c, attachment_id))
log.info("staging_deleted", extra={"fields": {"attachment_id": attachment_id}})
except Unavailable:
pass # stays present/deleting; the next cleanup pass repeats (DELETE is idempotent)
async def cleanup(self) -> None:
s = self.settings
ids = await self.db.run(lambda c: jobs.staging_to_delete(c, s.staging_retention_seconds))
tokens = await self.db.run(lambda c: jobs.attempts_to_clean(c, s.orphan_grace_seconds))
if "pause_cleanup" in s.fault_points and (ids or tokens):
# Experiment: the race "cleanup read its candidates -> replay commits -> cleanup goes on".
log.warning("cleanup_paused", extra={"fields": {"attachments": ids, "attempts": tokens}})
await asyncio.sleep(s.fault_value("pause_cleanup", 10))
for attachment_id in ids:
await self.delete_staging(attachment_id)
for token in tokens:
keys = await self.db.run(lambda c: jobs.lock_attempt_for_cleanup(c, token))
if keys is None:
continue
await asyncio.gather(*(self.final.delete(k) for k in keys))
await self.db.run(lambda c: jobs.mark_attempt_cleaned(c, token))
log.info("attempt_objects_deleted", extra={"fields": {"token": token, "objects": len(keys)}})
async def cleanup_loop(self, stop: asyncio.Event) -> None:
while not stop.is_set() and self.settings.cleanup_interval_seconds > 0:
await service.sleep_or_stop(stop, self.settings.cleanup_interval_seconds)
try:
await self.cleanup()
except Unavailable:
pass
# ---------------------------------------------------------------- lifecycle
async def run(self) -> int:
stop = service.stop_event()
await self.db.open()
await self.staging.open()
await self.final.open()
cleaner = asyncio.create_task(self.cleanup_loop(stop))
try:
while not stop.is_set():
if self.connection is None or self.connection.is_closed:
await self.disconnect()
try:
await self.connect()
except (aio_pika.exceptions.AMQPError, ConnectionError, OSError, asyncio.TimeoutError) as exc:
log.warning("broker_unavailable", extra={"fields": {"error": type(exc).__name__}})
await self.disconnect()
if self.connection is not None and not self.connection.is_closed:
service.beat()
await service.sleep_or_stop(stop, 2.0)
await self.shutdown()
finally:
cleaner.cancel()
await self.disconnect()
await self.final.close()
await self.staging.close()
await self.db.close()
log.info("worker_stopped")
return 0
async def shutdown(self) -> None:
"""Stop consuming; running attempts get WORKER_SHUTDOWN_SECONDS, the rest is released."""
if self.queue is not None and self.consumer_tag is not None:
try:
await self.queue.cancel(self.consumer_tag)
except Exception: # noqa: BLE001
pass
pending = set(self.tasks)
log.info("worker_draining", extra={"fields": {"in_flight": len(pending)}})
if pending:
_, pending = await asyncio.wait(pending, timeout=self.settings.worker_shutdown_seconds)
for task in pending:
task.cancel()
if pending:
await asyncio.wait(pending, timeout=10)
def main() -> int:
try:
settings = Settings.from_env()
if not settings.broker_url:
raise ConfigError("BROKER_URL_FILE (or BROKER_URL) must be set")
except ConfigError as exc:
print(f"configuration error: {exc}", file=sys.stderr)
return 2
logging_setup.configure(settings.log_level)
log.info("worker_starting", extra={"fields": {
"instance_id": settings.instance_id, "concurrency": settings.worker_concurrency,
"max_attempts": settings.job_max_attempts, "timeout_s": settings.job_timeout_seconds,
"lease_s": settings.job_lease_seconds, "memory_mb": settings.image_max_memory_mb,
"fault_points": sorted(settings.fault_points)}})
return asyncio.run(Worker(settings).run())
if __name__ == "__main__":
sys.exit(main())