| Conditions | 10 |
| Total Lines | 101 |
| 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.runners.mistral.MistralRunner.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 |
||
| 139 | @retrying.retry( |
||
| 140 | retry_on_exception=utils.retry_on_exceptions, |
||
| 141 | wait_exponential_multiplier=cfg.CONF.mistral.retry_exp_msec, |
||
| 142 | wait_exponential_max=cfg.CONF.mistral.retry_exp_max_msec, |
||
| 143 | stop_max_delay=cfg.CONF.mistral.retry_stop_max_msec) |
||
| 144 | def run(self, action_parameters): |
||
| 145 | # Test connection |
||
| 146 | self._client.workflows.list() |
||
| 147 | |||
| 148 | # Setup inputs for the workflow execution. |
||
| 149 | inputs = self.runner_parameters.get('context', dict()) |
||
| 150 | inputs.update(action_parameters) |
||
| 151 | |||
| 152 | # This URL is used by Mistral to talk back to the API |
||
| 153 | api_url = get_mistral_api_url() |
||
| 154 | endpoint = api_url + '/actionexecutions' |
||
| 155 | |||
| 156 | # This URL is available in the context and can be used by the users inside a workflow, |
||
| 157 | # similar to "ST2_ACTION_API_URL" environment variable available to actions |
||
| 158 | public_api_url = get_full_public_api_url() |
||
| 159 | |||
| 160 | # Build context with additional information |
||
| 161 | parent_context = { |
||
| 162 | 'execution_id': self.execution_id |
||
| 163 | } |
||
| 164 | if getattr(self.liveaction, 'context', None): |
||
| 165 | parent_context.update(self.liveaction.context) |
||
| 166 | |||
| 167 | st2_execution_context = { |
||
| 168 | 'endpoint': endpoint, |
||
| 169 | 'parent': parent_context, |
||
| 170 | 'notify': {}, |
||
| 171 | 'skip_notify_tasks': self._skip_notify_tasks |
||
| 172 | } |
||
| 173 | |||
| 174 | # Include notification information |
||
| 175 | if self._notify: |
||
| 176 | notify_dict = NotificationsHelper.from_model(notify_model=self._notify) |
||
| 177 | st2_execution_context['notify'] = notify_dict |
||
| 178 | |||
| 179 | if self.auth_token: |
||
| 180 | st2_execution_context['auth_token'] = self.auth_token.token |
||
| 181 | |||
| 182 | options = { |
||
| 183 | 'env': { |
||
| 184 | 'st2_execution_id': self.execution_id, |
||
| 185 | 'st2_liveaction_id': self.liveaction_id, |
||
| 186 | 'st2_action_api_url': public_api_url, |
||
| 187 | '__actions': { |
||
| 188 | 'st2.action': { |
||
| 189 | 'st2_context': st2_execution_context |
||
| 190 | } |
||
| 191 | } |
||
| 192 | } |
||
| 193 | } |
||
| 194 | |||
| 195 | # Get workbook/workflow definition from file. |
||
| 196 | with open(self.entry_point, 'r') as def_file: |
||
| 197 | def_yaml = def_file.read() |
||
| 198 | |||
| 199 | def_dict = yaml.safe_load(def_yaml) |
||
| 200 | is_workbook = ('workflows' in def_dict) |
||
| 201 | |||
| 202 | if not is_workbook: |
||
| 203 | # Non-workbook definition containing multiple workflows is not supported. |
||
| 204 | if len([k for k, _ in six.iteritems(def_dict) if k != 'version']) != 1: |
||
| 205 | raise Exception('Workflow (not workbook) definition is detected. ' |
||
| 206 | 'Multiple workflows is not supported.') |
||
| 207 | |||
| 208 | action_ref = '%s.%s' % (self.action.pack, self.action.name) |
||
| 209 | self._check_name(action_ref, is_workbook, def_dict) |
||
| 210 | def_dict_xformed = utils.transform_definition(def_dict) |
||
| 211 | def_yaml_xformed = yaml.safe_dump(def_dict_xformed, default_flow_style=False) |
||
| 212 | |||
| 213 | # Save workbook/workflow definition. |
||
| 214 | if is_workbook: |
||
| 215 | self._save_workbook(action_ref, def_yaml_xformed) |
||
| 216 | default_workflow = self._find_default_workflow(def_dict_xformed) |
||
| 217 | execution = self._client.executions.create(default_workflow, |
||
| 218 | workflow_input=inputs, |
||
| 219 | **options) |
||
| 220 | else: |
||
| 221 | self._save_workflow(action_ref, def_yaml_xformed) |
||
| 222 | execution = self._client.executions.create(action_ref, |
||
| 223 | workflow_input=inputs, |
||
| 224 | **options) |
||
| 225 | |||
| 226 | status = LIVEACTION_STATUS_RUNNING |
||
| 227 | partial_results = {'tasks': []} |
||
| 228 | |||
| 229 | # pylint: disable=no-member |
||
| 230 | current_context = { |
||
| 231 | 'execution_id': str(execution.id), |
||
| 232 | 'workflow_name': execution.workflow_name |
||
| 233 | } |
||
| 234 | |||
| 235 | exec_context = self.context |
||
| 236 | exec_context = self._build_mistral_context(exec_context, current_context) |
||
| 237 | LOG.info('Mistral query context is %s' % exec_context) |
||
| 238 | |||
| 239 | return (status, partial_results, exec_context) |
||
| 240 | |||
| 287 |