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

43 lines
1.5 KiB
Python

import os
import asyncio
from prisma_client import PrismaService
from services.common.service_base_async import ServiceBaseAsync, Job
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
prisma_service = PrismaService()
async def produce(svc: ServiceBaseAsync):
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)
async def handle_comment_publish(svc: ServiceBaseAsync, job: dict):
job_model = Job(**job)
await asyncio.sleep(PROCESS_SEC)
await svc.ack_current()
print("handle_comment_publish Database Consumer from comment:", job_model.task_id)
async def consume_default(svc: ServiceBaseAsync, job: dict):
job_model = Job(**job)
await asyncio.sleep(PROCESS_SEC)
print("consume_default Database Consumer default (DLQ):", job_model.task_id)
await svc.dlq_current(job_model, error="unsupported_routing_key")
if __name__ == "__main__":
svc = ServiceBaseAsync(produce_fn=produce, consume_fn=consume_default, handlers={"parser.comment.publish": handle_comment_publish})
asyncio.run(svc.run())