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

"""
The one place that queues the mcpclient 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, the tasks themselves included (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 connect to an MCP server. Celery Beat sends ``refresh_mcpclients`` by its name.
"""

from django.db import transaction

from smarter.apps.mcpclient.models import MCPClient
from smarter.lib import logging
from smarter.lib.django.waffle import SmarterWaffleSwitches

from .refresh import refresh_mcpclient

logger = logging.getSmarterLogger(
    __name__, any_switches=[SmarterWaffleSwitches.TASK_LOGGING, SmarterWaffleSwitches.MCPCLIENT_LOGGING]
)


[docs] def queue_refresh_mcpclient(mcpclient: MCPClient) -> bool: """ Queue :func:`~smarter.apps.mcpclient.tasks.refresh.refresh_mcpclient`, which refreshes an MCPClient's catalog. Only an active MCPClient with an endpoint is refreshed: there is nothing to connect to otherwise. The MCPClient's ``post_save`` receiver calls it, and :func:`~smarter.apps.mcpclient.tasks.refresh.refresh_mcpclients` for every active MCPClient. It is queued once the current transaction commits, so that the worker finds the MCPClient as it was saved. :param mcpclient: The MCPClient. :returns: True if the refresh was queued, False if the MCPClient is inactive or has no endpoint. """ if not mcpclient.is_active: logger.debug("MCPClient %s is not active: not refreshing it.", mcpclient.pk) return False if not mcpclient.endpoint_url: logger.debug("MCPClient %s has no endpoint: not refreshing it.", mcpclient.pk) return False mcpclient_id = mcpclient.pk transaction.on_commit(lambda: refresh_mcpclient.delay(mcpclient_id)) return True
__all__ = ["queue_refresh_mcpclient"]