Skip to content

Commit

Permalink
feat(root): log signal name and exit with (128+signum) to indicate it
Browse files Browse the repository at this point in the history
Signed-off-by: Rongrong <[email protected]>
  • Loading branch information
Rongronggg9 committed May 25, 2024
1 parent 8a3e225 commit 9a3e198
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,14 +273,22 @@ def force_quit(*_):
os.kill(os.getpid(), signal.SIGKILL)


def sig_handler(signum, *_, **__):
try:
logger.warning(f'Received signal {signal.Signals(signum).name}')
except ValueError:
logger.warning(f'Received signal {signum}')
exit(128 + signum)


def main():
# bot.disconnected usually means the bot is logged out due to a network error or Telegram DC degradation,
# so we should exit with a non-zero code to indicate an error.
# This aims to avoid confusion when running the bot in a container or as a service.
exit_code = 100

try:
signal.signal(signal.SIGTERM, lambda *_, **__: exit()) # graceful exit handler
signal.signal(signal.SIGTERM, sig_handler) # graceful exit handler

init()

Expand Down

0 comments on commit 9a3e198

Please sign in to comment.