Skip to content

Commit

Permalink
Fix event loop
Browse files Browse the repository at this point in the history
  • Loading branch information
Benyamin Ginzburg committed Aug 20, 2024
1 parent 6f60410 commit e87e039
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 18 deletions.
24 changes: 8 additions & 16 deletions bus_bot/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,39 +36,31 @@
logger.info(f'Sentry is {"ENABLED" if env.SENTRY_KEY else "DISABLED"}')


async def on_start(bot: Bot):
async def on_start(bot: Bot, watcher_manager: WatcherManager):
logger.info('STARTING BUS BOT...')
await bot.delete_webhook(drop_pending_updates=True)

if env.DOCKER_MODE:
await bot.delete_webhook(drop_pending_updates=True)
await bot.set_webhook(env.WEBHOOK_URL)

if env.METRICS_DSN and env.METRICS_TABLE_NAME:
await aiogram_metrics.register(env.METRICS_DSN, env.METRICS_TABLE_NAME)

await bot.set_my_commands(DEFAULT_COMMANDS)


async def async_main(dp: Dispatcher, bot: Bot):
await bot.delete_webhook()
await dp.start_polling(bot, allowed_updates=ALLOWED_UPDATES)
watcher_manager.run_in_background()


def main():
motor_client = AsyncIOMotorClient(env.DB_URL)
storage = MongoStorage(client=motor_client, db_name=env.DB_NAME, collection_name=env.DB_COLLECTION_NAME)
db_repo = DbRepo(motor_client)

session = httpx.AsyncClient()
wm = WatcherManager()
wm.run_in_background()

dp = Dispatcher(
storage=storage,

# context objects
db_repo=db_repo,
http_session=session,
watcher_manager=wm,
db_repo=DbRepo(motor_client),
http_session=httpx.AsyncClient(),
watcher_manager=WatcherManager(),
)

register_handlers(dp)
Expand All @@ -89,7 +81,7 @@ def main():

web.run_app(app, host=env.WEBAPP_HOST, port=env.WEBAPP_PORT)
else:
asyncio.run(async_main(dp, bot))
asyncio.run(dp.start_polling(bot, allowed_updates=ALLOWED_UPDATES))


if __name__ == '__main__':
Expand Down
3 changes: 1 addition & 2 deletions bus_bot/service/watcher_management_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,7 @@ async def _run_worker(self):
limiter.put()

def run_in_background(self):
loop = asyncio.get_event_loop()
self.__worker = loop.create_task(self._run_worker())
self.__worker = asyncio.create_task(self._run_worker())

async def close(self):
self.__worker.cancel()
Expand Down

0 comments on commit e87e039

Please sign in to comment.