39 lines
1.4 KiB
Python
39 lines
1.4 KiB
Python
"""Один durable Stream, consumer group; ACK только после эффекта/устойчивого retry."""
|
|
import json
|
|
|
|
import redis
|
|
|
|
from src.config import secret
|
|
|
|
STREAM = 'attachments:v1'
|
|
GROUP = 'image-workers'
|
|
|
|
|
|
def connect():
|
|
return redis.Redis.from_url(secret('BROKER_URL'), decode_responses=True,
|
|
socket_connect_timeout=2, socket_timeout=3,
|
|
retry_on_timeout=False)
|
|
|
|
|
|
def bootstrap(client):
|
|
try:
|
|
client.xgroup_create(STREAM, GROUP, id='0', mkstream=True)
|
|
except redis.ResponseError as error:
|
|
if 'BUSYGROUP' not in str(error):
|
|
raise
|
|
|
|
|
|
def publish(client, job):
|
|
payload = {'schema_version': 1, 'job_id': str(job['id']), 'type': 'attachment.process.v1',
|
|
'attachment_id': str(job['attachment_id']), 'pipeline_version': job['pipeline_version'],
|
|
'attempt': job['attempt'], 'created_at': job['created_at'].isoformat().replace('+00:00', 'Z')}
|
|
return client.xadd(STREAM, {'job': json.dumps(payload)})
|
|
|
|
|
|
def acknowledge(client, message_id):
|
|
# Единственная группа: подтверждённые записи можно удалить. Pending не trim-им.
|
|
with client.pipeline(transaction=True) as pipe:
|
|
pipe.xack(STREAM, GROUP, message_id)
|
|
pipe.xdel(STREAM, message_id)
|
|
pipe.execute()
|