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
This commit is contained in:
1 parent
59ff1bdfa0
commit
098eab025f
13 files changed
+292
-42
No files matched your search
+36
-14
@@ -2,22 +2,44 @@ import logging
|
||||
|
||||
import click
|
||||
from flask import current_app
|
||||
from flask.cli import AppGroup
|
||||
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")
|
||||
@click.option("--once", is_flag=True, help="Process waiting jobs, then exit.")
|
||||
@click.option("--poll", default=2.0, show_default=True, help="Seconds between checks when idle.")
|
||||
@click.option("--metrics/--no-metrics", default=True, help="Serve Prometheus metrics.")
|
||||
def worker(once: bool, poll: float, metrics: bool) -> None:
|
||||
"""Process import jobs: uploaded Last.fm exports and pulls from Last.fm."""
|
||||
from scrobbler.services.imports import run_worker
|
||||
|
||||
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s")
|
||||
if metrics:
|
||||
from scrobbler.metrics.server import start_metrics_server
|
||||
|
||||
start_metrics_server(current_app.config["IMPORT_WORKER_METRICS_PORT"])
|
||||
run_worker(poll_seconds=poll, once=once)
|
||||
@_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)
|
||||
Reference in new issue
Block a user