41 lines
1.3 KiB
Python
41 lines
1.3 KiB
Python
import sys
|
|
|
|
from time import sleep
|
|
from logging import getLogger, basicConfig, INFO, StreamHandler, FileHandler
|
|
|
|
from ..mail_handler import EmailReaderService
|
|
from .params import IsBankConfig
|
|
|
|
|
|
format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
|
|
handlers = [StreamHandler(sys.stdout), FileHandler('isbank_email_service.log')]
|
|
basicConfig(level=INFO, format=format, handlers=handlers)
|
|
logger = getLogger(IsBankConfig.SERVICE_NAME)
|
|
|
|
|
|
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()
|
|
|