GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — kale/action-datastore (#6)
by Manas
05:59
created

st2common.garbage_collection.purge_executions()   F

Complexity

Conditions 11

Size

Total Lines 67

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 67
rs 3.75
cc 11

How to fix   Long Method    Complexity   

Long Method

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:

Complexity

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
2
# contributor license agreements.  See the NOTICE file distributed with
3
# this work for additional information regarding copyright ownership.
4
# The ASF licenses this file to You under the Apache License, Version 2.0
5
# (the "License"); you may not use this file except in compliance with
6
# the License.  You may obtain a copy of the License at
7
#
8
#     http://www.apache.org/licenses/LICENSE-2.0
9
#
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS,
12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
# See the License for the specific language governing permissions and
14
# limitations under the License.
15
16
"""
17
Module with utility functions for purging old action executions and
18
corresponding live action objects.
19
"""
20
21
import copy
22
23
from mongoengine.errors import InvalidQueryError
24
25
from st2common.constants import action as action_constants
26
from st2common.persistence.liveaction import LiveAction
27
from st2common.persistence.execution import ActionExecution
28
29
__all__ = [
30
    'purge_executions'
31
]
32
33
DONE_STATES = [action_constants.LIVEACTION_STATUS_SUCCEEDED,
34
               action_constants.LIVEACTION_STATUS_FAILED,
35
               action_constants.LIVEACTION_STATUS_TIMED_OUT,
36
               action_constants.LIVEACTION_STATUS_CANCELED]
37
38
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