|
| 1 | +"""Static capability snapshot collection for a Pulsar app. |
| 2 | +
|
| 3 | +A ``PulsarCapabilities`` is a serializable summary of what a Pulsar |
| 4 | +server is configured to do — staging paths, dependency resolvers, |
| 5 | +container runtimes available on the host — so that Galaxy can read it |
| 6 | +at job-build time and adjust (or refuse) requests that the remote does |
| 7 | +not actually support. |
| 8 | +
|
| 9 | +The data is collected once at app startup (in ``PulsarApp.__init__``), |
| 10 | +cached on the app, and published once to a relay topic from |
| 11 | +``messaging.bind_app``. It is never recomputed. Consumers fetch the |
| 12 | +latest snapshot via the relay's REST topic-messages endpoint. |
| 13 | +
|
| 14 | +Schema is versioned (``SCHEMA_VERSION``); bumping it is a wire-breaking |
| 15 | +change for consumers, so add new optional fields rather than reshape |
| 16 | +existing ones. |
| 17 | +""" |
| 18 | +from __future__ import annotations |
| 19 | + |
| 20 | +import logging |
| 21 | +import os |
| 22 | +import shutil |
| 23 | +from dataclasses import ( |
| 24 | + asdict, |
| 25 | + dataclass, |
| 26 | + field, |
| 27 | +) |
| 28 | +from typing import ( |
| 29 | + TYPE_CHECKING, |
| 30 | + Dict, |
| 31 | + List, |
| 32 | + Optional, |
| 33 | + Union, |
| 34 | +) |
| 35 | + |
| 36 | +from galaxy.tool_util.deps.resolvers.conda import CondaDependencyResolver |
| 37 | + |
| 38 | +from pulsar import __version__ as pulsar_version |
| 39 | + |
| 40 | +if TYPE_CHECKING: |
| 41 | + # Imported lazily to avoid the runtime cycle: pulsar.core imports this |
| 42 | + # module to populate the cache. |
| 43 | + from pulsar.core import PulsarApp |
| 44 | + from pulsar.managers.stateful import StatefulManagerProxy |
| 45 | + |
| 46 | +log = logging.getLogger(__name__) |
| 47 | + |
| 48 | +SCHEMA_VERSION = 1 |
| 49 | + |
| 50 | + |
| 51 | +@dataclass |
| 52 | +class DependencyResolverInfo: |
| 53 | + type: str |
| 54 | + disabled: bool = False |
| 55 | + versionless: bool = False |
| 56 | + auto_init: Optional[bool] = None |
| 57 | + auto_install: Optional[bool] = None |
| 58 | + prefix: Optional[str] = None |
| 59 | + |
| 60 | + |
| 61 | +@dataclass |
| 62 | +class ContainerRuntimeInfo: |
| 63 | + docker_available: bool = False |
| 64 | + singularity_available: bool = False |
| 65 | + apptainer_available: bool = False |
| 66 | + |
| 67 | + |
| 68 | +@dataclass |
| 69 | +class ManagerCapabilities: |
| 70 | + name: str |
| 71 | + type: str |
| 72 | + num_concurrent_jobs: Union[int, str, None] = None |
| 73 | + |
| 74 | + |
| 75 | +@dataclass |
| 76 | +class PulsarCapabilities: |
| 77 | + schema_version: int |
| 78 | + manager_name: str |
| 79 | + pulsar_version: str |
| 80 | + staging_directory: str |
| 81 | + persistence_directory: Optional[str] |
| 82 | + tool_dependency_dir: Optional[str] |
| 83 | + dependency_resolvers: List[DependencyResolverInfo] = field(default_factory=list) |
| 84 | + conda_available: bool = False |
| 85 | + container_runtime: ContainerRuntimeInfo = field(default_factory=ContainerRuntimeInfo) |
| 86 | + manager: Optional[ManagerCapabilities] = None |
| 87 | + |
| 88 | + def to_dict(self) -> dict: |
| 89 | + return asdict(self) |
| 90 | + |
| 91 | + |
| 92 | +def collect_capabilities(app: "PulsarApp", manager: "StatefulManagerProxy") -> PulsarCapabilities: |
| 93 | + """Build the capabilities snapshot for a single manager on this app.""" |
| 94 | + resolvers = _collect_dependency_resolvers(app) |
| 95 | + manager_caps = _collect_manager(manager) |
| 96 | + return PulsarCapabilities( |
| 97 | + schema_version=SCHEMA_VERSION, |
| 98 | + manager_name=manager_caps.name, |
| 99 | + pulsar_version=pulsar_version, |
| 100 | + staging_directory=app.staging_directory, |
| 101 | + persistence_directory=app.persistence_directory, |
| 102 | + tool_dependency_dir=_tool_dependency_dir(app), |
| 103 | + dependency_resolvers=resolvers, |
| 104 | + conda_available=_conda_available(resolvers), |
| 105 | + container_runtime=_detect_container_runtime(), |
| 106 | + manager=manager_caps, |
| 107 | + ) |
| 108 | + |
| 109 | + |
| 110 | +def collect_all_capabilities(app: "PulsarApp") -> Dict[str, PulsarCapabilities]: |
| 111 | + """Map ``manager_name -> PulsarCapabilities`` for every manager on the app. |
| 112 | +
|
| 113 | + Used by ``PulsarApp.__init__`` to populate the cache. Failures on |
| 114 | + individual managers are logged and skipped so a misconfigured |
| 115 | + manager cannot prevent the app from coming up. |
| 116 | + """ |
| 117 | + out: Dict[str, PulsarCapabilities] = {} |
| 118 | + for name, manager in app.managers.items(): |
| 119 | + try: |
| 120 | + out[name] = collect_capabilities(app, manager) |
| 121 | + except Exception: |
| 122 | + log.exception("Failed to collect capabilities for manager %s", name) |
| 123 | + return out |
| 124 | + |
| 125 | + |
| 126 | +def _collect_dependency_resolvers(app: "PulsarApp") -> List[DependencyResolverInfo]: |
| 127 | + out: List[DependencyResolverInfo] = [] |
| 128 | + for r in app.dependency_manager.dependency_resolvers: |
| 129 | + # ``resolver_type`` and ``versionless`` are class attributes on |
| 130 | + # concrete subclasses, not the base — probe defensively rather |
| 131 | + # than narrow the loop type to every known subclass. |
| 132 | + rt = getattr(r, "resolver_type", None) |
| 133 | + if not rt: |
| 134 | + continue |
| 135 | + info = DependencyResolverInfo( |
| 136 | + type=str(rt), |
| 137 | + disabled=r.disabled, |
| 138 | + versionless=bool(getattr(r, "versionless", False)), |
| 139 | + ) |
| 140 | + if isinstance(r, CondaDependencyResolver): |
| 141 | + info.auto_init = bool(r.auto_init) |
| 142 | + info.auto_install = bool(r.auto_install) |
| 143 | + info.prefix = str(r.prefix) if r.prefix else None |
| 144 | + out.append(info) |
| 145 | + return out |
| 146 | + |
| 147 | + |
| 148 | +def _conda_available(resolvers: List[DependencyResolverInfo]) -> bool: |
| 149 | + """A conda resolver is enabled and conda is reachable on this host. |
| 150 | +
|
| 151 | + "Reachable" means either the conda binary is on PATH or the resolver's |
| 152 | + configured prefix exists on disk — Galaxy on the requesting side wants |
| 153 | + to know whether ``dependency_resolution=remote`` will actually find |
| 154 | + something to run. |
| 155 | + """ |
| 156 | + for r in resolvers: |
| 157 | + if r.type != "conda" or r.disabled: |
| 158 | + continue |
| 159 | + if shutil.which("conda"): |
| 160 | + return True |
| 161 | + if r.prefix and os.path.isdir(r.prefix): |
| 162 | + return True |
| 163 | + return False |
| 164 | + |
| 165 | + |
| 166 | +def _detect_container_runtime() -> ContainerRuntimeInfo: |
| 167 | + return ContainerRuntimeInfo( |
| 168 | + docker_available=bool(shutil.which("docker")), |
| 169 | + singularity_available=bool(shutil.which("singularity")), |
| 170 | + apptainer_available=bool(shutil.which("apptainer")), |
| 171 | + ) |
| 172 | + |
| 173 | + |
| 174 | +def _collect_manager(manager: "StatefulManagerProxy") -> ManagerCapabilities: |
| 175 | + underlying = manager._proxied_manager |
| 176 | + # ``manager_type`` is a class attribute set on each concrete manager |
| 177 | + # implementation but not on the proxy or its base. The ``work_threads`` |
| 178 | + # attribute is queued_python-only — falling back to a public |
| 179 | + # ``num_concurrent_jobs`` covers any future manager that exposes one. |
| 180 | + mtype = getattr(type(underlying), "manager_type", "unknown") |
| 181 | + work_threads = getattr(underlying, "work_threads", None) |
| 182 | + num: Union[int, str, None] |
| 183 | + if work_threads is not None: |
| 184 | + try: |
| 185 | + num = len(work_threads) |
| 186 | + except TypeError: |
| 187 | + num = None |
| 188 | + else: |
| 189 | + num = getattr(underlying, "num_concurrent_jobs", None) |
| 190 | + return ManagerCapabilities(name=manager.name, type=str(mtype), num_concurrent_jobs=num) |
| 191 | + |
| 192 | + |
| 193 | +def _tool_dependency_dir(app: "PulsarApp") -> Optional[str]: |
| 194 | + base = getattr(app.dependency_manager, "default_base_path", None) |
| 195 | + return str(base) if base else None |
0 commit comments