Files
mtusi2026lab5/src/outbox.py
T
2026-10-26 10:29:54 +03:00

34 lines
1.0 KiB
Python

"""Transactional-outbox dispatcher; safe to run as one or more replicas."""
import asyncio
import logging
import os
from .broker import publish
from .config import load_settings
from .storage.postgres import PostgresStore
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s outbox %(message)s")
async def run() -> None:
settings = load_settings()
store = PostgresStore(settings.database_url)
await store.connect()
interval = max(0.1, float(os.environ.get("OUTBOX_DISPATCH_INTERVAL_SECONDS", "1")))
try:
while True:
try:
await store.schedule_due_jobs()
for event in await store.due_outbox_events():
await publish(event["payload"])
await store.mark_outbox_published(event["id"])
except Exception:
logging.exception("outbox dispatch failed; durable event remains pending")
await asyncio.sleep(interval)
finally:
await store.close()
if __name__ == "__main__":
asyncio.run(run())