From b38bd86530b19869a5dac31e048e8c56f7d25cee Mon Sep 17 00:00:00 2001 From: Filipe Santos Date: Thu, 24 May 2018 23:23:22 +1200 Subject: [PATCH] Gracefully shutdown --- traktarr.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/traktarr.py b/traktarr.py index 23e4b8c..53f06d7 100755 --- a/traktarr.py +++ b/traktarr.py @@ -2,6 +2,7 @@ import os.path import sys import time +import signal import click import schedule @@ -701,13 +702,19 @@ def init_notifications(): return +# Handles exit signals, cancels jobs and exits cleanly +def exit_handler(signum, frame): + log.info(f"Received {signal.Signals(signum).name}, canceling jobs and exiting.") + schedule.clear() + exit() + + ############################################################ # MAIN ############################################################ if __name__ == "__main__": print(""" - ,--. ,--. ,--. ,-' '-.,--.--. ,--,--.| |,-.,-' '-. ,--,--.,--.--.,--.--. '-. .-'| .--'' ,-. || /'-. .-'' ,-. || .--'| .--' @@ -722,5 +729,11 @@ if __name__ == "__main__": ######################################################################### # GNU General Public License v3.0 # ######################################################################### - """) +""") + + # Register the signal handlers + signal.signal(signal.SIGTERM, exit_handler) + signal.signal(signal.SIGINT, exit_handler) + + # Start application app()