scrobbler/worker.py runs tasks that modules register: queue tasks
(called while they report work) and periodic tasks. A failing task is
logged, counted, rolled back and doesn't stop the others. Imports are
now just a registered task plus a periodic stall check.
- `flask worker` replaces `flask imports worker` (kept as an alias);
compose uses the new command
- scrobbler_worker_tasks_total{task,outcome} and a Worker tasks panel
on the Usage dashboard
- WORKER_METRICS_PORT (IMPORT_WORKER_METRICS_PORT still honoured)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MK52BGoJbPhZ1vSFuKcGUW
46 lines
1.5 KiB
Python
46 lines
1.5 KiB
Python
import logging
|
|
|
|
import click
|
|
from flask import current_app
|
|
from flask.cli import AppGroup, with_appcontext
|
|
|
|
|
|
def _run_worker(once: bool, poll: float, serve_metrics: bool) -> None:
|
|
from scrobbler import worker
|
|
|
|
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s")
|
|
if serve_metrics:
|
|
from scrobbler.metrics.server import start_metrics_server
|
|
|
|
start_metrics_server(current_app.config["WORKER_METRICS_PORT"])
|
|
logging.getLogger(__name__).info("worker tasks: %s", ", ".join(worker.registered()))
|
|
worker.run(poll_seconds=poll, once=once)
|
|
|
|
|
|
def _worker_options(fn):
|
|
fn = click.option(
|
|
"--metrics/--no-metrics", "serve_metrics", default=True, help="Serve Prometheus metrics."
|
|
)(fn)
|
|
fn = click.option(
|
|
"--poll", default=2.0, show_default=True, help="Seconds between checks when idle."
|
|
)(fn)
|
|
return click.option("--once", is_flag=True, help="Do all waiting work, then exit.")(fn)
|
|
|
|
|
|
@click.command("worker")
|
|
@_worker_options
|
|
@with_appcontext
|
|
def worker_command(once: bool, poll: float, serve_metrics: bool) -> None:
|
|
"""Run background work: imports, and tasks registered by optional modules."""
|
|
_run_worker(once, poll, serve_metrics)
|
|
|
|
|
|
imports_cli = AppGroup("imports", help="History imports.")
|
|
|
|
|
|
@imports_cli.command("worker")
|
|
@_worker_options
|
|
def imports_worker(once: bool, poll: float, serve_metrics: bool) -> None:
|
|
"""Alias for `flask worker` (kept for existing deployments)."""
|
|
_run_worker(once, poll, serve_metrics)
|