fix(host-agent): advertise connected devices
This commit is contained in:
@@ -4,6 +4,7 @@ import asyncio
|
|||||||
from typing import TYPE_CHECKING
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
from cloud.internal_api.models import DeviceSnapshotModel, HeartbeatResponse
|
from cloud.internal_api.models import DeviceSnapshotModel, HeartbeatResponse
|
||||||
|
from core.errors import DeviceRuntimeError
|
||||||
from device.manager import DeviceManager
|
from device.manager import DeviceManager
|
||||||
from host_agent.client import HostAgentClient
|
from host_agent.client import HostAgentClient
|
||||||
from host_agent.config import HostAgentConfig
|
from host_agent.config import HostAgentConfig
|
||||||
@@ -17,7 +18,7 @@ def build_device_snapshot(manager: DeviceManager) -> list[DeviceSnapshotModel]:
|
|||||||
DeviceSnapshotModel(
|
DeviceSnapshotModel(
|
||||||
device_id=device.id,
|
device_id=device.id,
|
||||||
driver_type=device.driver_type,
|
driver_type=device.driver_type,
|
||||||
status=device.status,
|
status="idle" if device.status == "busy" else device.status,
|
||||||
capability_tags=list(device.capability_tags),
|
capability_tags=list(device.capability_tags),
|
||||||
)
|
)
|
||||||
for device in sorted(manager.list_devices(), key=lambda item: item.id)
|
for device in sorted(manager.list_devices(), key=lambda item: item.id)
|
||||||
@@ -47,6 +48,7 @@ class HeartbeatSynchronizer:
|
|||||||
)
|
)
|
||||||
|
|
||||||
async def run(self, stop: asyncio.Event) -> None:
|
async def run(self, stop: asyncio.Event) -> None:
|
||||||
|
self.connect_devices()
|
||||||
while not stop.is_set():
|
while not stop.is_set():
|
||||||
await self.sync_once()
|
await self.sync_once()
|
||||||
try:
|
try:
|
||||||
@@ -56,3 +58,12 @@ class HeartbeatSynchronizer:
|
|||||||
)
|
)
|
||||||
except TimeoutError:
|
except TimeoutError:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
def connect_devices(self) -> None:
|
||||||
|
for device in self.manager.list_devices():
|
||||||
|
if device.status != "idle":
|
||||||
|
continue
|
||||||
|
try:
|
||||||
|
self.manager.connect(device.id)
|
||||||
|
except DeviceRuntimeError:
|
||||||
|
continue
|
||||||
|
|||||||
@@ -9,6 +9,11 @@ from host_agent.config import HostAgentConfig
|
|||||||
from host_agent.heartbeat import HeartbeatSynchronizer, build_device_snapshot
|
from host_agent.heartbeat import HeartbeatSynchronizer, build_device_snapshot
|
||||||
|
|
||||||
|
|
||||||
|
class ConnectableDriver:
|
||||||
|
def connect(self) -> None:
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
def _config() -> HostAgentConfig:
|
def _config() -> HostAgentConfig:
|
||||||
return HostAgentConfig(
|
return HostAgentConfig(
|
||||||
control_plane_url="https://control.example",
|
control_plane_url="https://control.example",
|
||||||
@@ -38,7 +43,7 @@ def test_build_device_snapshot_copies_complete_non_secret_state() -> None:
|
|||||||
|
|
||||||
assert [device.device_id for device in snapshot] == ["device-a", "device-b"]
|
assert [device.device_id for device in snapshot] == ["device-a", "device-b"]
|
||||||
assert snapshot[1].driver_type == "appium"
|
assert snapshot[1].driver_type == "appium"
|
||||||
assert snapshot[1].status == "busy"
|
assert snapshot[1].status == "idle"
|
||||||
assert snapshot[1].capability_tags == ["android", "physical"]
|
assert snapshot[1].capability_tags == ["android", "physical"]
|
||||||
assert "must-not-leave-host" not in repr(snapshot)
|
assert "must-not-leave-host" not in repr(snapshot)
|
||||||
|
|
||||||
@@ -47,7 +52,7 @@ def test_heartbeat_synchronizer_runs_at_configured_interval_until_stopped() -> N
|
|||||||
manager = DeviceManager()
|
manager = DeviceManager()
|
||||||
manager.register_device(
|
manager.register_device(
|
||||||
"device-a",
|
"device-a",
|
||||||
lambda: object(), # type: ignore[arg-type,return-value]
|
lambda: ConnectableDriver(), # type: ignore[arg-type,return-value]
|
||||||
)
|
)
|
||||||
calls: list[list[str]] = []
|
calls: list[list[str]] = []
|
||||||
|
|
||||||
@@ -75,3 +80,4 @@ def test_heartbeat_synchronizer_runs_at_configured_interval_until_stopped() -> N
|
|||||||
|
|
||||||
asyncio.run(scenario())
|
asyncio.run(scenario())
|
||||||
assert calls == [["device-a"], ["device-a"], ["device-a"]]
|
assert calls == [["device-a"], ["device-a"], ["device-a"]]
|
||||||
|
assert manager.status("device-a") == "busy"
|
||||||
|
|||||||
Reference in New Issue
Block a user