Skeleton contracts / contracts (pull_request) Canceled after 0s
50 lines
1.3 KiB
Python
50 lines
1.3 KiB
Python
"""Redis Streams durable job broker (at-least-once, consumer group, no bytes in messages)."""
|
|
import json
|
|
import logging
|
|
import os
|
|
|
|
log = logging.getLogger("messenger.broker")
|
|
|
|
client = None
|
|
STREAM = "attachment.jobs"
|
|
GROUP = "attachment-workers"
|
|
|
|
|
|
def start():
|
|
global client
|
|
import redis
|
|
import ssl
|
|
client = redis.Redis(
|
|
host=os.environ.get("BROKER_HOST", "redis"),
|
|
port=int(os.environ.get("BROKER_PORT", "6379")),
|
|
db=0,
|
|
decode_responses=True,
|
|
socket_connect_timeout=3,
|
|
socket_timeout=5,
|
|
password=os.environ.get("REDIS_PASSWORD"),
|
|
ssl=True,
|
|
ssl_cert_reqs=ssl.CERT_REQUIRED,
|
|
ssl_ca_certs=os.environ.get("TLS_CA_FILE", "/certs/ca.crt"),
|
|
)
|
|
client.ping()
|
|
try:
|
|
client.xgroup_create(STREAM, GROUP, id="0", mkstream=True)
|
|
except redis.ResponseError as exc:
|
|
if "BUSYGROUP" not in str(exc):
|
|
raise
|
|
log.info("broker ready: stream=%s group=%s", STREAM, GROUP)
|
|
|
|
|
|
def publish(job):
|
|
"""Publisher confirm = the XADD reply id. The job carries IDs, never bytes or secrets."""
|
|
return client.xadd(STREAM, {"job": json.dumps(job, ensure_ascii=False)})
|
|
|
|
|
|
def ready():
|
|
try:
|
|
client.ping()
|
|
return True
|
|
except Exception:
|
|
log.exception("broker readiness check failed")
|
|
return False
|