Worker tasks are registered per app
CI / validate-monitoring (push) Successful in 8s
CI / lint (push) Successful in 10s
CI / test (push) Successful in 1m46s
CI / images (push) Skipped
CI / assets (push) Skipped
CI / python-package (push) Skipped
CI / notify (push) Successful in 5s

The task registry was module-global, so tasks registered by an app with
federation enabled would also run under an app without it (and break
the "federation off registers nothing" guarantee depending on order).
Tasks now live on each app (app.extensions["worker_tasks"]);
register_task/register_periodic take the app, and the worker runs the
current app's tasks.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MK52BGoJbPhZ1vSFuKcGUW
This commit is contained in:
mattandClaude Opus 5.5 committed 2026-09-25 21:24:41 +01:00
1 parent d49aa72be1
commit c1a7dd0877
5 files changed
+56 -25

No files matched your search

+19 -13
View File
@@ -1,10 +1,11 @@
"""The background worker: runs tasks that modules register (`flask worker`).
Modules register work at app start-up; the worker itself knows nothing about them.
Modules register work on an app at start-up; the worker itself knows nothing about them.
Each app has its own tasks, so apps with different modules enabled never share them.
register_task(name, fn) fn() -> bool: True if it did some work.
Called repeatedly while there's work.
register_periodic(name, seconds, fn) fn() every `seconds` (and once at start).
register_task(app, name, fn) fn() -> bool: True if it did some work.
Called repeatedly while there's work.
register_periodic(app, name, seconds, fn) fn() every `seconds` (and once at start).
A task that raises is logged, counted and rolled back; the other tasks keep running.
"""
@@ -14,6 +15,8 @@ import time
from collections.abc import Callable
from dataclasses import dataclass
from flask import Flask, current_app
from scrobbler import metrics
from scrobbler.extensions import db
@@ -28,19 +31,22 @@ class _Task:
next_run: float = 0.0 # monotonic time; 0 = run at the first opportunity
_tasks: dict[str, _Task] = {}
def tasks(app: Flask | None = None) -> dict[str, _Task]:
return (app or current_app).extensions.setdefault("worker_tasks", {})
def register_task(name: str, fn: Callable[[], bool]) -> None:
_tasks[name] = _Task(name, fn)
def register_task(app: Flask, name: str, fn: Callable[[], bool]) -> None:
tasks(app)[name] = _Task(name, fn)
def register_periodic(name: str, every_seconds: float, fn: Callable[[], object]) -> None:
_tasks[name] = _Task(name, fn, every=every_seconds)
def register_periodic(
app: Flask, name: str, every_seconds: float, fn: Callable[[], object]
) -> None:
tasks(app)[name] = _Task(name, fn, every=every_seconds)
def registered() -> list[str]:
return sorted(_tasks)
def registered(app: Flask | None = None) -> list[str]:
return sorted(tasks(app))
def _call(task: _Task) -> bool:
@@ -61,7 +67,7 @@ def run_once() -> bool:
task did work (so there may be more waiting)."""
now = time.monotonic()
worked = False
for task in list(_tasks.values()):
for task in list(tasks().values()):
if task.every is not None:
if now >= task.next_run:
task.next_run = now + task.every
@@ -73,7 +79,7 @@ def run_once() -> bool:
def run(poll_seconds: float = 2.0, once: bool = False) -> None:
"""Run until stopped. With once=True, run until there's no more work, then return."""
for task in _tasks.values():
for task in tasks().values():
task.next_run = 0.0
while True:
if run_once():