def _validate_response(response): """Validate the response from the QCG PJM. This method checks the format of the response and exit code. Args: response (dict): deserialized JSON response Returns: dict: validated response data Raises: InternalError: in case the response format is invalid ConnectionError: in case of non zero exit code """ if not isinstance(response, dict) or 'code' not in response: raise errors.InternalError('Invalid reply from the service') if response['code'] != 0: if 'message' in response: raise errors.ConnectionError('Request failed - {}'.format( response['message'])) raise errors.ConnectionError('Request failed') if 'data' not in response: raise errors.InternalError('Invalid reply from the service') return response['data']
def _validate_response_wo_data(response): """Validate the response from the QCG PJM. This method checks the format of the response and exit code. Unlike the ``_validate_seponse`` this method not checks existence of the 'data' element and returns the full reponse. Args: response (dict): deserialized JSON response Returns: dict: response message Raises: InternalError: in case the response format is invalid ConnectionError: in case of non zero exit code """ if not isinstance(response, dict) or 'code' not in response: raise errors.InternalError('Invalid reply from the service') if response['code'] != 0: if 'message' in response: raise errors.ConnectionError('Request failed - %s' % response['message']) raise errors.ConnectionError('Request failed') return response
def wait4(self, names): """Wait for finish of specific jobs. This method waits until all specified jobs finish its execution (successfully or not). The QCG-PilotJob manager is periodically polled about status of not finished jobs. The poll interval (2 sec by default) can be changed by defining a 'poll_delay' key with appropriate value (in seconds) in configuration of instance. Args: names (str|list(str)): list of job names to get detailed information about Returns: dict - a map with job names and their terminal status Raises: InternalError: in case the response format is invalid ConnectionError: in case of non zero exit code, or if connection has not been established yet """ if isinstance(names, str): job_names = [names] else: job_names = list(names) _logger.info("waiting for finish of %d jobs", len(job_names)) result = {} not_finished = job_names while len(not_finished) > 0: try: jobs_status = self.status(not_finished) not_finished = [] for job_name, job_data in jobs_status['jobs'].items(): if 'status' not in job_data['data'] or job_data[ 'status'] != 0 or 'data' not in job_data: raise errors.InternalError( "Missing job's {} data".format(job_name)) if not Manager.is_status_finished( job_data['data']['status']): not_finished.append(job_name) else: result[job_name] = job_data['data']['status'] if len(not_finished) > 0: _logger.info("still %d jobs not finished", len(not_finished)) time.sleep(self._poll_delay) except Exception as exc: raise errors.ConnectionError(exc.args[0]) _logger.info("all jobs finished") return result
def list(self): """List all jobs. Return a list of all job names registered in the QCG PJM. Beside the name, each job will contain additional data, like: status (str) - current job status messages (str, optional) - error message generated during job processing inQueue (int, optional) - current job position in scheduling queue Returns: dict: dictionary with job names and attributes Raises: InternalError - in case of unexpected result format see _send_and_validate_result """ data = self._send_and_validate_result({"request": "listJobs"}) if 'jobs' not in data: raise errors.InternalError('Rquest failed - missing jobs data') return data['jobs']
def submit(self, jobs): """Submit jobs. Args: jobs (Jobs): the job descriptions to submit Returns: list(str): list of submitted job names Raises: InternalError - in case of unexpected result format see _send_and_validate_result """ data = self._send_and_validate_result({ "request": "submit", "jobs": jobs.ordered_jobs() }) if 'submitted' not in data or 'jobs' not in data: raise errors.InternalError('Missing response data') return data['jobs']