Implement durable image processing pipeline with outbox and recovery
This commit is contained in:
@@ -0,0 +1,126 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import logging
|
||||
import time
|
||||
|
||||
import pika
|
||||
|
||||
from app.broker import QUEUE_NAME, connect_broker
|
||||
from app.job_db import connect_db
|
||||
from app.responses import utc_text
|
||||
|
||||
|
||||
logger = logging.getLogger("messenger.publisher")
|
||||
|
||||
|
||||
def publish_one(db, channel) -> bool:
|
||||
with db.transaction():
|
||||
row = db.execute(
|
||||
"""
|
||||
SELECT o.id AS outbox_id,
|
||||
o.job_id,
|
||||
o.attempt,
|
||||
j.attachment_id,
|
||||
j.pipeline_version,
|
||||
j.created_at
|
||||
FROM attachment_outbox AS o
|
||||
JOIN attachment_jobs AS j ON j.id = o.job_id
|
||||
JOIN attachments AS a ON a.id = j.attachment_id
|
||||
WHERE o.published_at IS NULL
|
||||
AND o.available_at <= clock_timestamp()
|
||||
AND j.available_at <= clock_timestamp()
|
||||
AND j.status = 'queued'
|
||||
AND j.attempt = o.attempt
|
||||
AND a.status = 'queued'
|
||||
AND a.pipeline_version = j.pipeline_version
|
||||
ORDER BY o.available_at, o.id
|
||||
LIMIT 1
|
||||
FOR UPDATE OF o SKIP LOCKED
|
||||
"""
|
||||
).fetchone()
|
||||
|
||||
if row is None:
|
||||
return False
|
||||
|
||||
payload = {
|
||||
"schema_version": 1,
|
||||
"job_id": str(row["job_id"]),
|
||||
"type": "attachment.process.v1",
|
||||
"attachment_id": str(row["attachment_id"]),
|
||||
"pipeline_version": row["pipeline_version"],
|
||||
"attempt": row["attempt"],
|
||||
"created_at": utc_text(row["created_at"]),
|
||||
}
|
||||
|
||||
channel.basic_publish(
|
||||
exchange="",
|
||||
routing_key=QUEUE_NAME,
|
||||
body=json.dumps(
|
||||
payload,
|
||||
separators=(",", ":"),
|
||||
).encode("utf-8"),
|
||||
properties=pika.BasicProperties(
|
||||
content_type="application/json",
|
||||
delivery_mode=2,
|
||||
message_id=str(row["outbox_id"]),
|
||||
type="attachment.process.v1",
|
||||
),
|
||||
mandatory=True,
|
||||
)
|
||||
|
||||
db.execute(
|
||||
"""
|
||||
UPDATE attachment_outbox
|
||||
SET published_at = clock_timestamp()
|
||||
WHERE id = %s
|
||||
""",
|
||||
(row["outbox_id"],),
|
||||
)
|
||||
|
||||
logger.info(
|
||||
"published job_id=%s attempt=%s",
|
||||
row["job_id"],
|
||||
row["attempt"],
|
||||
)
|
||||
return True
|
||||
|
||||
|
||||
def main():
|
||||
logging.basicConfig(
|
||||
level=logging.INFO,
|
||||
format="%(asctime)s %(levelname)s %(message)s",
|
||||
)
|
||||
|
||||
while True:
|
||||
broker = None
|
||||
try:
|
||||
broker, channel = connect_broker()
|
||||
channel.confirm_delivery()
|
||||
|
||||
with connect_db() as db:
|
||||
logger.info("publisher connected")
|
||||
while True:
|
||||
if not publish_one(db, channel):
|
||||
broker.process_data_events(time_limit=0.5)
|
||||
|
||||
except KeyboardInterrupt:
|
||||
return
|
||||
|
||||
except Exception as exc:
|
||||
logger.warning(
|
||||
"publisher reconnect error_type=%s",
|
||||
type(exc).__name__,
|
||||
)
|
||||
time.sleep(2)
|
||||
|
||||
finally:
|
||||
if broker is not None and broker.is_open:
|
||||
try:
|
||||
broker.close()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user