From ced8e10a0e0548d0a4a2da7a327088e1ffb76e96 Mon Sep 17 00:00:00 2001 From: Kevin Veen-Birkenbach Date: Thu, 3 Sep 2026 17:00:47 +0200 Subject: [PATCH] fix(sys-stk-backend): rebind handlers before flush Ansible resolves a notification to the last handler block registered under that name and templates application_id in that block's context. Since 95186163aa sys-svc-compose is loaded first, so the provider roles included after it (svc-db-postgres via sys-svc-rdbms, objstore, engine) leave their own copy as the last one, and the trailing flush deployed the dedicated postgres instead of the app. Run 33751742070: six jobs waited on a keycloak or mattermost container that was never created. A dynamic include_role directly before the flush makes the app's copy the last registration again. import_role would not do: it expands when main.yml is parsed, before the providers run. The first include stays, so the missing-handler error stays fixed. Co-Authored-By: Claude Opus 5 (1M context) --- roles/sys-stk-backend/tasks/main.yml | 6 +++++ .../test_handler_registration_order.py | 25 +++++++++++++++++-- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/roles/sys-stk-backend/tasks/main.yml b/roles/sys-stk-backend/tasks/main.yml index 3a66f031bb..afac268cae 100644 --- a/roles/sys-stk-backend/tasks/main.yml +++ b/roles/sys-stk-backend/tasks/main.yml @@ -59,6 +59,12 @@ include_tasks: "{{ lookup('path_absolute', 'roles/web-app-keycloak/tasks/sso_proxy.yml') }}" when: lookup('sso', application_id, 'is_proxy_gated') | bool +- name: "📂 For '{{ application_id }}': re-own the compose handler topics before the flush" + ansible.builtin.include_role: + name: sys-svc-compose + tasks_from: none.yml + handlers_from: main + - name: "🚿 flush compose and SSO proxy" meta: flush_handlers when: compose_handlers_flush | bool diff --git a/tests/unit/python/roles/sys-stk-backend/test_handler_registration_order.py b/tests/unit/python/roles/sys-stk-backend/test_handler_registration_order.py index bf292866c9..491162a51f 100644 --- a/tests/unit/python/roles/sys-stk-backend/test_handler_registration_order.py +++ b/tests/unit/python/roles/sys-stk-backend/test_handler_registration_order.py @@ -14,11 +14,19 @@ COMPOSE_OWNER = "sys-svc-compose" _NOTIFY = re.compile(r"^\s*notify:\s*(?:\[\s*)?['\"]?(compose-[\w-]+)", re.MULTILINE) +def _role_include(task: dict) -> dict: + for key in ("include_role", "import_role"): + include = task.get(key) or task.get(f"ansible.builtin.{key}") + if isinstance(include, dict): + return include + return {} + + def _included_roles(tasks: list) -> list[str]: names = [] for task in tasks: - include = task.get("include_role") or task.get("import_role") or {} - if isinstance(include, dict) and include.get("name"): + include = _role_include(task) + if include.get("name"): names.append(include["name"]) return names @@ -29,6 +37,19 @@ class TestHandlerRegistrationOrder(unittest.TestCase): self.assertIn(COMPOSE_OWNER, order) self.assertEqual(order[0], COMPOSE_OWNER) + def test_the_owner_is_reloaded_directly_before_the_flush(self) -> None: + tasks = load_yaml_str(read_text(str(BACKEND))) + flush = next( + index + for index, task in enumerate(tasks) + if task.get("meta") == "flush_handlers" + or task.get("ansible.builtin.meta") == "flush_handlers" + ) + previous = tasks[flush - 1] + loader = _role_include(previous) + self.assertEqual(loader.get("name"), COMPOSE_OWNER) + self.assertEqual(loader.get("handlers_from"), "main") + def test_the_owner_still_holds_every_compose_topic_that_is_notified(self) -> None: owner = "" for path, content in iter_project_files_with_content(