Files
mtusi2026lab5/app/outbox_relay.py

31 lines
896 B
Python

import asyncio
import json
import sys
from app.broker import publish_job
from app.cache import make_cache
from app.db import db
from app.storage import Storage
async def main():
await db.connect()
cache = make_cache()
storage = Storage(db.pool, cache)
print("[outbox] started", flush=True)
while True:
try:
jobs = await storage.get_unpublished_outbox()
for job in jobs:
payload = json.loads(job["payload"]) if isinstance(job["payload"], str) else job["payload"]
await publish_job(payload)
await storage.mark_outbox_published(str(job["id"]))
print(f"[outbox] published {job['job_id']}", flush=True)
except Exception as e:
print(f"[outbox] error: {e}", flush=True)
await asyncio.sleep(1)
if __name__ == "__main__":
sys.exit(asyncio.run(main()))