def process_job(self, job_request): """ Validate, execute, and run the job request, wrapping it with any applicable job middleware. :param job_request: The job request :type job_request: dict :return: A `JobResponse` object :rtype: JobResponse :raise: JobError """ try: # Validate JobRequest message validation_errors = [ Error( code=error.code, message=error.message, field=error.pointer, ) for error in (JobRequestSchema.errors(job_request) or []) ] if validation_errors: raise JobError(errors=validation_errors) # Add the client object in case a middleware wishes to use it job_request['client'] = self.make_client(job_request['context']) # Add the async event loop in case a middleware wishes to use it job_request['async_event_loop'] = self._async_event_loop if self._async_event_loop_thread: job_request[ 'run_coroutine'] = self._async_event_loop_thread.run_coroutine else: job_request['run_coroutine'] = None # Build set of middleware + job handler, then run job wrapper = self.make_middleware_stack( [m.job for m in self.middleware], self.execute_job, ) job_response = wrapper(job_request) if 'correlation_id' in job_request['context']: job_response.context['correlation_id'] = job_request[ 'context']['correlation_id'] except JobError as e: self.metrics.counter('server.error.job_error').increment() job_response = JobResponse(errors=e.errors, ) except Exception as e: # Send an error response if no middleware caught this. # Formatting the error might itself error, so try to catch that self.metrics.counter('server.error.unhandled_error').increment() return self.handle_job_exception(e) return job_response
def process_job(self, job_request): """ Validate, execute, and run Job-level middleware for JobRequests. Args: job_request: a JobRequest dictionary. Returns: A JobResponse instance. """ try: # Validate JobRequest message validation_errors = [ Error( code=ERROR_CODE_INVALID, message=error.message, field=error.pointer, ) for error in (JobRequestSchema.errors(job_request) or []) ] if validation_errors: raise JobError(errors=validation_errors) # Add a client router in case a middleware wishes to use it job_request['client'] = self.make_client(job_request['context']) # Build set of middleware + job handler, then run job wrapper = self.make_middleware_stack( [m.job for m in self.middleware], self.execute_job, ) job_response = wrapper(job_request) except JobError as e: self.metrics.counter('server.error.job_error').increment() job_response = JobResponse(errors=e.errors, ) except Exception as e: # Send an error response if no middleware caught this. # Formatting the error might itself error, so try to catch that self.metrics.counter('server.error.unhandled_error').increment() return self.handle_error(e) return job_response
def test_invalid_actions(self, job): job['actions'] = 'invalid actions type' errors = JobRequestSchema.errors(job) assert len(errors) == 1 assert errors[0].pointer == 'actions'
def test_empty_actions(self, job): job['actions'] = [] errors = JobRequestSchema.errors(job) assert not errors
def test_missing_actions(self, job): del job['actions'] errors = JobRequestSchema.errors(job) assert len(errors) == 1 assert errors[0].pointer == 'actions'
def test_invalid_control(self, job): job['control'] = 'invalid control type' errors = JobRequestSchema.errors(job) assert len(errors) == 1 assert errors[0].pointer == 'control'
def test_missing_control(self, job): del job['control'] errors = JobRequestSchema.errors(job) assert len(errors) == 1 assert errors[0].pointer == 'control'
def test_valid_job(self, job): errors = JobRequestSchema.errors(job) assert not errors
def test_empty_actions(self, job): job['actions'] = [] errors = JobRequestSchema.errors(job) assert len(errors) == 1 assert errors[0].pointer == 'actions'