Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions st2common/st2common/cmd/purge_executions.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ def _register_cli_opts():
+ 'By default, only executions in completed states such as "succeeeded" '
+ ', "failed", "canceled" and "timed_out" are deleted.',
),
cfg.StrOpt(
"execution-id",
default=None,
help="Execution ID to purge.",
),
]
do_register_cli_opts(cli_opts)

Expand All @@ -74,11 +79,12 @@ def main():
timestamp = cfg.CONF.timestamp
action_ref = cfg.CONF.action_ref
purge_incomplete = cfg.CONF.purge_incomplete
execution_id = cfg.CONF.execution_id

if not timestamp:
LOG.error("Please supply a timestamp for purging models. Aborting.")
if not execution_id and not timestamp:
LOG.error("Please supply a timestamp or execution ID for purging models. Aborting.")
return 1
else:
if timestamp:
timestamp = datetime.strptime(timestamp, "%Y-%m-%dT%H:%M:%S.%fZ")
timestamp = timestamp.replace(tzinfo=pytz.UTC)

Expand All @@ -88,6 +94,7 @@ def main():
timestamp=timestamp,
action_ref=action_ref,
purge_incomplete=purge_incomplete,
exe_id=execution_id,
)
except Exception as e:
LOG.exception(six.text_type(e))
Expand Down
23 changes: 14 additions & 9 deletions st2common/st2common/garbage_collection/executions.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
]


def purge_executions(logger, timestamp, action_ref=None, purge_incomplete=False):
def purge_executions(logger, timestamp, action_ref=None, purge_incomplete=False, exe_id=None):
"""
Purge action executions and corresponding live action, execution output objects.

Expand All @@ -55,17 +55,22 @@ def purge_executions(logger, timestamp, action_ref=None, purge_incomplete=False)
:param purge_incomplete: True to also delete executions which are not in a done state.
:type purge_incomplete: ``bool``
"""
if not timestamp:
raise ValueError("Specify a valid timestamp to purge.")

logger.info(
"Purging executions older than timestamp: %s"
% timestamp.strftime("%Y-%m-%dT%H:%M:%S.%fZ")
)
if not timestamp and not exe_id:
raise ValueError("Specify a valid timestamp or execution ID to purge.")

if exe_id:
logger.info("Purging execution with ID: %s" % exe_id)
elif timestamp:
logger.info(
"Purging executions older than timestamp: %s"
% timestamp.strftime("%Y-%m-%dT%H:%M:%S.%fZ")
)

filters = {}

if purge_incomplete:
if exe_id:
filters["id"] = exe_id
elif purge_incomplete:
filters["start_timestamp__lt"] = timestamp
else:
filters["end_timestamp__lt"] = timestamp
Expand Down
Loading