From 2a460201bbcc5fc96f3ab7289fe1262a92007dd7 Mon Sep 17 00:00:00 2001 From: Marc 'risson' Schmitt Date: Tue, 24 Jun 2025 19:47:20 +0200 Subject: [PATCH] fix blueprints not applying correctly Signed-off-by: Marc 'risson' Schmitt --- authentik/blueprints/v1/meta/apply_blueprint.py | 5 +---- authentik/blueprints/v1/tasks.py | 7 +++++-- .../django_dramatiq_postgres/middleware.py | 8 +++++++- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/authentik/blueprints/v1/meta/apply_blueprint.py b/authentik/blueprints/v1/meta/apply_blueprint.py index 241839fca4..de518e4838 100644 --- a/authentik/blueprints/v1/meta/apply_blueprint.py +++ b/authentik/blueprints/v1/meta/apply_blueprint.py @@ -44,10 +44,7 @@ class ApplyBlueprintMetaSerializer(PassiveSerializer): return MetaResult() LOGGER.debug("Applying blueprint from meta model", blueprint=self.blueprint_instance) - apply_blueprint.send_with_options( - args=(self.blueprint_instance.pk,), - rel_obj=self.blueprint_instance, - ).get_result(block=True) + apply_blueprint(self.blueprint_instance.pk) return MetaResult() diff --git a/authentik/blueprints/v1/tasks.py b/authentik/blueprints/v1/tasks.py index 7af00d8450..f2e98addb2 100644 --- a/authentik/blueprints/v1/tasks.py +++ b/authentik/blueprints/v1/tasks.py @@ -12,7 +12,7 @@ from dacite.core import from_dict from django.db import DatabaseError, InternalError, ProgrammingError from django.utils.text import slugify from django.utils.timezone import now -from django_dramatiq_postgres.middleware import CurrentTask +from django_dramatiq_postgres.middleware import CurrentTask, CurrentTaskNotFound from dramatiq.actor import actor from dramatiq.middleware import Middleware from structlog.stdlib import get_logger @@ -191,7 +191,10 @@ def check_blueprint_v1_file(blueprint: BlueprintFile): @actor(description=_("Apply single blueprint.")) def apply_blueprint(instance_pk: UUID): - self: Task = CurrentTask.get_task() + try: + self: Task = CurrentTask.get_task() + except CurrentTaskNotFound: + self = Task() self.set_uid(str(instance_pk)) instance: BlueprintInstance | None = None try: diff --git a/packages/django-dramatiq-postgres/django_dramatiq_postgres/middleware.py b/packages/django-dramatiq-postgres/django_dramatiq_postgres/middleware.py index f7083045c0..eb01fe20d0 100644 --- a/packages/django-dramatiq-postgres/django_dramatiq_postgres/middleware.py +++ b/packages/django-dramatiq-postgres/django_dramatiq_postgres/middleware.py @@ -41,6 +41,12 @@ class FullyQualifiedActorName(Middleware): actor.actor_name = f"{actor.fn.__module__}.{actor.fn.__name__}" +class CurrentTaskNotFound(Exception): + """ + Not current task found. Did you call get_task outside a running task? + """ + + class CurrentTask(Middleware): def __init__(self): self.logger = get_logger(__name__, type(self)) @@ -55,7 +61,7 @@ class CurrentTask(Middleware): def get_task(cls) -> TaskBase: task = cls._TASKS.get() if not task: - raise RuntimeError("CurrentTask.get_task() can only be called in a running task") + raise CurrentTaskNotFound() return task[-1] def before_enqueue(self, broker: Broker, message: Message, delay: int):