Merge pull request #9 from jhollowe/logging-and-signals

Fixed logging and exit errors
This commit is contained in:
Esteban Sanchez 2021-07-17 23:18:00 +02:00 committed by GitHub
commit 039f7a7ef4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

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 get_config_value(key, default=""): def get_config_value(key, default=""):
input_path = os.environ.get("FILE__" + key, None) input_path = os.environ.get("FILE__" + key, None)
@ -185,7 +188,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("INFO") # default until config is loaded logger.setLevel("INFO") # default until config is loaded