#!/usr/bin/env python3 """Durable Stage 2 pipeline: adapter → outbox → JetStream → idempotent consumer.""" import asyncio import signal import time from adapter_plugins.registry import run_all from event_store import connect, init from nats_relay import consume_once, relay_once STOP = False def _stop(*_): global STOP STOP = True def main(): signal.signal(signal.SIGTERM, _stop) signal.signal(signal.SIGINT, _stop) conn = connect() init(conn) conn.close() print("events-pipeline start", flush=True) while not STOP: try: conn = connect() init(conn) emitted = run_all(conn) conn.close() relayed = asyncio.run(relay_once()) consumed = asyncio.run(consume_once(timeout=2)) print("tick emitted=%s relayed=%s consumed=%s" % (emitted, relayed, consumed), flush=True) except Exception as e: print("tick_error", str(e)[:300], flush=True) for _ in range(5): if STOP: break time.sleep(1) if __name__ == "__main__": main()