# __init__.py
import asyncio
from threading import Thread

from app.bot.services.mailing_service.mailing_consumer import AsyncMailingService

def start_mailing_service(app, bot):
    def run():
        loop = asyncio.new_event_loop()
        asyncio.set_event_loop(loop)
        try:
            loop.run_until_complete(_start_async_service(app, bot))
        finally:
            loop.run_until_complete(loop.shutdown_asyncgens())
            loop.close()

    Thread(target=run, name="AsyncMailingServiceThread", daemon=True).start()


async def _start_async_service(app, bot):
    service = AsyncMailingService(app, bot)
    await service.start()