async def start_back_handler(self): try: self.startingVM = True logger.info('call adb_devices') await vmsH.adb_devices() logger.info('call vmsH.set_vm_config') vmsH.set_vm_config(self.vmid, self.extend_vm_info) logger.info("call vmsH.start_vm(self.vmid)") await vmsH.start_vm(self.vmid) tmp_vm_start_time = Utils.get_now_time() tmp_vm_max_start_use_time = 300 * 1000 # 最大的运行检测时间 while Utils.get_now_time( ) - tmp_vm_start_time <= tmp_vm_max_start_use_time: await sleep(2) is_running = await self.is_vm_running() if is_running: break # 再次判断是否正在运行 is_running = await self.is_vm_running() if not is_running: self.startingVM = False await self.stop_adb_process() await self.stop_back_handler(force=True) else: await sleep(3) logger.info('call adb_devices again ...') await vmsH.adb_devices() await sleep(30) logger.info("call create_sub_process") await self.create_sub_process() except Exception: logger.exception("Error:") finally: self.startingVM = False logger.info("start_back_handler end ...")
async def stop_back_handler(self, force=False): """ 关闭VM :param force: 是否强制关闭,默认:非强制 :return: """ await sleep(1) await vmsH.stop_vm(self.vmid) await sleep(5) is_running = await self.is_vm_running() if is_running or force: self.startingVM = False logger.info('Close VM failed ... vmid={}'.format(self.vmid)) else: self.start_time = Utils.get_now_time() * 10 # 表示不会被重新启动
async def create_sub_process(self): logger.info('call create_sub_process ... vmid={}'.format(self.vmid)) try: # 后台进程正在运行中,关闭掉 if self.get_back_proc_is_running(): logger.info( 'Found back_proc is running, must check stop it ... vmid={}' .format(self.vmid)) if (Utils.get_now_time() - self.start_time) < 60 * 1000: return # 如果当前时间和最开始的启动时间只是差10秒钟,那么直接返回,不关闭后台进程 self.stop_all_back_procs() self.start_time = Utils.get_now_time() logger.info( 'Must create a new process back handler ... vmid={}'.format( self.vmid)) # 根据只创建一个后台跟踪的方式 current_dir = os.path.dirname(os.path.abspath(__file__)) logger.info('current dir path = {}'.format(current_dir)) enable_ads_flag = 'false' if self.enable_ads: enable_ads_flag = 'true' cmd = "{0} --enableads {1}".format(self.start_cmd, enable_ads_flag) logger.info('cmd = {}'.format(cmd)) proc = subprocess.Popen(cmd, cwd=current_dir, shell=False, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True) if proc: self.back_proc = proc except Exception: logger.exception('ERROR:')
def __init__(self, vmid, vmname, enable, enable_ads, start_cmd, appium_cmd, max_run_time, extend_vm_info): object.__init__(self) self.vmid = vmid self.vmname = vmname self.enable = enable self.enable_ads = enable_ads self.start_cmd = start_cmd self.start_time = Utils.get_now_time() self.appium_cmd = appium_cmd self.appium_cmd_handler = None self.max_run_time = max_run_time self.extend_vm_info = extend_vm_info self.back_proc = None self.startingVM = False self.busy = False
async def is_job_overtime(self): is_running = await self.is_vm_running() is_starting = await self.is_vm_starting() if not is_running or is_starting: return False running_time = Utils.get_now_time() - self.start_time # 判断时候有VM可以停止的标记文件 current_dir = os.path.dirname(os.path.abspath(__file__)) flag_file = os.path.join(current_dir, 'can-stop-vmid-{}-f.flag'.format(self.vmid)) run_overtime_case1 = running_time >= self.max_run_time and os.path.exists( flag_file) run_overtime_case2 = (running_time - self.max_run_time) >= 8 * 60 * 1000 # 严重超时8分钟 if run_overtime_case1 or run_overtime_case2: return True return False