|
| 1 | +# Copyright 2022 The StackStorm Authors. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | + |
| 16 | +""" |
| 17 | +A utility script that purges st2 workflow executions older than certain |
| 18 | +timestamp. |
| 19 | +
|
| 20 | +*** RISK RISK RISK. You will lose data. Run at your own risk. *** |
| 21 | +""" |
| 22 | + |
| 23 | +from __future__ import absolute_import |
| 24 | + |
| 25 | +from datetime import datetime |
| 26 | + |
| 27 | +import six |
| 28 | +import pytz |
| 29 | +from oslo_config import cfg |
| 30 | + |
| 31 | +from st2common import config |
| 32 | +from st2common import log as logging |
| 33 | +from st2common.config import do_register_cli_opts |
| 34 | +from st2common.script_setup import setup as common_setup |
| 35 | +from st2common.script_setup import teardown as common_teardown |
| 36 | +from st2common.constants.exit_codes import SUCCESS_EXIT_CODE |
| 37 | +from st2common.constants.exit_codes import FAILURE_EXIT_CODE |
| 38 | +from st2common.garbage_collection.workflows import purge_workflow_executions |
| 39 | + |
| 40 | +__all__ = ["main"] |
| 41 | + |
| 42 | +LOG = logging.getLogger(__name__) |
| 43 | + |
| 44 | + |
| 45 | +def _register_cli_opts(): |
| 46 | + cli_opts = [ |
| 47 | + cfg.StrOpt( |
| 48 | + "timestamp", |
| 49 | + default=None, |
| 50 | + help="Will delete workflow execution objects older than " |
| 51 | + + "this UTC timestamp. " |
| 52 | + + "Example value: 2015-03-13T19:01:27.255542Z.", |
| 53 | + ), |
| 54 | + cfg.BoolOpt( |
| 55 | + "purge-incomplete", |
| 56 | + default=False, |
| 57 | + help="Purge all models irrespective of their ``status``." |
| 58 | + + "By default, only workflow executions in completed states such as " |
| 59 | + + '"succeeeded", "failed", "canceled" and "timed_out" are deleted.', |
| 60 | + ), |
| 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 | + purge_incomplete = cfg.CONF.purge_incomplete |
| 72 | + |
| 73 | + if not timestamp: |
| 74 | + LOG.error("Please supply a timestamp for purging models. Aborting.") |
| 75 | + return 1 |
| 76 | + else: |
| 77 | + timestamp = datetime.strptime(timestamp, "%Y-%m-%dT%H:%M:%S.%fZ") |
| 78 | + timestamp = timestamp.replace(tzinfo=pytz.UTC) |
| 79 | + |
| 80 | + try: |
| 81 | + purge_workflow_executions( |
| 82 | + logger=LOG, timestamp=timestamp, purge_incomplete=purge_incomplete |
| 83 | + ) |
| 84 | + except Exception as e: |
| 85 | + LOG.exception(six.text_type(e)) |
| 86 | + return FAILURE_EXIT_CODE |
| 87 | + finally: |
| 88 | + common_teardown() |
| 89 | + |
| 90 | + return SUCCESS_EXIT_CODE |
0 commit comments