From a0b5275cc932946b7e74ce559fa1b7385a56a126 Mon Sep 17 00:00:00 2001 From: Eric Callahan Date: Sat, 23 Oct 2021 09:12:20 -0400 Subject: [PATCH] machine: add method to wait for service init Signed-off-by: Eric Callahan --- moonraker/components/machine.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/moonraker/components/machine.py b/moonraker/components/machine.py index 2237e47..cdd3730 100644 --- a/moonraker/components/machine.py +++ b/moonraker/components/machine.py @@ -9,6 +9,7 @@ import os import re import pathlib import logging +import asyncio import platform import distro @@ -83,8 +84,15 @@ class Machine: # Retreive list of services event_loop = self.server.get_event_loop() + self.init_evt = asyncio.Event() event_loop.register_callback(self._find_active_services) + async def wait_for_init(self, timeout: float = None) -> None: + try: + await asyncio.wait_for(self.init_evt.wait(), timeout) + except asyncio.TimeoutError: + pass + async def _handle_machine_request(self, web_request: WebRequest) -> str: ep = web_request.get_endpoint() if ep == "/machine/shutdown": @@ -141,7 +149,7 @@ class Machine: logging.exception(f"Error running cmd '{cmd}'") raise - def get_system_info(self) -> Dict[str, Dict[str, Any]]: + def get_system_info(self) -> Dict[str, Any]: return self.system_info def _get_sdcard_info(self) -> Dict[str, Any]: @@ -271,6 +279,7 @@ class Machine: self.system_info['available_services'] = avail_list self.system_info['service_state'] = self.available_services await self.update_service_status(notify=False) + self.init_evt.set() async def update_service_status(self, notify: bool = True) -> None: shell_cmd: SCMDComp = self.server.lookup_component('shell_command')