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 |
||
97 | def _do_run(self, runner, runnertype_db, action_db, liveaction_db): |
||
98 | # Create a temporary auth token which will be available |
||
99 | # for the duration of the action execution. |
||
100 | runner.auth_token = self._create_auth_token(runner.context) |
||
101 | |||
102 | updated_liveaction_db = None |
||
103 | try: |
||
104 | # Finalized parameters are resolved and then rendered. This process could |
||
105 | # fail. Handle the exception and report the error correctly. |
||
106 | try: |
||
107 | runner_params, action_params = param_utils.render_final_params( |
||
108 | runnertype_db.runner_parameters, action_db.parameters, liveaction_db.parameters, |
||
109 | liveaction_db.context) |
||
110 | runner.runner_parameters = runner_params |
||
111 | except ParamException as e: |
||
112 | raise actionrunner.ActionRunnerException(str(e)) |
||
113 | |||
114 | LOG.debug('Performing pre-run for runner: %s', runner.runner_id) |
||
115 | runner.pre_run() |
||
116 | |||
117 | # Mask secret parameters in the log context |
||
118 | resolved_action_params = ResolvedActionParameters(action_db=action_db, |
||
119 | runner_type_db=runnertype_db, |
||
120 | runner_parameters=runner_params, |
||
121 | action_parameters=action_params) |
||
122 | extra = {'runner': runner, 'parameters': resolved_action_params} |
||
123 | LOG.debug('Performing run for runner: %s' % (runner.runner_id), extra=extra) |
||
|
|||
124 | (status, result, context) = runner.run(action_params) |
||
125 | |||
126 | try: |
||
127 | result = json.loads(result) |
||
128 | except: |
||
129 | pass |
||
130 | |||
131 | action_completed = status in action_constants.LIVEACTION_COMPLETED_STATES |
||
132 | if isinstance(runner, AsyncActionRunner) and not action_completed: |
||
133 | self._setup_async_query(liveaction_db.id, runnertype_db, context) |
||
134 | except: |
||
135 | LOG.exception('Failed to run action.') |
||
136 | _, ex, tb = sys.exc_info() |
||
137 | # mark execution as failed. |
||
138 | status = action_constants.LIVEACTION_STATUS_FAILED |
||
139 | # include the error message and traceback to try and provide some hints. |
||
140 | result = {'error': str(ex), 'traceback': ''.join(traceback.format_tb(tb, 20))} |
||
141 | context = None |
||
142 | finally: |
||
143 | # Log action completion |
||
144 | extra = {'result': result, 'status': status} |
||
145 | LOG.debug('Action "%s" completed.' % (action_db.name), extra=extra) |
||
146 | |||
147 | # Always clean-up the auth_token |
||
148 | try: |
||
149 | LOG.debug('Setting status: %s for liveaction: %s', status, liveaction_db.id) |
||
150 | updated_liveaction_db = self._update_live_action_db(liveaction_db.id, status, |
||
151 | result, context) |
||
152 | except: |
||
153 | error = 'Cannot update LiveAction object for id: %s, status: %s, result: %s.' % ( |
||
154 | liveaction_db.id, status, result) |
||
155 | LOG.exception(error) |
||
156 | raise |
||
157 | |||
158 | executions.update_execution(updated_liveaction_db) |
||
159 | extra = {'liveaction_db': updated_liveaction_db} |
||
160 | LOG.debug('Updated liveaction after run', extra=extra) |
||
161 | |||
162 | # Deletion of the runner generated auth token is delayed until the token expires. |
||
163 | # Async actions such as Mistral workflows uses the auth token to launch other |
||
164 | # actions in the workflow. If the auth token is deleted here, then the actions |
||
165 | # in the workflow will fail with unauthorized exception. |
||
166 | is_async_runner = isinstance(runner, AsyncActionRunner) |
||
167 | action_completed = status in action_constants.LIVEACTION_COMPLETED_STATES |
||
168 | |||
169 | if not is_async_runner or (is_async_runner and action_completed): |
||
170 | try: |
||
171 | self._delete_auth_token(runner.auth_token) |
||
172 | except: |
||
173 | LOG.exception('Unable to clean-up auth_token.') |
||
174 | |||
175 | LOG.debug('Performing post_run for runner: %s', runner.runner_id) |
||
176 | runner.post_run(status, result) |
||
177 | runner.container_service = None |
||
178 | |||
179 | LOG.debug('Runner do_run result', extra={'result': updated_liveaction_db.result}) |
||
180 | LOG.audit('Liveaction completed', extra={'liveaction_db': updated_liveaction_db}) |
||
181 | |||
182 | return updated_liveaction_db |
||
183 | |||
268 |