Source code for smarter.apps.orchestrator.tasks.queue

"""
The one place that queues the orchestrator app's Celery tasks.

Every task of the app is queued by a function of this module, never by ``<task>.delay()`` or
``<task>.apply_async()`` anywhere else (ADR-030, rule 10, issue #1098). So this module is where to look for what
queues a task, and the one place that a test patches so that nothing reaches a real worker, which would call LLMs, and
charge for them.
"""

from django.db import transaction

from smarter.apps.account.models import UserProfile
from smarter.apps.orchestrator.models import OrchestrationRun, Orchestrator
from smarter.apps.orchestrator.runtime import check_may_run
from smarter.apps.orchestrator.signals import orchestration_run_queued

from .execute import execute_orchestration_run


[docs] def queue_execute_orchestration_run( orchestrator: Orchestrator, user_profile: UserProfile, objective: str ) -> OrchestrationRun: """ Start a run of the Orchestrator: check that it may start, record it, and queue :func:`~smarter.apps.orchestrator.tasks.execute.execute_orchestration_run`. The api's runs view calls it. The task is queued once the run's row is committed, so that the worker finds it, and ``orchestration_run_queued`` is sent. .. code-block:: python run = queue_execute_orchestration_run(orchestrator, user_profile, "Prepare the board pack for Q4 2025.") :param orchestrator: The Orchestrator. :param user_profile: The profile of the user who starts it. :param objective: What the run is to achieve, e.g. "Prepare the board pack for Q4 2025." :returns: The run, ``pending``. :raises OrchestrationRefusedError: If the run may not start, from :func:`~smarter.apps.orchestrator.runtime.check_may_run`. """ check_may_run(orchestrator, user_profile.user) run = OrchestrationRun.objects.create(orchestrator=orchestrator, objective=objective, started_by=user_profile) run_id = run.pk transaction.on_commit(lambda: execute_orchestration_run.delay(run_id)) orchestration_run_queued.send(sender=queue_execute_orchestration_run, run=run) return run
__all__ = ["queue_execute_orchestration_run"]