Allow graceful exit when shutting down in docker

This commit is contained in:
Jon 2021-11-22 21:51:32 -05:00
parent 4f52fd49c8
commit 7a6603b952
No known key found for this signature in database
GPG key ID: 9665BA6CF5DC2671
3 changed files with 27 additions and 4 deletions

5
.gitignore vendored
View file

@ -1,3 +1,8 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
*.log*
*.yml
.vscode/*

11
modules/docker.py Normal file
View file

@ -0,0 +1,11 @@
import signal
#Gracefully kill script when docker stops
class GracefulKiller:
kill_now = False
def __init__(self):
signal.signal(signal.SIGINT, self.exit_gracefully)
signal.signal(signal.SIGTERM, self.exit_gracefully)
def exit_gracefully(self, *args):
self.kill_now = True

View file

@ -19,10 +19,12 @@ try:
from qbittorrentapi import Client
import yaml
import schedule
from modules.docker import GracefulKiller
except ModuleNotFoundError:
print("Requirements Error: Requirements are not installed")
sys.exit(0)
if sys.version_info[0] != 3 or sys.version_info[1] < 6:
print("Version Error: Version: %s.%s.%s incompatible please use Python 3.6+" % (sys.version_info[0], sys.version_info[1], sys.version_info[2]))
sys.exit(0)
@ -187,7 +189,6 @@ client = Client(host=host,
############FUNCTIONS##############
#truncate the value of the torrent url to remove sensitive information
def trunc_val(s, d, n=3):
return d.join(s.split(d, n)[:n])
@ -839,7 +840,12 @@ def start():
set_tag_nohardlinks()
set_empty_recycle()
def end():
logger.info("Exiting Qbit_manage")
sys.exit(0)
if __name__ == '__main__':
killer = GracefulKiller()
logger.info(" _ _ _ ")
logger.info(" | | (_) | ")
logger.info(" __ _| |__ _| |_ _ __ ___ __ _ _ __ __ _ __ _ ___ ")
@ -857,8 +863,9 @@ if __name__ == '__main__':
schedule.every(sch).minutes.do(start)
logger.info(f" Scheduled Mode: Running every {sch} minutes.")
start()
while True:
while not killer.kill_now:
schedule.run_pending()
time.sleep(60)
time.sleep(1)
end()
except KeyboardInterrupt:
logger.info("Exiting Qbit_manage")
end()