|
| 1 | +import signal |
| 2 | +from typing import Optional, Type |
| 3 | + |
| 4 | +from ai.backend.agent.agent import AbstractAgent |
| 5 | +from ai.backend.agent.config.unified import AgentUnifiedConfig |
| 6 | +from ai.backend.agent.monitor import AgentErrorPluginContext, AgentStatsPluginContext |
| 7 | +from ai.backend.agent.types import AgentBackend |
| 8 | +from ai.backend.common.auth import PublicKey |
| 9 | +from ai.backend.common.etcd import AsyncEtcd |
| 10 | +from ai.backend.common.types import aobject |
| 11 | + |
| 12 | + |
| 13 | +class AgentRuntime(aobject): |
| 14 | + local_config: AgentUnifiedConfig |
| 15 | + agent: AbstractAgent |
| 16 | + |
| 17 | + _stop_signal: signal.Signals |
| 18 | + |
| 19 | + def __init__( |
| 20 | + self, |
| 21 | + local_config: AgentUnifiedConfig, |
| 22 | + etcd: AsyncEtcd, |
| 23 | + stats_monitor: AgentStatsPluginContext, |
| 24 | + error_monitor: AgentErrorPluginContext, |
| 25 | + agent_public_key: Optional[PublicKey], |
| 26 | + ) -> None: |
| 27 | + self.local_config = local_config |
| 28 | + |
| 29 | + self._stop_signal = signal.SIGTERM |
| 30 | + |
| 31 | + self.etcd = etcd |
| 32 | + self.stats_monitor = stats_monitor |
| 33 | + self.error_monitor = error_monitor |
| 34 | + self.agent_public_key = agent_public_key |
| 35 | + |
| 36 | + async def __ainit__(self) -> None: |
| 37 | + agent_cls = self._get_agent_cls() |
| 38 | + self.agent = await agent_cls.new( |
| 39 | + self.etcd, |
| 40 | + self.local_config, |
| 41 | + stats_monitor=self.stats_monitor, |
| 42 | + error_monitor=self.error_monitor, |
| 43 | + agent_public_key=self.agent_public_key, |
| 44 | + ) |
| 45 | + |
| 46 | + async def __aexit__(self, *exc_info) -> None: |
| 47 | + await self.agent.shutdown(self._stop_signal) |
| 48 | + |
| 49 | + def get_agent(self) -> AbstractAgent: |
| 50 | + return self.agent |
| 51 | + |
| 52 | + def get_etcd(self) -> AsyncEtcd: |
| 53 | + return self.etcd |
| 54 | + |
| 55 | + def mark_stop_signal(self, stop_signal: signal.Signals) -> None: |
| 56 | + self._stop_signal = stop_signal |
| 57 | + |
| 58 | + def _get_agent_cls(self) -> Type[AbstractAgent]: |
| 59 | + match self.local_config.agent_common.backend: |
| 60 | + case AgentBackend.DOCKER: |
| 61 | + from ai.backend.agent.docker.agent import DockerAgent |
| 62 | + |
| 63 | + return DockerAgent |
| 64 | + case AgentBackend.KUBERNETES: |
| 65 | + from ai.backend.agent.kubernetes.agent import KubernetesAgent |
| 66 | + |
| 67 | + return KubernetesAgent |
| 68 | + case AgentBackend.DUMMY: |
| 69 | + from ai.backend.agent.dummy.agent import DummyAgent |
| 70 | + |
| 71 | + return DummyAgent |
0 commit comments