Files
Scrobbler/src/scrobbler/cli.py
T
mattandClaude Opus 5.5 098eab025f
CI / validate-monitoring (push) Successful in 8s
CI / lint (push) Failing after 38s
CI / test (push) Successful in 46s
CI / images (push) Skipped
CI / assets (push) Skipped
CI / python-package (push) Skipped
CI / notify (push) Successful in 5s
Generic background worker (phase 0, step 2)
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
2026-09-25 19:57:25 +01:00

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)