fix(core): 修复启动模拟器前不会自动检测是否已启动的问题

This commit is contained in:
XcantloadX 2025-03-07 15:36:40 +08:00
parent 41d4c4ce78
commit 71d75b76b2
3 changed files with 31 additions and 6 deletions

View File

@ -187,15 +187,18 @@ class KotoneBot:
if not os.path.exists(exe):
user.error('「模拟器 exe 文件路径」对应的文件不存在!请检查路径是否正确。')
raise FileNotFoundError(f'Emulator executable not found: {exe}')
logger.info('Starting custom backend...')
self.backend_instance = create_custom(
adb_ip=config.backend.adb_ip,
adb_port=config.backend.adb_port,
exe_path=exe
)
self.backend_instance.start()
logger.info('Waiting for custom backend to be available...')
self.backend_instance.wait_available()
if not self.backend_instance.running():
logger.info('Starting custom backend...')
self.backend_instance.start()
logger.info('Waiting for custom backend to be available...')
self.backend_instance.wait_available()
else:
logger.info('Custom backend "%s" already running.', self.backend_instance)
def run(self, tasks: list[Task], *, by_priority: bool = True):
"""

View File

@ -1,3 +1,4 @@
import os
import subprocess
from psutil import process_iter
from .protocol import HostProtocol, Instance
@ -35,6 +36,21 @@ class CustomInstance(Instance):
self.process.wait()
self.process = None
@override
def running(self) -> bool:
if self.process is not None:
return True
else:
process_name = os.path.basename(self.exe_path)
p = next((proc for proc in process_iter() if proc.name() == process_name), None)
if p:
return True
else:
return False
def __repr__(self) -> str:
return f'CustomInstance(#{self.id}# at "{self.exe_path}" with {self.adb_ip}:{self.adb_port})'
def _type_check(ins: Instance) -> CustomInstance:
if not isinstance(ins, CustomInstance):
raise ValueError(f'Instance {ins} is not a CustomInstance')

View File

@ -37,8 +37,14 @@ class Instance:
adb_port: int
adb_ip: str = '127.0.0.1'
def start(self): ...
def stop(self): ...
def start(self):
raise NotImplementedError()
def stop(self):
raise NotImplementedError()
def running(self) -> bool:
raise NotImplementedError()
def wait_available(self, timeout: float = 180):
logger.info('Starting to wait for emulator %s(127.0.0.1:%d) to be available...', self.name, self.adb_port)