aboutsummaryrefslogtreecommitdiff
path: root/webmentions_ssg/tasks/extension.py
diff options
context:
space:
mode:
authorDennis Fink2026-11-12 23:34:46 +0100
committerDennis Fink2026-11-12 23:37:08 +0100
commit52fc66cb5ff19bf0def001ae50acc1b465a0b977 (patch)
treee791fa3e6d9f9b1b6fdf697f821adc84d2a57f0b /webmentions_ssg/tasks/extension.py
parentb439de2005b70ec3867d589d1f4c63b83441e620 (diff)
downloadwebmentions-ssg-52fc66cb5ff19bf0def001ae50acc1b465a0b977.tar.gz
webmentions-ssg-52fc66cb5ff19bf0def001ae50acc1b465a0b977.zip
fix(core): decouple task imports from app initialization
Keep stable lazy task handles that register against the active Huey backend so modules can be imported before app creation and survive backend reinitialization. Separate package metadata and the shared blueprint from application startup to remove import-order dependencies and keep build-time version lookup lightweight.
Diffstat (limited to '')
-rw-r--r--webmentions_ssg/tasks/extension.py93
1 files changed, 81 insertions, 12 deletions
diff --git a/webmentions_ssg/tasks/extension.py b/webmentions_ssg/tasks/extension.py
index ee9c442..0e031e6 100644
--- a/webmentions_ssg/tasks/extension.py
+++ b/webmentions_ssg/tasks/extension.py
@@ -4,8 +4,10 @@
"""Integrate Huey tasks with Flask application contexts."""
+from __future__ import annotations
+
from collections.abc import Callable
-from functools import wraps
+from functools import update_wrapper, wraps
from typing import Any, ParamSpec, TypeVar
from urllib.parse import urlsplit, urlunsplit
@@ -19,6 +21,55 @@ P = ParamSpec("P")
R = TypeVar("R")
+class _LazyTask:
+ """Stable public task handle which follows the extension's current backend.
+
+ Huey creates task classes and registers them when its decorator is applied.
+ Keep that backend-specific TaskWrapper private, so imported task handles can
+ be used across Flask app factories without referring to an obsolete queue.
+ """
+
+ def __init__(
+ self,
+ extension: Huey,
+ func: Callable[..., Any],
+ args: tuple[Any, ...],
+ kwargs: dict[str, Any],
+ *,
+ periodic: bool,
+ ) -> None:
+ self._extension = extension
+ self._func = func
+ self._args = args
+ self._kwargs = kwargs
+ self._periodic = periodic
+ self._registered: TaskWrapper | None = None
+ update_wrapper(self, func)
+
+ def register(self) -> None:
+ """Bind the original function to the latest initialized backend."""
+ backend = self._extension.huey
+ decorator = backend.periodic_task if self._periodic else backend.task
+ self._registered = decorator(*self._args, **self._kwargs)(
+ self._extension._in_app_context(self._func)
+ )
+
+ def _current(self) -> TaskWrapper:
+ if self._registered is None:
+ raise RuntimeError(
+ "Huey has not been initialized. Call huey.init_app(app) first."
+ )
+ return self._registered
+
+ def __call__(self, *args: Any, **kwargs: Any) -> Any:
+ """Enqueue a task using the active backend."""
+ return self._current()(*args, **kwargs)
+
+ def __getattr__(self, name: str) -> Any:
+ """Delegate Huey task operations (call_local, schedule, s, etc.)."""
+ return getattr(self._current(), name)
+
+
class Huey:
"""Provide Flask integration for a Huey instance.
@@ -33,6 +84,7 @@ class Huey:
"""
self.app: Flask | None = None
self._huey: BaseHuey | None = None
+ self._tasks: list[_LazyTask] = []
if app is not None:
self.init_app(app)
@@ -73,6 +125,11 @@ class Huey:
self.app = app
self._huey = huey_class(**config, **storage_kwargs)
+ # Import order is immaterial: tasks declared before initialization are
+ # registered now, and previously imported handles follow this backend.
+ for task in self._tasks:
+ task.register()
+
app.extensions["huey"] = self
@property
@@ -85,7 +142,7 @@ class Huey:
if self._huey is None:
raise RuntimeError(
"Huey has not been initialized. "
- "Call huey.init_app(app) before importing tasks."
+ "Call huey.init_app(app) before using tasks."
)
return self._huey
@@ -101,25 +158,37 @@ class Huey:
return wrapped
+ def _decorate_task(
+ self,
+ func: Callable[P, R],
+ task_args: tuple[Any, ...],
+ task_kwargs: dict[str, Any],
+ *,
+ periodic: bool,
+ ) -> _LazyTask:
+ handle = _LazyTask(self, func, task_args, task_kwargs, periodic=periodic)
+ self._tasks.append(handle)
+ if self._huey is not None:
+ handle.register()
+ return handle
+
def task(
self, *task_args: Any, **task_kwargs: Any
- ) -> Callable[[Callable[P, object]], TaskWrapper]:
- """Return a Huey task decorator with Flask application context."""
+ ) -> Callable[[Callable[P, R]], _LazyTask]:
+ """Declare a task even before :meth:`init_app` has run."""
- def decorator(func: Callable[P, R]) -> TaskWrapper:
- return self.huey.task(*task_args, **task_kwargs)(self._in_app_context(func))
+ def decorator(func: Callable[P, R]) -> _LazyTask:
+ return self._decorate_task(func, task_args, task_kwargs, periodic=False)
return decorator
def periodic_task(
self, *task_args: Any, **task_kwargs: Any
- ) -> Callable[[Callable[P, object]], TaskWrapper]:
- """Return a periodic Huey decorator with Flask application context."""
+ ) -> Callable[[Callable[P, R]], _LazyTask]:
+ """Declare a periodic task against the current or next backend."""
- def decorator(func: Callable[P, R]) -> TaskWrapper:
- return self.huey.periodic_task(*task_args, **task_kwargs)(
- self._in_app_context(func)
- )
+ def decorator(func: Callable[P, R]) -> _LazyTask:
+ return self._decorate_task(func, task_args, task_kwargs, periodic=True)
return decorator