| Conditions | 11 |
| Total Lines | 54 |
| 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 st2common.cmd.purge_executions() 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 |
||
| 73 | def purge_executions(timestamp=None, action_ref=None, purge_incomplete=False): |
||
| 74 | if not timestamp: |
||
| 75 | LOG.error('Specify a valid timestamp to purge.') |
||
| 76 | return 1 |
||
| 77 | |||
| 78 | LOG.info('Purging executions older than timestamp: %s' % |
||
|
|
|||
| 79 | timestamp.strftime('%Y-%m-%dT%H:%M:%S.%fZ')) |
||
| 80 | |||
| 81 | filters = {} |
||
| 82 | |||
| 83 | if purge_incomplete: |
||
| 84 | filters['start_timestamp__lt'] = isotime.parse(timestamp) |
||
| 85 | else: |
||
| 86 | filters['end_timestamp__lt'] = isotime.parse(timestamp) |
||
| 87 | filters['start_timestamp__lt'] = isotime.parse(timestamp) |
||
| 88 | filters['status'] = {"$in": DONE_STATES} |
||
| 89 | |||
| 90 | exec_filters = copy.copy(filters) |
||
| 91 | if action_ref: |
||
| 92 | exec_filters['action__ref'] = action_ref |
||
| 93 | |||
| 94 | liveaction_filters = copy.copy(filters) |
||
| 95 | if action_ref: |
||
| 96 | liveaction_filters['action'] = action_ref |
||
| 97 | |||
| 98 | try: |
||
| 99 | ActionExecution.delete_by_query(**exec_filters) |
||
| 100 | except InvalidQueryError: |
||
| 101 | LOG.exception('Bad query (%s) used to delete execution instances. ' + |
||
| 102 | 'Please contact support.', exec_filters) |
||
| 103 | return 2 |
||
| 104 | except: |
||
| 105 | LOG.exception('Deletion of execution models failed for query with filters: %s.', |
||
| 106 | exec_filters) |
||
| 107 | |||
| 108 | try: |
||
| 109 | LiveAction.delete_by_query(**liveaction_filters) |
||
| 110 | except InvalidQueryError: |
||
| 111 | LOG.exception('Bad query (%s) used to delete liveaction instances. ' + |
||
| 112 | 'Please contact support.', liveaction_filters) |
||
| 113 | return 3 |
||
| 114 | except: |
||
| 115 | LOG.exception('Deletion of liveaction models failed for query with filters: %s.', |
||
| 116 | liveaction_filters) |
||
| 117 | |||
| 118 | zombie_execution_instances = len(ActionExecution.query(**exec_filters)) |
||
| 119 | zombie_liveaction_instances = len(LiveAction.query(**liveaction_filters)) |
||
| 120 | |||
| 121 | if (zombie_execution_instances > 0) or (zombie_liveaction_instances > 0): |
||
| 122 | LOG.error('Zombie execution instances left: %d.', zombie_execution_instances) |
||
| 123 | LOG.error('Zombie liveaction instances left: %s.', zombie_liveaction_instances) |
||
| 124 | else: |
||
| 125 | # Print stats |
||
| 126 | LOG.info('#### All execution models less than timestamp %s were deleted.', timestamp) |
||
| 127 | |||
| 151 |