Gracefully exit the application

This commit is contained in:
Matteo Bertucci
2021-01-30 18:27:02 +01:00
parent dc284f7a70
commit b353c04338

View File

@ -1,6 +1,7 @@
import logging import logging
import threading import threading
from dataclasses import dataclass from dataclasses import dataclass
from signal import SIGINT, SIGTERM, signal
from typing import List from typing import List
import requests import requests
@ -51,6 +52,9 @@ class ApplicationJob(threading.Thread):
def launch(self) -> None: def launch(self) -> None:
"""Launch the application by validating arguments and starting the thread.""" """Launch the application by validating arguments and starting the thread."""
self.validate_arguments() self.validate_arguments()
log.debug("Registering exit hooks.")
signal(SIGINT, self.exit)
signal(SIGTERM, self.exit)
log.debug("Starting job.") log.debug("Starting job.")
self.start() self.start()
@ -193,3 +197,9 @@ class ApplicationJob(threading.Thread):
if not r.json()["success"]: if not r.json()["success"]:
error_message = ' / '.join(error["message"] for error in r.json()["errors"]) error_message = ' / '.join(error["message"] for error in r.json()["errors"])
raise ValueError(error_message) raise ValueError(error_message)
def exit(self, *_) -> None:
"""Gracefully exit the application."""
log.info("Exiting application.")
self.stop_signal.set()
self.join()