Skip to content

Commit

Permalink
Fix sigint exit
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertoBochet committed Jun 26, 2024
1 parent 396870a commit 3d45491
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions scraper_bot/__main__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#!/usr/bin/env python3
import asyncio
import json
import logging.config
import signal
import sys
from argparse import ArgumentParser
from asyncio import CancelledError, create_task
from signal import SIGINT

from pydantic import ValidationError

Expand All @@ -13,8 +14,6 @@


def main() -> int:
signal.signal(signal.SIGINT, lambda: sys.exit(0))

# loads logger config
setup_default_logger()

Expand Down Expand Up @@ -78,11 +77,25 @@ def main() -> int:

LOGGER.info("bot_scraper is ready to start")

# starts bot
bot.start()
if not settings.daemonize:
asyncio.run(bot.run_once())
return 0

async def daemonize():
LOGGER.info("Starting daemon")
task = create_task(bot.run())

task.get_loop().add_signal_handler(SIGINT, task.cancel)

try:
await task
except CancelledError:
LOGGER.info("Daemon has been stopped")

# starts bot as daemon
asyncio.run(daemonize())
return 0


if __name__ == "__main__":
sys.exit(main())
exit(main())

0 comments on commit 3d45491

Please sign in to comment.