import os import asyncio from typing import List from app.services.mail.IsBank.runner import initialize_service from app.services.common.service_base_async import ServiceBaseAsync from .mail_handler import Mails from .IsBank.params import IsBankConfig 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") _produced = False PROCESS_SEC = 10 email_service = initialize_service() def generate_unique_with_mail_id(mail_id: str, service_prefix: str): return f"{service_prefix}_{mail_id}" def process_mail_with_attachments(mail: Mails, mail_id: str): """ Process an email with attachments using MailReaderService Args: mail: Mail object, mail_id: Mail ID Raises: Exception: If processing mail fails """ try: mail_to_dict = mail.to_dict() task_uuid = generate_unique_with_mail_id(mail_id, IsBankConfig.SERVICE_NAME) process_mail_dict = dict(mail_id=mail_id, mail_data=mail_to_dict, service_prefix=email_service.config.SERVICE_PREFIX) return task_uuid, process_mail_dict except Exception as e: print(f"Email Service Runner Error processing mail {mail_id}: {str(e)}") raise def drop(): """Clean up resources""" try: email_service.commit() except Exception as e: print(f"Error during commit on drop: {str(e)}") try: email_service.logout() except Exception as e: print(f"Error during logout on drop: {str(e)}") async def produce(svc: ServiceBaseAsync): mails, count, length = email_service.refresh() for mail in mails: if not getattr(mail, 'id', None): print("Skipping email with no ID") continue mail_id, mail_dict = mail.id.decode('utf-8'), mail.to_dict() try: if mail.attachments: if any([str(attachment['filename']).lower().endswith('.pdf') for attachment in mail_dict['attachments']]): email_service.mark_no_attachment(mail_id) else: task_uuid, process_mail_dict = process_mail_with_attachments(mail, mail_id) await svc.enqueue(task_id=task_uuid, payload=process_mail_dict, type_="mail.service.isbank") else: email_service.mark_no_attachment(mail_id) except Exception as e: print(f"Error processing email {mail_id}: {str(e)}") continue await asyncio.sleep(PROCESS_SEC) async def handle_from_parser(svc: ServiceBaseAsync, job): print("Mail Consumer from parser:", job) await asyncio.sleep(PROCESS_SEC) return async def handle_database_publish(svc: ServiceBaseAsync, job): await asyncio.sleep(PROCESS_SEC) print("Mail Consumer from database:", job) return async def handle_from_mail(svc: ServiceBaseAsync, job): await asyncio.sleep(PROCESS_SEC) print("Mail Consumer from mail:", job) return async def consume_default(svc, job): await asyncio.sleep(PROCESS_SEC) print("Mail Consumer default:", job) return if __name__ == "__main__": svc = ServiceBaseAsync(produce, consume_default, handlers={"parser.publish": handle_from_parser, "mail.publish": handle_from_mail, "database.publish": handle_database_publish} ) asyncio.run(svc.run())