def _thread_func(_n_tries, errors=None): response = None if self.action.startswith('procedure.'): context['n_tries'] = _n_tries response = self._execute_procedure(**context) if response is not None: self._send_response(response) return response else: action = self.expand_value_from_context(self.action, **context) (module_name, method_name) = get_module_and_method_from_action(action) plugin = get_plugin(module_name) try: # Run the action args = self._expand_context(**context) args = self.expand_value_from_context(args, **context) response = plugin.run(method=method_name, **args) if response and response.is_error(): logger.warning(('Response processed with errors from ' + 'action {}: {}').format( action, str(response))) elif not response.disable_logging: logger.info('Processed response from action {}: {}'. format(action, str(response))) except Exception as e: # Retry mechanism plugin.logger.exception(e) logger.warning(('Uncaught exception while processing response ' + 'from action [{}]: {}').format(action, str(e))) errors = errors or [] if str(e) not in errors: errors.append(str(e)) response = Response(output=None, errors=errors) if _n_tries-1 > 0: logger.info('Reloading plugin {} and retrying'.format(module_name)) get_plugin(module_name, reload=True) response = _thread_func(_n_tries=_n_tries - 1, errors=errors) finally: self._send_response(response) return response
def _thread_func(n_tries): if self.action.startswith('procedure.'): context['n_tries'] = n_tries response = self._execute_procedure(**context) self._send_response(response) return response else: (module_name, method_name) = get_module_and_method_from_action(self.action) plugin = get_plugin(module_name) try: # Run the action args = self._expand_context(**context) response = plugin.run(method=method_name, **args) if response and response.is_error(): raise RuntimeError( 'Response processed with errors: {}'.format(response)) logging.info('Processed response from plugin {}: {}'.format( plugin, response)) except Exception as e: # Retry mechanism response = Response(output=None, errors=[str(e), traceback.format_exc()]) logging.exception(e) if n_tries: logging.info( 'Reloading plugin {} and retrying'.format(module_name)) get_plugin(module_name, reload=True) response = _thread_func(n_tries - 1) finally: self._send_response(response) return response
def _thread_func(_n_tries, errors=None): response = None try: if self.action.startswith('procedure.'): context['n_tries'] = _n_tries response = self._execute_procedure(**context) if response is not None: self._send_response(response) return response # utils.get_context is a special action that simply returns the current context elif self.action == 'utils.get_context': response = Response(output=context) self._send_response(response) return response else: action = self.expand_value_from_context( self.action, **context) (module_name, method_name) = get_module_and_method_from_action(action) plugin = get_plugin(module_name) except Exception as e: logger.exception(e) msg = 'Uncaught pre-processing exception from action [{}]: {}'.format( self.action, str(e)) logger.warning(msg) response = Response(output=None, errors=[msg]) self._send_response(response) return response try: # Run the action args = self._expand_context(**context) args = self.expand_value_from_context(args, **context) if isinstance(args, dict): response = plugin.run(method_name, **args) elif isinstance(args, list): response = plugin.run(method_name, *args) else: response = plugin.run(method_name, args) if not response: logger.warning( 'Received null response from action {}'.format(action)) else: if response.is_error(): logger.warning( ('Response processed with errors from ' + 'action {}: {}').format(action, str(response))) elif not response.disable_logging: logger.info( 'Processed response from action {}: {}'.format( action, str(response))) except (AssertionError, TimeoutError) as e: plugin.logger.exception(e) logger.warning('{} from action [{}]: {}'.format( type(e), action, str(e))) response = Response(output=None, errors=[str(e)]) except Exception as e: # Retry mechanism plugin.logger.exception(e) logger.warning( ('Uncaught exception while processing response ' + 'from action [{}]: {}').format(action, str(e))) errors = errors or [] if str(e) not in errors: errors.append(str(e)) response = Response(output=None, errors=errors) if _n_tries - 1 > 0: logger.info( 'Reloading plugin {} and retrying'.format(module_name)) get_plugin(module_name, reload=True) response = _thread_func(_n_tries=_n_tries - 1, errors=errors) finally: self._send_response(response) return response