diff options
Diffstat (limited to 'webmentions_ssg/tasks/extension.py')
| -rw-r--r-- | webmentions_ssg/tasks/extension.py | 53 |
1 files changed, 18 insertions, 35 deletions
diff --git a/webmentions_ssg/tasks/extension.py b/webmentions_ssg/tasks/extension.py index df6cb21..9f53f3b 100644 --- a/webmentions_ssg/tasks/extension.py +++ b/webmentions_ssg/tasks/extension.py @@ -93,54 +93,37 @@ class Huey: ) return self._huey + def _in_app_context(self, func: Callable[P, R]) -> Callable[P, R]: + """Return a task wrapper that executes with the configured Flask app.""" + + @wraps(func) + def wrapped(*args: P.args, **kwargs: P.kwargs) -> R: + if self.app is None: + raise RuntimeError("Flask app is not available.") + with self.app.app_context(): + return func(*args, **kwargs) + + return wrapped + def task( self, *task_args: Any, **task_kwargs: Any ) -> Callable[[Callable[P, object]], TaskWrapper]: - """ - Create a Huey task that runs within the Flask application context. - - :param task_args: Positional arguments forwarded to Huey's task decorator. - :param task_kwargs: Keyword arguments forwarded to Huey's task decorator. - :return: Decorator that registers the wrapped function as a Huey task. - """ + """Return a Huey task decorator with Flask application context.""" def decorator(func: Callable[P, R]) -> TaskWrapper: - @wraps(func) - def wrapper(*args: P.args, **kwargs: P.kwargs) -> R: - if self.app is None: - raise RuntimeError("Flask app is not available.") - - with self.app.app_context(): - return func(*args, **kwargs) - - return self.huey.task(*task_args, **task_kwargs)(wrapper) + return self.huey.task(*task_args, **task_kwargs)(self._in_app_context(func)) return decorator def periodic_task( self, *task_args: Any, **task_kwargs: Any ) -> Callable[[Callable[P, object]], TaskWrapper]: - """ - Create a periodic Huey task that runs within the Flask application context. - - :param task_args: Positional arguments forwarded to Huey's periodic task - decorator. - :param task_kwargs: Keyword arguments forwarded to Huey's periodic task - decorator. - :return: Decorator that registers the wrapped function as a periodic Huey - task. - """ + """Return a periodic Huey decorator with Flask application context.""" def decorator(func: Callable[P, R]) -> TaskWrapper: - @wraps(func) - def wrapper(*args: P.args, **kwargs: P.kwargs) -> R: - if self.app is None: - raise RuntimeError("Flask app is not available.") - - with self.app.app_context(): - return func(*args, **kwargs) - - return self.huey.periodic_task(*task_args, **task_kwargs)(wrapper) + return self.huey.periodic_task(*task_args, **task_kwargs)( + self._in_app_context(func) + ) return decorator |
