def start_task(task: Task): print('start_task') print(task.task_id) tc = TaskController() dm = DataManager() try: print('script path:' + get_script_path(task)) # codes from git which contain docker-compose.yml if is_git_files(task): sub = subprocess.Popen( ['docker-compose', 'up'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, cwd=get_script_work_path(task), bufsize=1) # uploaded codes else: sub = subprocess.Popen( ['python3.6', get_script_path(task)], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, cwd=get_script_work_path(task), bufsize=1) for line in iter(sub.stdout.readline, b''): if line: print(line) dm.add_custom_log(CustomLog( task_id=task.task_id, content=line, time=int(time.time()) )) sub.stdout.close() if sub.returncode == 0: print('Subprogram success') tc.finish_task(task.task_id) else: print('Subprogram failed') tc.add_custom_log_callback(CustomLog( task_id=task.task_id, content=sub.stdout.read().decode('utf8'), time=int(time.time()) )) tc.stop_task(task.task_id) except Exception as e: print('[error]' + str(e)) tc.add_custom_log_callback(CustomLog( task_id=task.task_id, content=str(e), time=int(time.time()) ))
def clone_error(request_task, error): # add pulling log db.add_pulling_log(task_log=CustomLog(task_id=request_task.task_id, content=str(error), time=int(time.time()))) # update status request_task.status = Task.PULL_FAILURE db.update_task(task=request_task)
def clone_finished(request_task): # update status request_task.status = Task.READY db.update_task(task=request_task) # add log db.add_pulling_log( task_log=CustomLog(task_id=request_task.task_id, content=f"clone {request_task.file} finished.", time=int(time.time())))
def stop_task(task: Task) -> bool: if not exist_task(task): return False sub: subprocess.Popen = tasks[task] sub.kill() tc = TaskController() tc.add_custom_log_callback(CustomLog( task_id=task.task_id, content='[stop]', time=int(time.time()) )) return True
def AddPullingLog(self, request, context): t: CustomLog = request.custom_log if t.task_id == 0: return data_manager_pb2.AddCustomLogResp( resp=data_manager_pb2.Response( code=10001, message="task id can't be zero")) if t.content == '': return data_manager_pb2.AddCustomLogResp( resp=data_manager_pb2.Response( code=10001, message="content can't be empty")) if time == 0: return data_manager_pb2.AddCustomLogResp( resp=data_manager_pb2.Response(code=10001, message="time can't be zero")) self.db.add_pulling_log(CustomLog(t.task_id, t.content, t.time)) return data_manager_pb2.AddPullingLogResp( resp=data_manager_pb2.Response(code=0, message="success"))
def send_message(self, msg): return self.stub.SendMessage(task_controller_pb2.SendMessageReq( message=msg )) if __name__ == '__main__': tc = TaskController() # task = Task( # task_id=1, # name="test_task", # create_time=int(time.time()), # union_train=0, # edge_nodes='nodes', # file='train.py' # ) # dm.add_task(task) # dm.start_task(2, int(time.time())) # dm.stop_task(2, int(time.time())) # dm.finish_task(2, int(time.time())) # ret = dm.get_all_tasks() # print(ret.resp) log = CustomLog( task_id=1, content='Test!!', time=int(time.time()) ) tc.add_custom_log_callback(log)