feat(analyzer): enhance analyzer cli and logging (#1507)

Some initial work on modernizing the analyzer app. This replace any custom logger or `logging` based logger with the logging tools from `libretime_shared.logging` and `loguru`.

- rename cli to main
- use pathlib in setup.py
- add api-client and shared package as dev deps
- rework main entrypoint cli to use click and shared helpers
- remove unused imports
- replace logging with logger
- rework analyzer app using shared abstract app
- move analyzer log path to systemd service
- change analyzer working dir

BREAKING CHANGE: The analyzer cli has been reworked and uses new flags / environnement variables for configuration.
`--debug` flag becomes `--log-level <level>`
`--rmq-config-file` flag becomes `--config <filepath>`
`--http-retry-queue-file` flag becomes `--retry-queue-filepath`.
`retry-queue-filepath` default value changed from `/tmp/airtime_analyzer_http_retries` to `retry_queue` in the working dir.
`LIBRETIME_CONF_DIR` environnement variable replaced by `LIBRETIME_CONFIG_FILEPATH`.

BREAKING CHANGE: When running analyzer as a systemd service, the working directory is now /var/lib/libretime/analyzer.
This commit is contained in:
Jonas L 2022-01-17 09:26:30 +01:00 committed by GitHub
parent bf59f20ffd
commit fe0b2c4a7a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 138 additions and 215 deletions

View file

@ -0,0 +1,69 @@
from pathlib import Path
from typing import Optional
import click
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 .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")
@click.command()
@cli_logging_options
@cli_config_options
@click.option(
"--retry-queue-filepath",
envvar=f"{DEFAULT_ENV_PREFIX}_RETRY_QUEUE_FILEPATH",
type=click.Path(path_type=Path),
help="Path to the retry queue file.",
default=DEFAULT_RETRY_QUEUE_FILEPATH,
)
def cli(
log_level: str,
log_filepath: Optional[Path],
config_filepath: Optional[Path],
retry_queue_filepath: Path,
):
"""
Run analyzer.
"""
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,
retry_queue_filepath=retry_queue_filepath,
)
class Analyzer(AbstractApp):
name = "analyzer"
def __init__(
self,
*,
config_filepath: Optional[Path],
retry_queue_filepath: Path,
**kwargs,
):
super().__init__(**kwargs)
# Read our rmq config file
rmq_config = read_config_file(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)
StatusReporter.stop_thread()