Exemplo n.º 1
0
 def add_task(self, task: Union[Tuple[int, str, str, dict], Tuple[int, str, str]], continue_, rec_addr):
     """
     向优先级队列中增加一个task
     该task为六元组,(priority, account, taskname,rec_addr, task, continue_)
     """
     if len(task) == 3:
         task = (0 - task[0], task[1], task[2], rec_addr, AutomatorRecorder.gettask(task[2]), continue_)
     else:
         task = (0 - task[0], task[1], task[2], rec_addr, task[3], continue_)  # 最大优先队列
     self._add_task(task)
Exemplo n.º 2
0
def execute(continue_=False, max_retry=3):
    """
    执行脚本
    :param continue_: 是否继续执行上次没执行完的脚本
    :param max_retry: 最大报错重试次数
    """
    try:
        # 连接adb与uiautomator
        devices = connect()
        # 读取账号
        all_accounts = readjson()
        # 读取任务,把有任务的账号加入队列
        accounts = []
        tasks = []
        for acc in all_accounts:
            AR = AutomatorRecorder(acc)
            user_dict = AR.getuser()
            if user_dict["taskfile"] == "":
                # 无任务,跳过
                continue
            run_status = AR.get_run_status()
            if run_status["error"] is not None:
                # 报错
                print("账号 ", acc, " 含有未解决的错误:", run_status["error"])
                continue
            elif run_status["finished"]:
                # 已经完成的
                print("账号 ", acc, " 已经完成!跳过。")
                continue
            try:
                tas = AR.gettask(user_dict["taskfile"])
            except Exception as e:
                print("账号 ", acc, " 所读取的任务文件 ", user_dict["task_file"], "有异常:",
                      e, "跳过。")
                continue
            # 完好的账号
            tasks += [tas]
            accounts += [acc]
            print("导入任务: 账号 ", acc, " 任务 ", user_dict["taskfile"], " 进度 ",
                  run_status["current"])

        # 这个队列用来保存设备, 初始化的时候先把所有的模拟器设备放入队列
        queue = Manager().Queue()

        # 进程池参数列表
        params = list()
        for acc, tas in zip(accounts, tasks):
            params.append((acc, tas, queue, continue_, max_retry))

        # 初始化队列, 先把所有的模拟器设备放入队列
        for device in devices:
            queue.put(device)

        # 这里是脱离了runmain的异步
        address = queue.get()
        a = Automator(address)
        # 传递程序启动的flags
        Multithreading({}).state_sent_resume()
        # 随着进程的异步
        a.program_start_async()
        # 放回address
        queue.put(address)

        # 进程池大小为模拟器数量, 保证同一时间最多有模拟器数量个进程在运行
        if trace_exception_for_debug:
            runmain(params[0])
        else:
            with Pool(len(devices)) as mp:
                mp.map(runmain, params)

        # 传递程序关闭的flags
        Multithreading({}).state_sent_pause()

        for _ in range(len(devices)):
            address = queue.get()
            a = Automator(address)
            # 关闭PCR
            a.d.app_stop("com.bilibili.priconne")
        # 退出adb
        os.system('cd adb & adb kill-server')
        pcr_log('admin').write_log(level='info', message='任务全部完成')
        pcr_log('admin').server_bot('', message='任务全部完成')
        if end_shutdown:
            os.system("shutdown -s -f -t 120")
    except Exception as e:
        if trace_exception_for_debug:
            raise e
        pcr_log("admin").write_log(level="error", message=f"execute发生了错误:{e}")