From b353c043382049aca2f7e9a6bb6b4fb7456309be Mon Sep 17 00:00:00 2001 From: Matteo Bertucci Date: Sat, 30 Jan 2021 18:27:02 +0100 Subject: [PATCH] Gracefully exit the application --- cloudflare_ddns/app.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/cloudflare_ddns/app.py b/cloudflare_ddns/app.py index 0c614ec..6c02692 100644 --- a/cloudflare_ddns/app.py +++ b/cloudflare_ddns/app.py @@ -1,6 +1,7 @@ import logging import threading from dataclasses import dataclass +from signal import SIGINT, SIGTERM, signal from typing import List import requests @@ -51,6 +52,9 @@ class ApplicationJob(threading.Thread): def launch(self) -> None: """Launch the application by validating arguments and starting the thread.""" self.validate_arguments() + log.debug("Registering exit hooks.") + signal(SIGINT, self.exit) + signal(SIGTERM, self.exit) log.debug("Starting job.") self.start() @@ -193,3 +197,9 @@ class ApplicationJob(threading.Thread): if not r.json()["success"]: error_message = ' / '.join(error["message"] for error in r.json()["errors"]) raise ValueError(error_message) + + def exit(self, *_) -> None: + """Gracefully exit the application.""" + log.info("Exiting application.") + self.stop_signal.set() + self.join()