class OrderProcessor:
def __init__(self):
self.processed_orders = set()
 
async def handle_message(self, msg, attempts: int):
order = json.loads(msg.value)
if order["id"] in self.processed_orders:
return # дедупликация
# stmt = (select(inbox).where(inbox.order_id == order_id))
# order = self.db.scalar(stmt)
# if order is None: New
# if order.status == Error:
async with httpx.AsyncClient() as client:
for attempt in range(attempts):
try:
resp = await client.post("http://payment-service/charge", json=order)
if 300 <= resp.status_code < 400
raise HttpExeception()
if resp.status_code > 400
raise
return
except Exception as e:
...
self.processed_orders.add(order["id"])
await consumer.commit()
 
async def run(self):
async for msg in consumer:
asyncio.create_task(self.handle_message(msg))