"""Deprecated, lazy compatibility facade for the Controller v2 service. New deployments use :mod:`wsgi`. These functions remain for local callers that imported the original module, but importing this module never opens the database, creates directories, starts threads, or connects to MQTT. """ from __future__ import annotations import threading from typing import Any from controller.config import ControllerConfig, load_config from controller.service import ControllerService, ControllerUnavailableError _lifecycle_lock = threading.Lock() _service: ControllerService | None = None _config: ControllerConfig | None = None # Kept as transition indicators for integrations that inspected the old module. mqtt_client: Any | None = None mqtt_started = False def start_controller() -> None: """Construct and start the singleton compatibility service explicitly.""" global _config, _service, mqtt_client, mqtt_started with _lifecycle_lock: if _service is not None and _service.started: return config = load_config() service = ControllerService(config) service.start() _config = config _service = service mqtt_client = service.mqtt mqtt_started = True def stop_controller() -> None: """Stop and discard the singleton compatibility service.""" global _config, _service, mqtt_client, mqtt_started with _lifecycle_lock: service = _service if service is not None: service.stop() _service = None _config = None mqtt_client = None mqtt_started = False def get_service() -> ControllerService: """Return the explicitly started compatibility service.""" with _lifecycle_lock: if _service is None or not _service.started: raise ControllerUnavailableError( "Controller is not started; call start_controller() first" ) return _service def enqueue_download( url: str, submitter: str | None = None, note: str | None = None, ) -> dict[str, Any]: """Queue a download through Controller v2.""" return get_service().enqueue_download(url, submitter, note) __all__ = [ "enqueue_download", "get_service", "mqtt_client", "mqtt_started", "start_controller", "stop_controller", ]