fix(core): 修复启动模拟器前不会自动检测是否已启动的问题
This commit is contained in:
parent
41d4c4ce78
commit
71d75b76b2
|
@ -187,15 +187,18 @@ class KotoneBot:
|
||||||
if not os.path.exists(exe):
|
if not os.path.exists(exe):
|
||||||
user.error('「模拟器 exe 文件路径」对应的文件不存在!请检查路径是否正确。')
|
user.error('「模拟器 exe 文件路径」对应的文件不存在!请检查路径是否正确。')
|
||||||
raise FileNotFoundError(f'Emulator executable not found: {exe}')
|
raise FileNotFoundError(f'Emulator executable not found: {exe}')
|
||||||
logger.info('Starting custom backend...')
|
|
||||||
self.backend_instance = create_custom(
|
self.backend_instance = create_custom(
|
||||||
adb_ip=config.backend.adb_ip,
|
adb_ip=config.backend.adb_ip,
|
||||||
adb_port=config.backend.adb_port,
|
adb_port=config.backend.adb_port,
|
||||||
exe_path=exe
|
exe_path=exe
|
||||||
)
|
)
|
||||||
self.backend_instance.start()
|
if not self.backend_instance.running():
|
||||||
logger.info('Waiting for custom backend to be available...')
|
logger.info('Starting custom backend...')
|
||||||
self.backend_instance.wait_available()
|
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):
|
def run(self, tasks: list[Task], *, by_priority: bool = True):
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
import os
|
||||||
import subprocess
|
import subprocess
|
||||||
from psutil import process_iter
|
from psutil import process_iter
|
||||||
from .protocol import HostProtocol, Instance
|
from .protocol import HostProtocol, Instance
|
||||||
|
@ -35,6 +36,21 @@ class CustomInstance(Instance):
|
||||||
self.process.wait()
|
self.process.wait()
|
||||||
self.process = None
|
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:
|
def _type_check(ins: Instance) -> CustomInstance:
|
||||||
if not isinstance(ins, CustomInstance):
|
if not isinstance(ins, CustomInstance):
|
||||||
raise ValueError(f'Instance {ins} is not a CustomInstance')
|
raise ValueError(f'Instance {ins} is not a CustomInstance')
|
||||||
|
|
|
@ -37,8 +37,14 @@ class Instance:
|
||||||
adb_port: int
|
adb_port: int
|
||||||
adb_ip: str = '127.0.0.1'
|
adb_ip: str = '127.0.0.1'
|
||||||
|
|
||||||
def start(self): ...
|
def start(self):
|
||||||
def stop(self): ...
|
raise NotImplementedError()
|
||||||
|
|
||||||
|
def stop(self):
|
||||||
|
raise NotImplementedError()
|
||||||
|
|
||||||
|
def running(self) -> bool:
|
||||||
|
raise NotImplementedError()
|
||||||
|
|
||||||
def wait_available(self, timeout: float = 180):
|
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)
|
logger.info('Starting to wait for emulator %s(127.0.0.1:%d) to be available...', self.name, self.adb_port)
|
||||||
|
|
Loading…
Reference in New Issue