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.cmd.purge_executions()   D

Complexity

Conditions 11

Size

Total Lines 54

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 54
rs 4.4999
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.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
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
"""
18
A utility script that purges st2 executions older than certain
19
timestamp.
20
21
*** RISK RISK RISK. You will lose data. Run at your own risk. ***
22
"""
23
24
from datetime import datetime
25
import pytz
26
27
from oslo_config import cfg
28
29
from st2common import config
30
from st2common import log as logging
31
from st2common.script_setup import setup as common_setup
32
from st2common.script_setup import teardown as common_teardown
33
from st2common.constants.exit_codes import SUCCESS_EXIT_CODE
34
from st2common.constants.exit_codes import FAILURE_EXIT_CODE
35
from st2common.garbage_collection.executions import purge_executions
36
37
LOG = logging.getLogger(__name__)
38
39
40
def _do_register_cli_opts(opts, ignore_errors=False):
41
    for opt in opts:
42
        try:
43
            cfg.CONF.register_cli_opt(opt)
44
        except:
45
            if not ignore_errors:
46
                raise
47
48
49
def _register_cli_opts():
50
    cli_opts = [
51
        cfg.StrOpt('timestamp', default=None,
52
                   help='Will delete execution and liveaction models older than ' +
53
                   'this UTC timestamp. ' +
54
                   'Example value: 2015-03-13T19:01:27.255542Z.'),
55
        cfg.StrOpt('action-ref', default='',
56
                   help='action-ref to delete executions for.'),
57
        cfg.BoolOpt('purge-incomplete', default=False,
58
                    help='Purge all models irrespective of their ``status``.' +
59
                    'By default, only executions in completed states such as "succeeeded" ' +
60
                    ', "failed", "canceled" and "timed_out" are deleted.'),
61
    ]
62
    _do_register_cli_opts(cli_opts)
63
64
65
def main():
66
    _register_cli_opts()
67
    common_setup(config=config, setup_db=True, register_mq_exchanges=False)
68
69
    # Get config values
70
    timestamp = cfg.CONF.timestamp
71
    action_ref = cfg.CONF.action_ref
72
    purge_incomplete = cfg.CONF.purge_incomplete
73
74
    if not timestamp:
75
        LOG.error('Please supply a timestamp for purging models. Aborting.')
76
        return 1
77
    else:
78
        timestamp = datetime.strptime(timestamp, '%Y-%m-%dT%H:%M:%S.%fZ')
79
        timestamp = timestamp.replace(tzinfo=pytz.UTC)
80
81
    try:
82
        purge_executions(logger=LOG, timestamp=timestamp, action_ref=action_ref,
83
                         purge_incomplete=purge_incomplete)
84
    except Exception as e:
85
        LOG.exception(str(e))
86
        return FAILURE_EXIT_CODE
87
    finally:
88
        common_teardown()
89
90
    return SUCCESS_EXIT_CODE
91