production-evyos-systems-an.../ServicesTask/app/services/database/main.py

49 lines
1.8 KiB
Python

import os
import asyncio
from prisma_client import PrismaService
from services.common.service_base_async import ServiceBaseAsync
PRODUCE_BURST = int(os.getenv("PRODUCE_BURST", "10"))
PRODUCE_ONCE = os.getenv("PRODUCE_ONCE", "true").lower() == "true"
EVENT_TYPE = os.getenv("EVENT_TYPE", "db-event")
PROCESS_SEC = 10
async def produce(svc: ServiceBaseAsync):
prisma_service = PrismaService()
async with prisma_service._asession() as db:
result = await db.account_records.find_many(take=10, skip=0)
result: list = prisma_service.to_dict(result, select={"id": True, "uu_id": True, "iban": True, "bank_reference_code": True, "bank_date": True, "bank_balance": True})
for row in result:
await svc.enqueue(task_id=row["uu_id"], payload=row, type_="database.account.records")
await asyncio.sleep(PROCESS_SEC)
print(f"Produced From Database Producer: {len(result)} events to '{svc.produce_key}")
async def handle_mail_publish(svc: ServiceBaseAsync, job):
await asyncio.sleep(PROCESS_SEC)
await svc.ack_current()
print("Database Consumer from mail:", job)
async def handle_mongo_publish(svc: ServiceBaseAsync, job):
prisma_service = PrismaService()
await asyncio.sleep(PROCESS_SEC)
await svc.ack_current()
print("Database Consumer from mongo:", job)
async def consume_default(svc: ServiceBaseAsync, job):
prisma_service = PrismaService()
await asyncio.sleep(PROCESS_SEC)
print("Database Consumer default (DLQ):", job.get("task_id"))
await svc.dlq_current(job, error="unsupported_routing_key")
if __name__ == "__main__":
svc = ServiceBaseAsync(produce_fn=produce, consume_fn=consume_default, handlers={"mail.service.publish": handle_mail_publish, "mongo.service.publish": handle_mongo_publish})
asyncio.run(svc.run())