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
This commit is contained in:
John Hollowell 2021-07-16 22:59:50 +00:00
parent 0ff6a56f18
commit a25005b6a0

View file

@ -14,7 +14,7 @@ from pythonjsonlogger import jsonlogger
# Enable dumps on stderr in case of segfault # Enable dumps on stderr in case of segfault
faulthandler.enable() faulthandler.enable()
logger = None logger = logging.getLogger()
class QbittorrentMetricsCollector(): class QbittorrentMetricsCollector():
@ -152,18 +152,21 @@ class QbittorrentMetricsCollector():
class SignalHandler(): class SignalHandler():
def __init__(self): def __init__(self):
self.shutdown = False self.shutdownCount = 0
# Register signal handler # Register signal handler
signal.signal(signal.SIGINT, self._on_signal_received) signal.signal(signal.SIGINT, self._on_signal_received)
signal.signal(signal.SIGTERM, self._on_signal_received) signal.signal(signal.SIGTERM, self._on_signal_received)
def is_shutting_down(self): def is_shutting_down(self):
return self.shutdown return self.shutdownCount > 0
def _on_signal_received(self, signal, frame): 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") logger.info("Exporter is shutting down")
self.shutdown = True self.shutdownCount += 1
def main(): def main():
@ -187,7 +190,6 @@ def main():
datefmt="%Y-%m-%d %H:%M:%S" datefmt="%Y-%m-%d %H:%M:%S"
) )
logHandler.setFormatter(formatter) logHandler.setFormatter(formatter)
logger = logging.getLogger()
logger.addHandler(logHandler) logger.addHandler(logHandler)
logger.setLevel(config["log_level"]) logger.setLevel(config["log_level"])