Conditions | 7 |
Total Lines | 53 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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:
1 | # Licensed to the StackStorm, Inc ('StackStorm') under one or more |
||
55 | def process(self, wf_ex_db): |
||
56 | # Refresh record from the database in case the request is in the queue for too long. |
||
57 | conductor, wf_ex_db = wf_svc.refresh_conductor(str(wf_ex_db.id)) |
||
58 | |||
59 | # Continue if workflow is still active. |
||
60 | if conductor.get_workflow_state() not in states.COMPLETED_STATES: |
||
61 | # Set workflow to running state. |
||
62 | conductor.set_workflow_state(states.RUNNING) |
||
63 | |||
64 | # Identify the next set of tasks to execute. |
||
65 | next_tasks = conductor.get_next_tasks() |
||
66 | |||
67 | # Mark the tasks as running in the task flow before actual task execution. |
||
68 | for task in next_tasks: |
||
69 | conductor.update_task_flow(task['id'], states.RUNNING) |
||
70 | |||
71 | # Update timestamp and output if workflow is no longer running. |
||
72 | if conductor.get_workflow_state() in states.COMPLETED_STATES: |
||
73 | wf_ex_db.end_timestamp = date_utils.get_datetime_utc_now() |
||
74 | wf_ex_db.output = conductor.get_workflow_output() |
||
75 | |||
76 | # Write the updated workflow state and task flow to the database. |
||
77 | wf_ex_db.status = conductor.get_workflow_state() |
||
78 | wf_ex_db.errors = copy.deepcopy(conductor.errors) |
||
79 | wf_ex_db.flow = conductor.flow.serialize() |
||
80 | wf_ex_db = wf_db_access.WorkflowExecution.update(wf_ex_db, publish=False) |
||
81 | |||
82 | # If workflow execution is no longer active, then update database and stop. |
||
83 | if wf_ex_db.status in states.COMPLETED_STATES: |
||
84 | # Update the corresponding liveaction and action execution for the workflow. |
||
85 | wf_ac_ex_db = ex_db_access.ActionExecution.get_by_id(wf_ex_db.action_execution) |
||
86 | wf_lv_ac_db = ac_db_util.get_liveaction_by_id(wf_ac_ex_db.liveaction['id']) |
||
87 | |||
88 | result = wf_ex_db.output |
||
89 | |||
90 | if wf_ex_db.status in states.ABENDED_STATES: |
||
91 | result['errors'] = wf_ex_db.errors |
||
92 | |||
93 | wf_lv_ac_db = ac_db_util.update_liveaction_status( |
||
94 | status=wf_ex_db.status, |
||
95 | result=result, |
||
96 | end_timestamp=wf_ex_db.end_timestamp, |
||
97 | liveaction_db=wf_lv_ac_db) |
||
98 | |||
99 | ex_svc.update_execution(wf_lv_ac_db) |
||
100 | |||
101 | # stop processing |
||
102 | return |
||
103 | |||
104 | # Request task execution for the tasks. |
||
105 | for task in next_tasks: |
||
106 | st2_ctx = {'execution_id': wf_ex_db.action_execution} |
||
107 | wf_svc.request_task_execution(wf_ex_db, task['id'], task['spec'], task['ctx'], st2_ctx) |
||
108 | |||
113 |
It is generally a bad practice to shadow variables from the outer-scope. In most cases, this is done unintentionally and might lead to unexpected behavior: