From 35d38df9d32e1159372baf5ef8ffbead166f7688 Mon Sep 17 00:00:00 2001 From: jo Date: Tue, 18 Jan 2022 06:37:47 +0100 Subject: [PATCH] feat(analyzer): load config using shared helpers - make rabbitmq config optional --- .../systemd/libretime-analyzer.service | 1 + analyzer/libretime_analyzer/config.py | 5 ++++ analyzer/libretime_analyzer/config_file.py | 16 ---------- analyzer/libretime_analyzer/main.py | 11 +++---- .../libretime_analyzer/message_listener.py | 29 +++++++------------ 5 files changed, 20 insertions(+), 42 deletions(-) create mode 100644 analyzer/libretime_analyzer/config.py delete mode 100644 analyzer/libretime_analyzer/config_file.py diff --git a/analyzer/install/systemd/libretime-analyzer.service b/analyzer/install/systemd/libretime-analyzer.service index 864766150..6ae654d62 100644 --- a/analyzer/install/systemd/libretime-analyzer.service +++ b/analyzer/install/systemd/libretime-analyzer.service @@ -3,6 +3,7 @@ Description=LibreTime Media Analyzer Service [Service] Environment=LIBRETIME_LOG_FILEPATH=/var/log/libretime/analyzer.log +Environment=LIBRETIME_CONFIG_FILEPATH=/etc/airtime/airtime.conf WorkingDirectory=/var/lib/libretime/analyzer ExecStart=/usr/local/bin/libretime-analyzer diff --git a/analyzer/libretime_analyzer/config.py b/analyzer/libretime_analyzer/config.py new file mode 100644 index 000000000..f6b2c3ec6 --- /dev/null +++ b/analyzer/libretime_analyzer/config.py @@ -0,0 +1,5 @@ +from libretime_shared.config import BaseConfig, RabbitMQConfig + + +class Config(BaseConfig): + rabbitmq = RabbitMQConfig() diff --git a/analyzer/libretime_analyzer/config_file.py b/analyzer/libretime_analyzer/config_file.py deleted file mode 100644 index 4620f6c10..000000000 --- a/analyzer/libretime_analyzer/config_file.py +++ /dev/null @@ -1,16 +0,0 @@ -import configparser - - -def read_config_file(config_path): - """Parse the application's config file located at config_path.""" - config = configparser.SafeConfigParser() - try: - config.readfp(open(config_path)) - except IOError as e: - print("Failed to open config file at {}: {}".format(config_path, e.strerror)) - exit(-1) - except Exception as e: - print(e.strerror) - exit(-1) - - return config diff --git a/analyzer/libretime_analyzer/main.py b/analyzer/libretime_analyzer/main.py index 68db2fde1..d6efa68f5 100644 --- a/analyzer/libretime_analyzer/main.py +++ b/analyzer/libretime_analyzer/main.py @@ -6,13 +6,12 @@ from libretime_shared.app import AbstractApp from libretime_shared.cli import cli_config_options, cli_logging_options from libretime_shared.config import DEFAULT_ENV_PREFIX -from .config_file import read_config_file +from .config import Config from .message_listener import MessageListener from .status_reporter import StatusReporter VERSION = "1.0" -DEFAULT_LIBRETIME_CONFIG_FILEPATH = Path("/etc/airtime/airtime.conf") DEFAULT_RETRY_QUEUE_FILEPATH = Path("retry_queue") @@ -38,8 +37,7 @@ def cli( Analyzer( log_level=log_level, log_filepath=log_filepath, - # Handle default until the config file can be optional - config_filepath=config_filepath or DEFAULT_LIBRETIME_CONFIG_FILEPATH, + config_filepath=config_filepath, retry_queue_filepath=retry_queue_filepath, ) @@ -56,14 +54,13 @@ class Analyzer(AbstractApp): ): super().__init__(**kwargs) - # Read our rmq config file - rmq_config = read_config_file(config_filepath) + config = Config(filepath=config_filepath) # Start up the StatusReporter process StatusReporter.start_thread(retry_queue_filepath) # Start listening for RabbitMQ messages telling us about newly # uploaded files. This blocks until we receive a shutdown signal. - self._msg_listener = MessageListener(rmq_config) + self._msg_listener = MessageListener(config.rabbitmq) StatusReporter.stop_thread() diff --git a/analyzer/libretime_analyzer/message_listener.py b/analyzer/libretime_analyzer/message_listener.py index 91e7b607b..742c59d3b 100644 --- a/analyzer/libretime_analyzer/message_listener.py +++ b/analyzer/libretime_analyzer/message_listener.py @@ -5,6 +5,7 @@ import signal import time import pika +from libretime_shared.config import RabbitMQConfig from loguru import logger from .pipeline import Pipeline @@ -56,25 +57,14 @@ QUEUE = "airtime-uploads" class MessageListener: - def __init__(self, rmq_config): - """Start listening for file upload notification messages - from RabbitMQ - - Keyword arguments: - rmq_config: A ConfigParser object containing the [rabbitmq] configuration. + def __init__(self, config: RabbitMQConfig): + """ + Start listening for file upload event messages from RabbitMQ. """ + self.config = config self._shutdown = False - # Read the RabbitMQ connection settings from the rmq_config file - # The exceptions throw here by default give good error messages. - RMQ_CONFIG_SECTION = "rabbitmq" - self._host = rmq_config.get(RMQ_CONFIG_SECTION, "host") - self._port = rmq_config.getint(RMQ_CONFIG_SECTION, "port") - self._username = rmq_config.get(RMQ_CONFIG_SECTION, "user") - self._password = rmq_config.get(RMQ_CONFIG_SECTION, "password") - self._vhost = rmq_config.get(RMQ_CONFIG_SECTION, "vhost") - # Set up a signal handler so we can shutdown gracefully # For some reason, this signal handler must be set up here. I'd rather # put it in AirtimeAnalyzerServer, but it doesn't work there (something to do @@ -104,11 +94,12 @@ class MessageListener: """Connect to the RabbitMQ server and start listening for messages.""" self._connection = pika.BlockingConnection( pika.ConnectionParameters( - host=self._host, - port=self._port, - virtual_host=self._vhost, + host=self.config.host, + port=self.config.port, + virtual_host=self.config.vhost, credentials=pika.credentials.PlainCredentials( - self._username, self._password + self.config.user, + self.config.password, ), ) )