def get_task_result_status(self, kwargs): """ 批量查询job任务结果 """ result = None result_message = {} kwargs.update({ "bk_username": self.bk_username, }) for query_max_times in range(100): result = JobV3Api.get_job_instance_status(kwargs, raw=True) if result["result"]: is_finished = result["data"].get("finished") if is_finished: if result["data"].get("job_instance").get("status") != 3: # 错误需要捕捉到错误日志,打印到前端展示 step_instance_id = result["data"].get( "step_instance_list")[0]['step_instance_id'] kwargs['step_instance_id'] = step_instance_id kwargs['bk_cloud_id'] = bk_cloud_id result_message = self.get_task_ip_log(kwargs) break time.sleep(2) # 执行中则继续轮询 else: time.sleep(2) return result, json.dumps(result_message)
def schedule(self, data, parent_data, callback_data=None): act_info = data.get_one_of_inputs('act_info') bk_username = act_info['bk_username'] app_id = act_info['app_id'] if data.get_one_of_outputs('result_message') == 'skip': # 表示该节点已经内部跳过,不执行监听 self.finish_schedule() return True # 控制是否读取任务正确时读取返回日志的变量,默认false is_read_success_message = act_info.get('is_read_success_message') if is_read_success_message is None: is_read_success_message = False target_ips = data.get_one_of_outputs('target_ips') job_instance_id = data.get_one_of_outputs('job_instance_id') kwargs = { "bk_biz_id": app_id, "bk_username": bk_username, "job_instance_id": job_instance_id, "return_ip_result": True, } res = JobV3Api.get_job_instance_status(kwargs, raw=True) if not res['result']: return False if res['data']['finished']: self.finish_schedule() step_instance_id = res["data"].get("step_instance_list")[0]['step_instance_id'] kwargs['step_instance_id'] = step_instance_id kwargs['bk_cloud_id'] = bk_cloud_id if res["data"].get("job_instance").get("status") == 3: # job任务执行成功 if is_read_success_message: data.outputs.result_message = self.__get_task_ip_log(kwargs=kwargs, target_ips=target_ips) else: data.outputs.result_message = "success" else: # job结束后status不等于3 视为调用失败 data.outputs.result_message = self.__get_task_ip_log(kwargs=kwargs, target_ips=target_ips) return False return True