Conditions | 11 |
Total Lines | 67 |
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.garbage_collection.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 |
||
39 | def purge_executions(logger, timestamp, action_ref=None, purge_incomplete=False): |
||
40 | """ |
||
41 | :param timestamp: Exections older than this timestamp will be deleted. |
||
42 | :type timestamp: ``datetime.datetime |
||
43 | |||
44 | :param action_ref: Only delete executions for the provided actions. |
||
45 | :type action_ref: ``str`` |
||
46 | |||
47 | :param purge_incomplete: True to also delete executions which are not in a done state. |
||
48 | :type purge_incomplete: ``bool`` |
||
49 | """ |
||
50 | if not timestamp: |
||
51 | raise ValueError('Specify a valid timestamp to purge.') |
||
52 | |||
53 | logger.info('Purging executions older than timestamp: %s' % |
||
54 | timestamp.strftime('%Y-%m-%dT%H:%M:%S.%fZ')) |
||
55 | |||
56 | filters = {} |
||
57 | |||
58 | if purge_incomplete: |
||
59 | filters['start_timestamp__lt'] = timestamp |
||
60 | else: |
||
61 | filters['end_timestamp__lt'] = timestamp |
||
62 | filters['start_timestamp__lt'] = timestamp |
||
63 | filters['status'] = {'$in': DONE_STATES} |
||
64 | |||
65 | exec_filters = copy.copy(filters) |
||
66 | if action_ref: |
||
67 | exec_filters['action__ref'] = action_ref |
||
68 | |||
69 | liveaction_filters = copy.deepcopy(filters) |
||
70 | if action_ref: |
||
71 | liveaction_filters['action'] = action_ref |
||
72 | |||
73 | # TODO: Update this code to return statistics on deleted objects once we |
||
74 | # upgrade to newer version of MongoDB where delete_by_query actually returns |
||
75 | # some data |
||
76 | |||
77 | try: |
||
78 | ActionExecution.delete_by_query(**exec_filters) |
||
79 | except InvalidQueryError as e: |
||
80 | msg = ('Bad query (%s) used to delete execution instances: %s' |
||
81 | 'Please contact support.' % (exec_filters, str(e))) |
||
82 | raise InvalidQueryError(msg) |
||
83 | except: |
||
84 | logger.exception('Deletion of execution models failed for query with filters: %s.', |
||
85 | exec_filters) |
||
86 | |||
87 | try: |
||
88 | LiveAction.delete_by_query(**liveaction_filters) |
||
89 | except InvalidQueryError as e: |
||
90 | msg = ('Bad query (%s) used to delete liveaction instances: %s' |
||
91 | 'Please contact support.' % (liveaction_filters, str(e))) |
||
92 | raise InvalidQueryError(msg) |
||
93 | except: |
||
94 | logger.exception('Deletion of liveaction models failed for query with filters: %s.', |
||
95 | liveaction_filters) |
||
96 | |||
97 | zombie_execution_instances = len(ActionExecution.query(**exec_filters)) |
||
98 | zombie_liveaction_instances = len(LiveAction.query(**liveaction_filters)) |
||
99 | |||
100 | if (zombie_execution_instances > 0) or (zombie_liveaction_instances > 0): |
||
101 | logger.error('Zombie execution instances left: %d.', zombie_execution_instances) |
||
102 | logger.error('Zombie liveaction instances left: %s.', zombie_liveaction_instances) |
||
103 | |||
104 | # Print stats |
||
105 | logger.info('All execution models older than timestamp %s were deleted.', timestamp) |
||
106 |