def render_task_response(self, task_job): """formats and returns the task rpc jobs result""" created_at = str(task_job.created_at) enqueued_at = str(task_job.enqueued_at) started_at = str(task_job.started_at) ended_at = str(task_job.ended_at) try: current_time = datetime.datetime.utcnow() created_parsed_time = datetime.datetime.strptime(created_at, "%Y-%m-%d %H:%M:%S.%f") # if enqueued but not started calculate time if enqueued_at != "None" and enqueued_at and started_at == "None": parsed_time = datetime.datetime.strptime(enqueued_at, "%Y-%m-%d %H:%M:%S.%f") task_job.meta["enqueued_elapsed_seconds"] = (current_time - parsed_time).seconds task_job.save() # if created but not finished calculate time if ended_at != "None" and ended_at: parsed_time = datetime.datetime.strptime(ended_at, "%Y-%m-%d %H:%M:%S.%f") task_job.meta["total_elapsed_seconds"] = (parsed_time - created_parsed_time).seconds task_job.save() elif ended_at == "None": task_job.meta["total_elapsed_seconds"] = (current_time - created_parsed_time).seconds task_job.save() # clean up vars for response created_at = None if created_at == "None" else created_at enqueued_at = None if enqueued_at == "None" else enqueued_at started_at = None if started_at == "None" else started_at ended_at = None if ended_at == "None" else ended_at except Exception as e: log.error(f"render_task_response : {str(e)}") pass resultdata = None resultdata = Response(status="success", data={ "task_id": task_job.get_id(), "created_on": created_at, "task_queue": task_job.description, "task_meta": { "enqueued_at": enqueued_at, "started_at": started_at, "ended_at": ended_at, "enqueued_elapsed_seconds": task_job.meta["enqueued_elapsed_seconds"], "total_elapsed_seconds": task_job.meta["total_elapsed_seconds"] }, "task_status": task_job.get_status(), "task_result": task_job.result, "task_errors": task_job.meta["errors"] }).dict() return resultdata
def render_netpalm_payload(job_result={}): """in band rpc job result renderer""" try: job = get_current_job() resultdata = Response(status="success", data={"task_id": job.id, "created_on": job.created_at.strftime("%Y-%m-%d %H:%M:%S.%f"), "task_queue": job.description, "task_status": "finished", "task_result": job_result, "task_errors": job.meta["errors"] }).dict() return resultdata except Exception as e: return e
def retrieve_task_result(self, netpalm_response: Response): """ waits for the task to complete the returns the result """ if isinstance(netpalm_response, dict): req_data = netpalm_response else: req_data = netpalm_response.dict(exclude_none=True) if req_data["status"] == "success": task_id = req_data["data"]["task_id"] while True: r = self.fetchtask(task_id=task_id) if (r["data"]["task_status"] == "finished") or (r["data"]["task_status"] == "failed"): return r time.sleep(0.3) else: return req_data