def __change_phase(self, phase, verbose=False): if self._phase == 'PENDING': context = "async/"+str(self.jobid)+"/phase" args = { "PHASE": str(phase)} data = self.connHandler.url_encode(args) response = self.connHandler.execute_tappost(subcontext=context, data=data, verbose=verbose) if verbose: print(response.status, response.reason) print(response.getheaders()) self.__last_phase_response_status = response.status if phase == 'RUN': # a request for RUN does not mean the server executes the job phase = 'QUEUED' if response.status != 200 and response.status != 303: errMsg = taputils.get_http_response_error(response) print(response.status, errMsg) raise requests.exceptions.HTTPError(errMsg) else: if response.status != 200: errMsg = taputils.get_http_response_error(response) print(response.status, errMsg) raise requests.exceptions.HTTPError(errMsg) self._phase = phase return response else: raise ValueError("Cannot start a job in phase: " + str(self._phase))
def get_error(self, verbose=False): """Returns the error associated to a job Parameters ---------- verbose : bool, optional, default 'False' flag to display information about the process Returns ------- The job error. """ subContext = "async/" + str(self.jobid) + "/error" resultsResponse = self.connHandler.execute_tapget(subContext) # resultsResponse = self.__readAsyncResults(self.__jobid, debug) if verbose: print(resultsResponse.status, resultsResponse.reason) print(resultsResponse.getheaders()) if (resultsResponse.status != 200 and resultsResponse.status != 303 and resultsResponse.status != 302): errMsg = taputils.get_http_response_error(resultsResponse) print(resultsResponse.status, errMsg) raise requests.exceptions.HTTPError(errMsg) else: if resultsResponse.status == 303 or resultsResponse.status == 302: # get location location = self.connHandler.\ find_header(resultsResponse.getheaders(), "location") if location is None: raise requests.exceptions.HTTPError("No location found " + "after redirection " + "was received (303)") if verbose: print("Redirect to %s", location) # load relativeLocation = self.__extract_relative_location(location, self.jobid) relativeLocationSubContext = "async/" + str(self.jobid) +\ "/" + str(relativeLocation) response = self.connHandler.\ execute_tapget(relativeLocationSubContext) response = self.__handle_redirect_if_required(response, verbose) isError = self.connHandler.\ check_launch_response_status(response, verbose, 200) if isError: errMsg = taputils.get_http_response_error(resultsResponse) print(resultsResponse.status, errMsg) raise requests.exceptions.HTTPError(errMsg) else: response = resultsResponse errMsg = taputils.get_http_response_error(response) return errMsg
def __load_async_job_results(self, debug=False): wjResponse, phase = self.wait_for_job_end() subContext = "async/" + str(self.jobid) + "/results/result" resultsResponse = self.connHandler.execute_tapget(subContext) # resultsResponse = self.__readAsyncResults(self.__jobid, debug) if debug: print(resultsResponse.status, resultsResponse.reason) print(resultsResponse.getheaders()) resultsResponse = self.__handle_redirect_if_required(resultsResponse, debug) isError = self.connHandler.\ check_launch_response_status(resultsResponse, debug, 200) self._phase = phase if phase == 'ERROR': errMsg = self.get_error(debug) raise SystemError(errMsg) else: if isError: errMsg = taputils.get_http_response_error(resultsResponse) print(resultsResponse.status, errMsg) raise requests.exceptions.HTTPError(errMsg) else: outputFormat = self.parameters['format'] results = utils.read_http_response(resultsResponse, outputFormat) self.set_results(results)
def get_phase(self, update=False): """Returns the job phase. May optionally update the job's phase. Parameters ---------- update : bool if True, the phase will by updated by querying the server before returning. Returns ------- The job phase """ if update: phase_request = "async/"+str(self.jobid)+"/phase" response = self.connHandler.execute_tapget(phase_request) self.__last_phase_response_status = response.status if response.status != 200: errMsg = taputils.get_http_response_error(response) print(response.status, errMsg) raise requests.exceptions.HTTPError(errMsg) self._phase = str(response.read().decode('utf-8')) return self._phase
def send_parameter(self, name=None, value=None, verbose=False): """Sends a job parameter (allowed in PENDING phase only). Parameters ---------- name : string Parameter name. value : string Parameter value. """ if self._phase == 'PENDING': # send post parameter/value context = "async/"+str(self.jobid) args = { name: str(value)} data = self.connHandler.url_encode(args) response = self.connHandler.execute_tappost(subcontext=context, data=data, verbose=verbose) if verbose: print(response.status, response.reason) print(response.getheaders()) self.__last_phase_response_status = response.status if response.status != 200: errMsg = taputils.get_http_response_error(response) print(response.status, errMsg) raise requests.exceptions.HTTPError(errMsg) return response else: raise ValueError("Cannot start a job in phase: " + str(self._phase))
def check_launch_response_status(self, response, debug, expected_response_status, raise_exception=True): """Checks the response status code Returns True if the response status code is the expected_response_status argument Parameters ---------- response : HTTP(s) response object, mandatory HTTP(s) response debug : bool, mandatory flag to display information about the process expected_response_status : int, mandatory expected response status code raise_exception : boolean, optional, default True if 'True' and the response status is not the expected one, an exception is raised. Returns ------- 'True' if the HTTP(s) response status is the provided 'expected_response_status' argument """ isError = False if response.status != expected_response_status: if debug: print(f"ERROR: {response.status}: {response.reason}") isError = True if isError and raise_exception: errMsg = taputils.get_http_response_error(response) print(response.status, errMsg) raise requests.exceptions.HTTPError(errMsg) else: return isError
def check_launch_response_status(self, response, debug, expected_response_status, raise_exception=True): isError = False if response.status != expected_response_status: if debug: print(f"ERROR: {response.status}: {response.reason}") isError = True if isError and raise_exception: errMsg = taputils.get_http_response_error(response) print(response.status, errMsg) raise requests.exceptions.HTTPError(errMsg) else: return isError