72 lines
2.8 KiB
Python
72 lines
2.8 KiB
Python
import os
|
|
import asyncio
|
|
|
|
from comment_requirements import DefaultImportsToMemory
|
|
from services.common.service_base_async import ServiceBaseAsync
|
|
from services.types.task import Job
|
|
|
|
from prisma_client import PrismaService
|
|
|
|
|
|
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()
|
|
is_db_pulled = False
|
|
|
|
|
|
async def produce(svc: ServiceBaseAsync):
|
|
global is_db_pulled
|
|
|
|
# Get build info to memory
|
|
if not is_db_pulled:
|
|
await default_imports()
|
|
is_db_pulled = True
|
|
async with prisma_service._asession() as db:
|
|
# Routine Email Service
|
|
routine_email_service_result = await db.account_records.find_many(
|
|
where={"is_email_send": False,"active": True, "is_confirmed": True, "deleted": False}, take=3, skip=0
|
|
)
|
|
if not routine_email_service_result:
|
|
await asyncio.sleep(PROCESS_SEC)
|
|
return
|
|
routine_email_service_result: list = prisma_service.to_dict(routine_email_service_result, select={"id": True, "uu_id": True, "iban": True, "bank_reference_code": True, "bank_date": True, "bank_balance": True})
|
|
generate_task__uuid = ""
|
|
for row in routine_email_service_result:
|
|
generate_task__uuid += str(row["uu_id"])[:4]
|
|
await svc.enqueue(task_id=generate_task__uuid, payload=routine_email_service_result, action="routine.email.send.service")
|
|
# Get Build and Company Requirements
|
|
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 handle_routine_email_send_service_ack(svc: ServiceBaseAsync, job: dict):
|
|
job_model = Job(**job)
|
|
await svc.ack_current()
|
|
print("handle_routine_email_send_service_ack Database Consumer from routine.email.send.service:", job_model.task_id)
|
|
return
|
|
|
|
|
|
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")
|
|
|
|
|
|
async def default_imports():
|
|
update_comment_requirements = DefaultImportsToMemory(prisma_service)
|
|
await update_comment_requirements.renew_requirements()
|
|
|
|
if __name__ == "__main__":
|
|
svc = ServiceBaseAsync(produce_fn=produce, consume_fn=consume_default, handlers={"parser.comment.publish": handle_comment_publish, "mail.service.publish": handle_routine_email_send_service_ack})
|
|
asyncio.run(svc.run())
|