diff options
Diffstat (limited to '')
| -rw-r--r-- | webmentions_ssg/tasks/extension.py | 58 |
1 files changed, 31 insertions, 27 deletions
diff --git a/webmentions_ssg/tasks/extension.py b/webmentions_ssg/tasks/extension.py index 58e0036..faed364 100644 --- a/webmentions_ssg/tasks/extension.py +++ b/webmentions_ssg/tasks/extension.py @@ -15,10 +15,12 @@ from flask import Flask from huey import Huey as BaseHuey from huey.api import TaskWrapper -from .json_serializer import JSONSerializer +from .json_serializer import TaggedJSONSerializer -def _redis_backend(parsed: SplitResult) -> tuple[type[BaseHuey], dict[str, Any]]: +def _redis_class_and_options( + parsed: SplitResult, +) -> tuple[type[BaseHuey], dict[str, Any]]: """Select a Redis Huey flavour while retaining the underlying URL scheme. :param parsed: Parsed Redis backend URL, including any flavour suffix. @@ -47,7 +49,9 @@ def _redis_backend(parsed: SplitResult) -> tuple[type[BaseHuey], dict[str, Any]] return backends[flavour], {"url": fixed_url} -def _path_backend(url: str, scheme: str) -> tuple[type[BaseHuey], dict[str, Any]]: +def _file_class_and_options( + url: str, scheme: str +) -> tuple[type[BaseHuey], dict[str, Any]]: """Extract a relative or absolute SQLite/file storage path. :param url: Backend URL containing the storage filename or path. @@ -86,7 +90,7 @@ def _path_backend(url: str, scheme: str) -> tuple[type[BaseHuey], dict[str, Any] return backend, {option: path} -class _LazyTask: +class _DeferredTask: """Stable public task handle which follows the extension's current backend. Huey creates task classes and registers them when its decorator is applied. @@ -111,7 +115,7 @@ class _LazyTask: self._registered: TaskWrapper | None = None update_wrapper(self, func) - def register(self) -> None: + def bind(self) -> None: """Bind the original function to the latest initialized backend. Register the task decorator again so its stable handle uses the currently @@ -120,10 +124,10 @@ class _LazyTask: 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) + self._extension._with_app_context(self._func) ) - def _current(self) -> TaskWrapper: + def _bound_task(self) -> TaskWrapper: if self._registered is None: raise RuntimeError( "Huey has not been initialized. Call huey.init_app(app) first." @@ -137,7 +141,7 @@ class _LazyTask: :param kwargs: Keyword arguments passed to the queued task. :returns: Result of enqueueing the task on the current backend. """ - return self._current()(*args, **kwargs) + return self._bound_task()(*args, **kwargs) def __getattr__(self, name: str) -> Any: """Delegate Huey task operations (call_local, schedule, s, etc.). @@ -145,7 +149,7 @@ class _LazyTask: :param name: Huey task attribute to delegate. :returns: Requested attribute of the currently bound Huey task. """ - return getattr(self._current(), name) + return getattr(self._bound_task(), name) class Huey: @@ -162,7 +166,7 @@ class Huey: """ self.app: Flask | None = None self._huey: BaseHuey | None = None - self._tasks: list[_LazyTask] = [] + self._task_handles: list[_DeferredTask] = [] if app is not None: self.init_app(app) @@ -192,7 +196,7 @@ class Huey: raise RuntimeError( "HUEY.serializer cannot be overridden; JSON is required." ) - config["serializer"] = JSONSerializer( + config["serializer"] = TaggedJSONSerializer( compression=config.pop("compression", False), use_zlib=config.pop("use_zlib", False), ) @@ -205,8 +209,8 @@ class Huey: # 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() + for task in self._task_handles: + task.bind() app.extensions["huey"] = self @@ -224,7 +228,7 @@ class Huey: ) return self._huey - def _in_app_context[**P, R](self, func: Callable[P, R]) -> Callable[P, R]: + def _with_app_context[**P, R](self, func: Callable[P, R]) -> Callable[P, R]: """Return a task wrapper that executes with the configured Flask app. :param func: Task function to wrap in a Flask application context. @@ -241,23 +245,23 @@ class Huey: return wrapped - def _decorate_task[**P, R]( + def _register_task[**P, R]( 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) + ) -> _DeferredTask: + handle = _DeferredTask(self, func, task_args, task_kwargs, periodic=periodic) + self._task_handles.append(handle) if self._huey is not None: - handle.register() + handle.bind() return handle def task[**P, R]( self, *task_args: Any, **task_kwargs: Any - ) -> Callable[[Callable[P, R]], _LazyTask]: + ) -> Callable[[Callable[P, R]], _DeferredTask]: """Declare a task even before :meth:`init_app` has run. :param task_args: Positional arguments for the Huey task decorator. @@ -265,14 +269,14 @@ class Huey: :returns: Decorator that registers and returns a lazily bound task. """ - def decorator(func: Callable[P, R]) -> _LazyTask: - return self._decorate_task(func, task_args, task_kwargs, periodic=False) + def decorator(func: Callable[P, R]) -> _DeferredTask: + return self._register_task(func, task_args, task_kwargs, periodic=False) return decorator def periodic_task[**P, R]( self, *task_args: Any, **task_kwargs: Any - ) -> Callable[[Callable[P, R]], _LazyTask]: + ) -> Callable[[Callable[P, R]], _DeferredTask]: """Declare a periodic task against the current or next backend. :param task_args: Positional arguments for the Huey periodic-task decorator. @@ -280,8 +284,8 @@ class Huey: :returns: Decorator that registers and returns a lazily bound periodic task. """ - def decorator(func: Callable[P, R]) -> _LazyTask: - return self._decorate_task(func, task_args, task_kwargs, periodic=True) + def decorator(func: Callable[P, R]) -> _DeferredTask: + return self._register_task(func, task_args, task_kwargs, periodic=True) return decorator @@ -309,9 +313,9 @@ class Huey: scheme = parsed.scheme.lower() if scheme.partition("+")[0] in {"redis", "rediss"}: - return _redis_backend(parsed) + return _redis_class_and_options(parsed) if scheme in {"sqlite", "file"}: - return _path_backend(url, scheme) + return _file_class_and_options(url, scheme) if scheme in {"postgres", "postgresql"}: from huey import PostgresHuey |
