From a25005b6a0b0966cd3a9eecae0876e8502d20295 Mon Sep 17 00:00:00 2001 From: John Hollowell Date: Fri, 16 Jul 2021 22:59:50 +0000 Subject: [PATCH] Fixed logging and exit errors * Create logger in module so it is always available (still configured in main()) * Allow signals sent multiple times to forcibly kill the exporter --- qbittorrent_exporter/exporter.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/qbittorrent_exporter/exporter.py b/qbittorrent_exporter/exporter.py index faa5caf..af5a9a5 100644 --- a/qbittorrent_exporter/exporter.py +++ b/qbittorrent_exporter/exporter.py @@ -14,7 +14,7 @@ from pythonjsonlogger import jsonlogger # Enable dumps on stderr in case of segfault faulthandler.enable() -logger = None +logger = logging.getLogger() class QbittorrentMetricsCollector(): @@ -152,18 +152,21 @@ class QbittorrentMetricsCollector(): class SignalHandler(): def __init__(self): - self.shutdown = False + self.shutdownCount = 0 # Register signal handler signal.signal(signal.SIGINT, self._on_signal_received) signal.signal(signal.SIGTERM, self._on_signal_received) def is_shutting_down(self): - return self.shutdown + return self.shutdownCount > 0 def _on_signal_received(self, signal, frame): + if self.shutdownCount > 1: + logger.warn("Forcibly killing exporter") + sys.exit(1) logger.info("Exporter is shutting down") - self.shutdown = True + self.shutdownCount += 1 def main(): @@ -187,7 +190,6 @@ def main(): datefmt="%Y-%m-%d %H:%M:%S" ) logHandler.setFormatter(formatter) - logger = logging.getLogger() logger.addHandler(logHandler) logger.setLevel(config["log_level"])