Реализовать устойчивый приём вложений, брокер и обработку миниатюр
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
"""Отдельный relay: lease публикации, publisher confirm, восстановление jobs."""
|
||||
|
||||
import logging
|
||||
import signal
|
||||
import threading
|
||||
import time
|
||||
|
||||
import pika
|
||||
import psycopg
|
||||
from psycopg_pool import PoolTimeout
|
||||
|
||||
from src import broker, jobs
|
||||
from src.storage import storage
|
||||
|
||||
|
||||
def main():
|
||||
logging.basicConfig(level=logging.WARNING)
|
||||
stopping = threading.Event()
|
||||
for sig in (signal.SIGINT, signal.SIGTERM):
|
||||
signal.signal(sig, lambda *_: stopping.set())
|
||||
storage.pool.open(wait=True)
|
||||
next_recovery = 0.0
|
||||
try:
|
||||
while not stopping.is_set():
|
||||
reserved = None
|
||||
try:
|
||||
with broker.channel(heartbeat=10) as (connection, channel):
|
||||
channel.confirm_delivery()
|
||||
while not stopping.is_set():
|
||||
if time.monotonic() >= next_recovery:
|
||||
jobs.recover_expired()
|
||||
next_recovery = time.monotonic() + 1
|
||||
reserved = jobs.reserve_publish()
|
||||
if reserved:
|
||||
broker.publish(channel, jobs.envelope(reserved))
|
||||
jobs.confirm_publish(reserved)
|
||||
reserved = None
|
||||
else:
|
||||
connection.process_data_events(time_limit=0.2)
|
||||
except (pika.exceptions.AMQPError, OSError, psycopg.Error, PoolTimeout) as exc:
|
||||
logging.warning('dispatch temporarily unavailable: %s', type(exc).__name__)
|
||||
if reserved:
|
||||
try:
|
||||
jobs.publish_failed(reserved)
|
||||
except (psycopg.Error, PoolTimeout):
|
||||
pass # expires through the durable publication deadline
|
||||
stopping.wait(1)
|
||||
finally:
|
||||
storage.pool.close()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in New Issue
Block a user