class ExternalBackend(object): """REANA yadage external packtivity backend class.""" def __init__(self): """Initialize the REANA packtivity backend.""" self.config = packconfig() self.rjc_api_client = RJC_API_Client('reana-job-controller') self._fail_info = None def submit(self, spec, parameters, state, metadata): """Submit a yadage packtivity to RJC.""" parameters, state = finalize_inputs(parameters, state) job = build_job(spec['process'], parameters, state, self.config) if 'command' in job: prettified_cmd = job['command'] wrapped_cmd = make_oneliner(job) elif 'script' in job: prettified_cmd = job['script'] wrapped_cmd = make_script(job) image = spec['environment']['image'] imagetag = spec['environment'].get('imagetag', '') if imagetag: image = image + ":" + imagetag kerberos = None compute_backend = None kubernetes_uid = None resources = spec['environment'].get('resources', None) if resources: for item in resources: if 'kerberos' in item.keys(): kerberos = item['kerberos'] if 'compute_backend' in item.keys(): compute_backend = item['compute_backend'] if 'kubernetes_uid' in item.keys(): kubernetes_uid = item['kubernetes_uid'] log.info('state context is {0}'.format(state)) log.info('would run job {0}'.format(job)) state.ensure() log.info('submitting!') workflow_uuid = os.getenv('workflow_uuid', 'default') job_request_body = { 'workflow_uuid': workflow_uuid, 'image': image, 'cmd': wrapped_cmd, 'prettified_cmd': prettified_cmd, 'workflow_workspace': os.getenv('workflow_workspace', 'default'), 'job_name': metadata['name'], 'cvmfs_mounts': MOUNT_CVMFS, } if compute_backend: job_request_body['compute_backend'] = compute_backend if kerberos: job_request_body['kerberos'] = kerberos if kubernetes_uid: job_request_body['kubernetes_uid'] = kubernetes_uid job_id = self.rjc_api_client.submit(**job_request_body) log.info('submitted job:{0}'.format(job_id)) message = {"job_id": str(job_id)} workflow_uuid = os.getenv('workflow_uuid', 'default') status_running = 1 try: publisher = REANAWorkflowStatusPublisher() publisher.publish_workflow_status(workflow_uuid, status_running, message=message) except Exception as e: log.info('Status: workflow - {workflow_uuid} ' 'status - {status} message - {message}'.format( workflow_uuid=workflow_uuid, status=status_running, message=message)) log.info('workflow status publish failed: {0}'.format(e)) return ReanaExternalProxy(jobproxy=job_id, spec=spec, pardata=parameters, statedata=state) def result(self, resultproxy): """Retrieve the result of a packtivity run by RJC.""" resultproxy.pardata, resultproxy.statedata \ = finalize_inputs(resultproxy.pardata, resultproxy.statedata) return publish(resultproxy.spec['publisher'], resultproxy.pardata, resultproxy.statedata, self.config) def _get_state(self, resultproxy): """Get the packtivity state.""" status_res = self.rjc_api_client.check_status( resultproxy.jobproxy['job_id']) return status_res['status'] def ready(self, resultproxy): """Check if a packtivity is finished.""" return self._get_state(resultproxy) != 'started' def successful(self, resultproxy): """Check if the packtivity was successful.""" return self._get_state(resultproxy) == 'succeeded' def fail_info(self, resultproxy): """Retrieve the fail info.""" self._fail_info += "\nraw info: {}".format(resultproxy) return self._fail_info
class ExternalBackend: """REANA yadage external packtivity backend class. Submits jobs and fetches their statuses from the JobController. """ def __init__(self): """Initialize the REANA packtivity backend.""" self.config = packconfig() self.rjc_api_client = RJC_API_Client("reana-job-controller") self.jobs_statuses = {} self._fail_info = "" @staticmethod def _get_resources(resources: List[Union[Dict, Any]]) -> Dict[str, Any]: parameters = {} def set_parameter(resource: Dict[str, Any], key: str) -> None: if key in resource and resource[key] is not None: parameters[key] = resource[key] for item in resources: if not isinstance(item, dict): log.info( "REANA only supports dictionary entries for resources. " f'"{item}" value is not formatted in such a way and will be ignored.' ) continue set_parameter(item, "kerberos") set_parameter(item, "compute_backend") set_parameter(item, "kubernetes_uid") set_parameter(item, "kubernetes_memory_limit") set_parameter(item, "kubernetes_job_timeout") set_parameter(item, "unpacked_img") set_parameter(item, "voms_proxy") set_parameter(item, "htcondor_max_runtime") set_parameter(item, "htcondor_accounting_group") return parameters def submit( # noqa: C901 self, spec, parameters, state, metadata # noqa: C901 ) -> ReanaExternalProxy: # noqa: C901 """Submit a yadage packtivity to RJC.""" parameters, state = finalize_inputs(parameters, state) job = build_job(spec["process"], parameters, state, self.config) log.debug(f"state context is {state}") state.ensure() if "command" in job: prettified_cmd = wrapped_cmd = job["command"] elif "script" in job: prettified_cmd = job["script"] wrapped_cmd = make_script(job) image = spec["environment"]["image"] imagetag = spec["environment"].get("imagetag", "") if imagetag: image = f"{image}:{imagetag}" resources = spec["environment"].get("resources", []) resources_parameters = self._get_resources(resources) log.debug(f"would run job {job}") workflow_uuid = os.getenv("workflow_uuid", "default") job_request_body = { "workflow_uuid": workflow_uuid, "image": image, "cmd": wrapped_cmd, "prettified_cmd": prettified_cmd, "workflow_workspace": os.getenv("workflow_workspace", "default"), "job_name": metadata["name"], "cvmfs_mounts": MOUNT_CVMFS, **resources_parameters, } job_submit_response = self.rjc_api_client.submit(**job_request_body) job_id = job_submit_response.get("job_id") log.info(f"Submitted job with id: {job_id}") return ReanaExternalProxy(jobproxy=job_submit_response, spec=spec, pardata=parameters, statedata=state) def result(self, resultproxy: ReanaExternalProxy): """Retrieve the result of a packtivity run by RJC.""" resultproxy.pardata, resultproxy.statedata = finalize_inputs( resultproxy.pardata, resultproxy.statedata) return publish( resultproxy.spec["publisher"], resultproxy.pardata, resultproxy.statedata, self.config, ) def _get_job_status_from_controller(self, job_id: str) -> str: response = self.rjc_api_client.check_status(job_id) return response["status"] def _refresh_job_status(self, job_id: str) -> None: self.jobs_statuses[job_id] = self._get_job_status_from_controller( job_id) def _should_refresh_job_status(self, job_id: str) -> bool: if job_id in self.jobs_statuses: status = self.jobs_statuses[job_id] return status == JobStatus.started else: return True def _get_state(self, resultproxy: ReanaExternalProxy) -> str: """Get the packtivity state.""" job_id = resultproxy.jobproxy["job_id"] if self._should_refresh_job_status(job_id): self._refresh_job_status(job_id) return self.jobs_statuses[job_id] def ready(self, resultproxy: ReanaExternalProxy) -> bool: """Check if a packtivity is finished.""" return self._get_state(resultproxy) != JobStatus.started def successful(self, resultproxy: ReanaExternalProxy) -> bool: """Check if the packtivity was successful.""" return self._get_state(resultproxy) == JobStatus.finished def fail_info(self, resultproxy): """Retrieve the fail info.""" self._fail_info += f"\nraw info: {resultproxy}" return self._fail_info
class ExternalBackend(object): """REANA yadage external packtivity backend class.""" def __init__(self): """Initialize the REANA packtivity backend.""" self.config = packconfig() self.rjc_api_client = RJC_API_Client("reana-job-controller") self._fail_info = None def submit(self, spec, parameters, state, metadata): """Submit a yadage packtivity to RJC.""" parameters, state = finalize_inputs(parameters, state) job = build_job(spec["process"], parameters, state, self.config) if "command" in job: prettified_cmd = job["command"] wrapped_cmd = make_oneliner(job) elif "script" in job: prettified_cmd = job["script"] wrapped_cmd = make_script(job) image = spec["environment"]["image"] imagetag = spec["environment"].get("imagetag", "") if imagetag: image = image + ":" + imagetag kerberos = None compute_backend = None kubernetes_uid = None unpacked_img = None voms_proxy = None resources = spec["environment"].get("resources", None) if resources: for item in resources: if "kerberos" in item.keys(): kerberos = item["kerberos"] if "compute_backend" in item.keys(): compute_backend = item["compute_backend"] if "kubernetes_uid" in item.keys(): kubernetes_uid = item["kubernetes_uid"] if "unpacked_img" in item.keys(): unpacked_img = item["unpacked_img"] if "voms_proxy" in item.keys(): voms_proxy = item["voms_proxy"] log.info("state context is {0}".format(state)) log.info("would run job {0}".format(job)) state.ensure() log.info("submitting!") workflow_uuid = os.getenv("workflow_uuid", "default") job_request_body = { "workflow_uuid": workflow_uuid, "image": image, "cmd": wrapped_cmd, "prettified_cmd": prettified_cmd, "workflow_workspace": os.getenv("workflow_workspace", "default"), "job_name": metadata["name"], "cvmfs_mounts": MOUNT_CVMFS, } if compute_backend: job_request_body["compute_backend"] = compute_backend if kerberos: job_request_body["kerberos"] = kerberos if kubernetes_uid: job_request_body["kubernetes_uid"] = kubernetes_uid if unpacked_img: job_request_body["unpacked_img"] = unpacked_img if voms_proxy: job_request_body["voms_proxy"] = voms_proxy job_id = self.rjc_api_client.submit(**job_request_body) log.info("submitted job:{0}".format(job_id)) message = {"job_id": str(job_id)} workflow_uuid = os.getenv("workflow_uuid", "default") status_running = 1 try: publisher = REANAWorkflowStatusPublisher() publisher.publish_workflow_status(workflow_uuid, status_running, message=message) except Exception as e: log.info("Status: workflow - {workflow_uuid} " "status - {status} message - {message}".format( workflow_uuid=workflow_uuid, status=status_running, message=message)) log.info("workflow status publish failed: {0}".format(e)) return ReanaExternalProxy(jobproxy=job_id, spec=spec, pardata=parameters, statedata=state) def result(self, resultproxy): """Retrieve the result of a packtivity run by RJC.""" resultproxy.pardata, resultproxy.statedata = finalize_inputs( resultproxy.pardata, resultproxy.statedata) return publish( resultproxy.spec["publisher"], resultproxy.pardata, resultproxy.statedata, self.config, ) def _get_state(self, resultproxy): """Get the packtivity state.""" status_res = self.rjc_api_client.check_status( resultproxy.jobproxy["job_id"]) return status_res["status"] def ready(self, resultproxy): """Check if a packtivity is finished.""" return self._get_state(resultproxy) != "started" def successful(self, resultproxy): """Check if the packtivity was successful.""" return self._get_state(resultproxy) == "succeeded" def fail_info(self, resultproxy): """Retrieve the fail info.""" self._fail_info += "\nraw info: {}".format(resultproxy) return self._fail_info