forked from mtusi2026/mtusi2026lab5
81 lines
2.6 KiB
Python
81 lines
2.6 KiB
Python
#!/usr/bin/env python3
|
|
import logging
|
|
import os
|
|
import time
|
|
import uuid
|
|
|
|
import pika
|
|
import psycopg
|
|
|
|
from .jobs import LEASE_NETWORK_BUDGET_SECONDS, QUEUE_NAME, connect, declare_queues, encode_job
|
|
from .security import isoformat
|
|
from .store import Store
|
|
|
|
LOGGER = logging.getLogger("messenger.publisher")
|
|
|
|
|
|
def publish_pending(store: Store, channel: pika.adapters.blocking_connection.BlockingChannel) -> bool:
|
|
with store.pool.connection() as conn:
|
|
with conn.transaction():
|
|
row = store.claim_outbox(conn)
|
|
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": isoformat(row["created_at"]),
|
|
}
|
|
channel.basic_publish(
|
|
exchange="",
|
|
routing_key=QUEUE_NAME,
|
|
body=encode_job(payload),
|
|
properties=pika.BasicProperties(
|
|
content_type="application/json",
|
|
delivery_mode=2,
|
|
message_id=str(row["id"]),
|
|
type="attachment.process.v1",
|
|
),
|
|
mandatory=True,
|
|
)
|
|
conn.execute(
|
|
"UPDATE attachment_outbox SET published_at = clock_timestamp() WHERE id = %s",
|
|
(row["id"],),
|
|
)
|
|
return True
|
|
|
|
|
|
def main() -> None:
|
|
logging.basicConfig(level=os.getenv("LOG_LEVEL", "INFO"))
|
|
store = Store()
|
|
store.open()
|
|
try:
|
|
while True:
|
|
connection = None
|
|
try:
|
|
connection = connect()
|
|
channel = connection.channel()
|
|
declare_queues(channel)
|
|
channel.confirm_delivery()
|
|
store.recover_expired_attachment_jobs(
|
|
int(os.getenv("JOB_MAX_ATTEMPTS", "5")),
|
|
int(os.getenv("JOB_TIMEOUT_SECONDS", "60")) + LEASE_NETWORK_BUDGET_SECONDS,
|
|
)
|
|
if not publish_pending(store, channel):
|
|
time.sleep(1)
|
|
except (OSError, pika.exceptions.AMQPError, psycopg.Error):
|
|
LOGGER.exception("outbox publishing cycle failed")
|
|
time.sleep(2)
|
|
finally:
|
|
if connection is not None and connection.is_open:
|
|
connection.close()
|
|
finally:
|
|
store.close()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|