updated Services Task database
This commit is contained in:
@@ -11,6 +11,7 @@ COPY app/services/mail/README.md ./
|
||||
COPY app/core ./app/core
|
||||
COPY app/services/common/ ./app/services/common/
|
||||
COPY app/services/mail/ ./app/services/mail/
|
||||
COPY app/services/types/ ./app/services/types/
|
||||
|
||||
RUN pip install --upgrade pip && pip install --no-cache-dir .
|
||||
RUN mkdir -p /app/data
|
||||
|
||||
@@ -9,6 +9,7 @@ class IsBankConfig:
|
||||
NO_ATTACHMENT_FOLDER: str = "NoAttachment"
|
||||
COMPLETED_FOLDER: str = "Completed"
|
||||
SERVICE_NAME: str = "IsBankEmailService"
|
||||
BANK_NAME: str = "IsBank"
|
||||
TASK_DATA_PREFIX: str = ConfigServices.MAIN_TASK_PREFIX
|
||||
TASK_MAILID_INDEX_PREFIX: str = ConfigServices.TASK_MAILID_INDEX_PREFIX
|
||||
TASK_UUID_INDEX_PREFIX: str = ConfigServices.TASK_UUID_INDEX_PREFIX
|
||||
|
||||
@@ -13,16 +13,28 @@ basicConfig(level=INFO, format=format, handlers=handlers)
|
||||
logger = getLogger(IsBankConfig.SERVICE_NAME)
|
||||
|
||||
|
||||
def initialize_service():
|
||||
def drop(email_service: EmailReaderService):
|
||||
"""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)}")
|
||||
|
||||
|
||||
def initialize_service() -> EmailReaderService:
|
||||
"""Initialize the service with proper error handling"""
|
||||
try:
|
||||
logger.info("Creating EmailReaderService")
|
||||
email_service = EmailReaderService(IsBankConfig())
|
||||
|
||||
logger.info("Connecting to email service")
|
||||
email_service.login_and_connect()
|
||||
return email_service
|
||||
except Exception as e:
|
||||
drop(email_service)
|
||||
logger.error(f"Service initialization failed: {str(e)}")
|
||||
sleep(5)
|
||||
return initialize_service()
|
||||
|
||||
@@ -1,15 +0,0 @@
|
||||
import asyncio
|
||||
import uuid
|
||||
from services.service_base_async import ServiceBaseAsync
|
||||
|
||||
async def produce(service: ServiceBaseAsync):
|
||||
fake_mails = [{"to": "user@example.com", "subj": "Hi", "body": "Hello!"}]
|
||||
for mail in fake_mails:
|
||||
await service.enqueue(mail, "send-mail")
|
||||
|
||||
async def consume(service: ServiceBaseAsync, job: dict):
|
||||
print(f"[MAIL] Gönderiliyor: {job}")
|
||||
await asyncio.sleep(0.1)
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(ServiceBaseAsync(produce, consume).run())
|
||||
@@ -1,12 +1,14 @@
|
||||
import os
|
||||
import asyncio
|
||||
from typing import List
|
||||
|
||||
from app.services.mail.IsBank.runner import initialize_service
|
||||
from app.services.mail.mail_handler import Mails
|
||||
from app.services.mail.IsBank.params import IsBankConfig
|
||||
from app.services.common.service_base_async import ServiceBaseAsync
|
||||
|
||||
from .mail_handler import Mails
|
||||
from .IsBank.params import IsBankConfig
|
||||
from app.services.types.queue import Enqueue
|
||||
from app.services.types.mail import MailParsedResult, ProcessMailObject
|
||||
from app.services.types.task import Job
|
||||
|
||||
|
||||
PRODUCE_BURST = int(os.getenv("PRODUCE_BURST", "10"))
|
||||
@@ -22,7 +24,7 @@ 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):
|
||||
def process_mail_with_attachments(mail: Mails, mail_id: str, count: int, total: int) -> ProcessMailObject:
|
||||
"""
|
||||
Process an email with attachments using MailReaderService
|
||||
Args: mail: Mail object, mail_id: Mail ID
|
||||
@@ -31,74 +33,70 @@ def process_mail_with_attachments(mail: Mails, mail_id: str):
|
||||
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
|
||||
return ProcessMailObject(uuid=task_uuid, id=mail_id, data=mail_to_dict, service=email_service.config.SERVICE_PREFIX, count=count, total=total, attachments=mail.attachments)
|
||||
except Exception as e:
|
||||
print(f"Email Service Runner Error processing mail {mail_id}: {str(e)}")
|
||||
raise
|
||||
raise Exception(f"Email Service Runner Error processing mail {mail_id}: {str(e)}")
|
||||
|
||||
|
||||
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):
|
||||
# Isbank producer mail Reader
|
||||
async def produce(svc: ServiceBaseAsync):
|
||||
mails, count, length = email_service.refresh()
|
||||
if not mails:
|
||||
await asyncio.sleep(PROCESS_SEC)
|
||||
return
|
||||
for mail in mails:
|
||||
if not getattr(mail, 'id', None):
|
||||
print("Skipping email with no ID")
|
||||
print(f"Skipping email with no ID: {mail.subject}")
|
||||
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)
|
||||
is_attachment_pdf = any([str(attachment['filename']).lower().endswith('.pdf') for attachment in mail_dict['attachments']])
|
||||
if not is_attachment_pdf:
|
||||
process_mail_object = process_mail_with_attachments(mail, mail_id, count, length)
|
||||
enqueue = Enqueue(task_id=process_mail_object.uuid, payload=process_mail_object.model_dump(), action=IsBankConfig.SERVICE_NAME)
|
||||
await svc.enqueue(enqueue)
|
||||
await svc.ack_current()
|
||||
print(f"Mail Consumer from parser with attachments : {mail_id}")
|
||||
continue
|
||||
print(f"Mail Consumer from parser with no attachments : {mail_id}")
|
||||
email_service.mark_no_attachment(mail_id)
|
||||
await svc.ack_current()
|
||||
except Exception as e:
|
||||
print(f"Error processing email {mail_id}: {str(e)}")
|
||||
await svc.retry_current()
|
||||
continue
|
||||
await asyncio.sleep(PROCESS_SEC)
|
||||
|
||||
|
||||
async def handle_from_parser(svc: ServiceBaseAsync, job):
|
||||
print("Mail Consumer from parser:", job)
|
||||
job_model = Job(**job)
|
||||
await svc.ack_current()
|
||||
print("Mail Consumer from parser :", job_model.model_dump())
|
||||
await asyncio.sleep(PROCESS_SEC)
|
||||
return
|
||||
|
||||
|
||||
async def handle_database_publish(svc: ServiceBaseAsync, job):
|
||||
async def handle_parser_excel(svc: ServiceBaseAsync, job):
|
||||
job_model = Job(**job)
|
||||
parsed_result = MailParsedResult(**job_model.payload)
|
||||
if parsed_result.send_to == "Completed":
|
||||
print("Mail Consumer from parser excel :", parsed_result.mail_data.id)
|
||||
email_service.mark_completed(parsed_result.mail_data.id)
|
||||
await svc.ack_current()
|
||||
await asyncio.sleep(PROCESS_SEC)
|
||||
print("Mail Consumer from database:", job)
|
||||
return
|
||||
|
||||
|
||||
async def handle_from_mail(svc: ServiceBaseAsync, job):
|
||||
async def consume_default(svc: ServiceBaseAsync, job):
|
||||
job_model = Job(**job)
|
||||
await svc.dlq_current()
|
||||
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)
|
||||
print("Mail Consumer default:", job_model.model_dump())
|
||||
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}
|
||||
)
|
||||
svc = ServiceBaseAsync(produce, consume_default, handlers={"parser.comment.publish": handle_from_parser, "parser.excel.publish": handle_parser_excel})
|
||||
asyncio.run(svc.run())
|
||||
|
||||
Reference in New Issue
Block a user