From 8959277f90a507770f987ebd0442ed1ae69af344 Mon Sep 17 00:00:00 2001 From: Mikhail Sandakov Date: Mon, 9 Sep 2024 09:34:09 +0300 Subject: [PATCH] Invoke the exit function to delete the lock file and release resources upon receiving signals --- pleskdistup/main.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/pleskdistup/main.py b/pleskdistup/main.py index bc3a4f3..348ac64 100644 --- a/pleskdistup/main.py +++ b/pleskdistup/main.py @@ -6,6 +6,7 @@ import locale import logging import os +import signal import sys import time import traceback @@ -317,6 +318,12 @@ def try_lock(lock_file: PathType) -> typing.Generator[bool, None, None]: log.warn(f"Failed to remove lockfile {lock_file!r}: {ex}") +def exit_signal_handler(signum, frame): + # exit will trigger blocks finalization, so lockfile will be removed + log.info(f"Received signal {signum}, going to exit...") + sys.exit(1) + + DESC_MESSAGE = """Use this utility to dist-upgrade your server with Plesk. The utility writes a log to the file specified by --log-file. If there are any issues, you can find more information in the log file. @@ -406,6 +413,10 @@ def main(): else: options.help = False + # signals handler initialization + for signum in (signal.SIGINT, signal.SIGTERM, signal.SIGHUP, signal.SIGABRT): + signal.signal(signum, exit_signal_handler) + # Configure locale to avoid problems on systems where LANG or LC_CTYPE changed, # while files on the system still has utf-8 encoding # We should do it before initializing logger to make sure utf-8 symbols will be