"""Persistent Valkey Streams transport; database outbox remains the durable intent.""" import json import os import redis STREAM = 'attachment-jobs' GROUP = 'image-workers' def connect(): return redis.Redis.from_url(os.environ['BROKER_URL'], decode_responses=True, socket_connect_timeout=2, socket_timeout=3) def ensure_group(client): try: client.xgroup_create(STREAM, GROUP, id='0', mkstream=True) except redis.ResponseError as exc: if not str(exc).startswith('BUSYGROUP'): raise def publish(client, payload): # appendfsync always in Compose: successful XADD is the broker acceptance # boundary. This is NOT an atomic transaction with PostgreSQL's outbox update. return client.xadd(STREAM, {'job': json.dumps(payload, separators=(',', ':'))}) def receive(client, consumer): ensure_group(client) claimed = client.xautoclaim(STREAM, GROUP, consumer, min_idle_time=65_000, start_id='0-0', count=1) if claimed[1]: return claimed[1][0] items = client.xreadgroup(GROUP, consumer, {STREAM: '>'}, count=1, block=1000) return items[0][1][0] if items else None def acknowledge(client, message_id): # Called only after durable terminal/no-op outcome (or durable scheduled retry). # One consumer group owns this stream. Ack+delete prevents unbounded history. with client.pipeline(transaction=True) as pipe: pipe.xack(STREAM, GROUP, message_id) pipe.xdel(STREAM, message_id) pipe.execute()