| Conditions | 11 |
| Total Lines | 86 |
| Lines | 0 |
| Ratio | 0 % |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like st2actions.container.RunnerContainer._do_run() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | # Licensed to the StackStorm, Inc ('StackStorm') under one or more |
||
| 89 | def _do_run(self, runner, runnertype_db, action_db, liveaction_db): |
||
| 90 | # Create a temporary auth token which will be available |
||
| 91 | # for the duration of the action execution. |
||
| 92 | runner.auth_token = self._create_auth_token(runner.context) |
||
| 93 | |||
| 94 | updated_liveaction_db = None |
||
| 95 | try: |
||
| 96 | # Finalized parameters are resolved and then rendered. This process could |
||
| 97 | # fail. Handle the exception and report the error correctly. |
||
| 98 | try: |
||
| 99 | runner_params, action_params = param_utils.render_final_params( |
||
| 100 | runnertype_db.runner_parameters, action_db.parameters, liveaction_db.parameters, |
||
| 101 | liveaction_db.context) |
||
| 102 | runner.runner_parameters = runner_params |
||
| 103 | except ParamException as e: |
||
| 104 | raise actionrunner.ActionRunnerException(str(e)) |
||
| 105 | |||
| 106 | LOG.debug('Performing pre-run for runner: %s', runner.runner_id) |
||
| 107 | runner.pre_run() |
||
| 108 | |||
| 109 | # Mask secret parameters in the log context |
||
| 110 | resolved_action_params = ResolvedActionParameters(action_db=action_db, |
||
| 111 | runner_type_db=runnertype_db, |
||
| 112 | runner_parameters=runner_params, |
||
| 113 | action_parameters=action_params) |
||
| 114 | extra = {'runner': runner, 'parameters': resolved_action_params} |
||
| 115 | LOG.debug('Performing run for runner: %s' % (runner.runner_id), extra=extra) |
||
|
|
|||
| 116 | (status, result, context) = runner.run(action_params) |
||
| 117 | |||
| 118 | try: |
||
| 119 | result = json.loads(result) |
||
| 120 | except: |
||
| 121 | pass |
||
| 122 | |||
| 123 | action_completed = status in action_constants.LIVEACTION_COMPLETED_STATES |
||
| 124 | if isinstance(runner, AsyncActionRunner) and not action_completed: |
||
| 125 | self._setup_async_query(liveaction_db.id, runnertype_db, context) |
||
| 126 | except: |
||
| 127 | LOG.exception('Failed to run action.') |
||
| 128 | _, ex, tb = sys.exc_info() |
||
| 129 | # mark execution as failed. |
||
| 130 | status = action_constants.LIVEACTION_STATUS_FAILED |
||
| 131 | # include the error message and traceback to try and provide some hints. |
||
| 132 | result = {'error': str(ex), 'traceback': ''.join(traceback.format_tb(tb, 20))} |
||
| 133 | context = None |
||
| 134 | finally: |
||
| 135 | # Log action completion |
||
| 136 | extra = {'result': result, 'status': status} |
||
| 137 | LOG.debug('Action "%s" completed.' % (action_db.name), extra=extra) |
||
| 138 | |||
| 139 | # Always clean-up the auth_token |
||
| 140 | try: |
||
| 141 | LOG.debug('Setting status: %s for liveaction: %s', status, liveaction_db.id) |
||
| 142 | updated_liveaction_db = self._update_live_action_db(liveaction_db.id, status, |
||
| 143 | result, context) |
||
| 144 | except: |
||
| 145 | error = 'Cannot update LiveAction object for id: %s, status: %s, result: %s.' % ( |
||
| 146 | liveaction_db.id, status, result) |
||
| 147 | LOG.exception(error) |
||
| 148 | raise |
||
| 149 | |||
| 150 | executions.update_execution(updated_liveaction_db) |
||
| 151 | extra = {'liveaction_db': updated_liveaction_db} |
||
| 152 | LOG.debug('Updated liveaction after run', extra=extra) |
||
| 153 | |||
| 154 | # Deletion of the runner generated auth token is delayed until the token expires. |
||
| 155 | # Async actions such as Mistral workflows uses the auth token to launch other |
||
| 156 | # actions in the workflow. If the auth token is deleted here, then the actions |
||
| 157 | # in the workflow will fail with unauthorized exception. |
||
| 158 | is_async_runner = isinstance(runner, AsyncActionRunner) |
||
| 159 | action_completed = status in action_constants.LIVEACTION_COMPLETED_STATES |
||
| 160 | |||
| 161 | if not is_async_runner or (is_async_runner and action_completed): |
||
| 162 | try: |
||
| 163 | self._delete_auth_token(runner.auth_token) |
||
| 164 | except: |
||
| 165 | LOG.exception('Unable to clean-up auth_token.') |
||
| 166 | |||
| 167 | LOG.debug('Performing post_run for runner: %s', runner.runner_id) |
||
| 168 | runner.post_run(status, result) |
||
| 169 | runner.container_service = None |
||
| 170 | |||
| 171 | LOG.debug('Runner do_run result', extra={'result': updated_liveaction_db.result}) |
||
| 172 | LOG.audit('Liveaction completed', extra={'liveaction_db': updated_liveaction_db}) |
||
| 173 | |||
| 174 | return updated_liveaction_db |
||
| 175 | |||
| 260 |