"""Transactional outbox publisher and expired-lease watchdog, independently restartable.""" import signal import time from pathlib import Path import redis from messenger.broker import Broker, Job from messenger.domain import DomainError from messenger.faults import checkpoint from messenger.pipeline import Pipeline, event from messenger.repository import PostgresRepository, stamp from messenger.settings import Settings def publish_one(db, broker, settings): with db.connection(write=True) as conn: row = conn.execute( "SELECT o.job_id,j.attachment_id,j.pipeline_version,j.attempt,j.created_at " "FROM job_outbox o JOIN attachment_jobs j USING(job_id) " "WHERE o.published_at IS NULL AND o.due_at<=clock_timestamp() AND j.state='queued' " "ORDER BY o.due_at LIMIT 1 FOR UPDATE OF o SKIP LOCKED" ).fetchone() if not row: return False job = Job( schema_version=1, type="attachment.process.v1", job_id=str(row["job_id"]), attachment_id=str(row["attachment_id"]), pipeline_version=row["pipeline_version"], attempt=row["attempt"] + 1, created_at=stamp(row["created_at"]), ) stream_id = broker.publish(job) # Commit-on-return may be lost here; repeat sends the same logical job_id. checkpoint(db, settings, row["job_id"], "after_publish") conn.execute( "UPDATE job_outbox SET published_at=clock_timestamp(),stream_id=%s WHERE job_id=%s", (stream_id, row["job_id"]), ) event(conn, row["job_id"], "published", stream_id=stream_id, attempt=job.attempt) return True def main(): settings = Settings.from_env() db = PostgresRepository(settings.database_url, pool_size=4) db.open() broker = Broker(settings) pipeline = Pipeline(db, settings) running = True def stop(*_): nonlocal running running = False signal.signal(signal.SIGTERM, stop) signal.signal(signal.SIGINT, stop) try: while running: try: pipeline.recover() sent = publish_one(db, broker, settings) Path("/tmp/relay-heartbeat").touch() if not sent: time.sleep(0.2) except redis.RedisError, DomainError: # Keep the transaction uncommitted and retry. No credentials/SQL in logs. time.sleep(0.5) finally: broker.close() db.close() if __name__ == "__main__": main()