From 52fc66cb5ff19bf0def001ae50acc1b465a0b977 Mon Sep 17 00:00:00 2001 From: Dennis Fink Date: Thu, 12 Nov 2026 23:34:46 +0100 Subject: 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. --- tests/tasks/test_extension.py | 61 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-) (limited to 'tests/tasks/test_extension.py') diff --git a/tests/tasks/test_extension.py b/tests/tasks/test_extension.py index 19f1191..a2495e8 100644 --- a/tests/tasks/test_extension.py +++ b/tests/tasks/test_extension.py @@ -8,7 +8,7 @@ from typing import Any import huey as huey_package import pytest -from flask import Flask, has_app_context +from flask import Flask, current_app, has_app_context from huey import crontab from webmentions_ssg.tasks.extension import Huey @@ -94,6 +94,65 @@ def test_task_raises_without_app(app: Flask, periodic: bool) -> None: task.call_local() +@pytest.mark.parametrize("periodic", [False, True], ids=["task", "periodic-task"]) +def test_task_declaration_before_init_and_rebind_after_init(periodic: bool) -> None: + """Imported task handles do not capture the first application backend.""" + queue = Huey() + observed_apps: list[str] = [] + + @task_decorator(queue, periodic) + def identify_app() -> str: + name = current_app.import_name + observed_apps.append(name) + return name + + assert identify_app.__name__ == "identify_app" + with pytest.raises(RuntimeError, match="Huey has not been initialized"): + identify_app.call_local() + + first = Flask("first") + first.config["TESTING"] = True + queue.init_app(first) + assert identify_app.call_local() == "first" + # Huey discards periodic-task results, even in immediate mode. Verify + # execution through a side effect as well as the returned result. + assert identify_app().get() == (None if periodic else "first") + assert observed_apps == ["first", "first"] + + previous_backend = queue.huey + second = Flask("second") + second.config["TESTING"] = True + queue.init_app(second) + + assert queue.huey is not previous_backend + assert identify_app.call_local() == "second" + assert identify_app().get() == (None if periodic else "second") + assert observed_apps == ["first", "first", "second", "second"] + # The new broker must recognize the registered task when deserializing work. + queued = identify_app.s() + restored = queue.huey.deserialize_task(queue.huey.serialize_task(queued)) + assert type(restored) is type(queued) + + +def test_multiple_deferred_tasks_registered_on_initialization() -> None: + queue = Huey() + + @queue.task(retries=2, retry_delay=50) + def first(value: int) -> int: + return value + 1 + + @queue.task() + def second(value: int) -> int: + return first.call_local(value) + 1 + + app = Flask("tasks") + app.config["TESTING"] = True + queue.init_app(app) + + assert second(2).get() == 4 + assert first.call_local(2) == 3 + + def test_getattr_delegates_to_huey(app: Flask) -> None: huey = Huey(app) assert huey.immediate is True -- cgit v1.3.1