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 trigger instance objects. |
18
|
|
|
""" |
19
|
|
|
|
20
|
|
|
from mongoengine.errors import InvalidQueryError |
21
|
|
|
|
22
|
|
|
from st2common.persistence.trigger import TriggerInstance |
23
|
|
|
from st2common.util import isotime |
24
|
|
|
|
25
|
|
|
__all__ = [ |
26
|
|
|
'purge_trigger_instances' |
27
|
|
|
] |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
def purge_trigger_instances(logger, timestamp): |
31
|
|
|
""" |
32
|
|
|
:param timestamp: Trigger instances older than this timestamp will be deleted. |
33
|
|
|
:type timestamp: ``datetime.datetime |
34
|
|
|
""" |
35
|
|
|
if not timestamp: |
36
|
|
|
raise ValueError('Specify a valid timestamp to purge.') |
37
|
|
|
|
38
|
|
|
logger.info('Purging trigger instances older than timestamp: %s' % |
39
|
|
|
timestamp.strftime('%Y-%m-%dT%H:%M:%S.%fZ')) |
40
|
|
|
|
41
|
|
|
query_filters = {'occurrence_time__lt': isotime.parse(timestamp)} |
42
|
|
|
|
43
|
|
|
# TODO: Update this code to return statistics on deleted objects once we |
44
|
|
|
# upgrade to newer version of MongoDB where delete_by_query actually returns |
45
|
|
|
# some data |
46
|
|
|
|
47
|
|
|
try: |
48
|
|
|
TriggerInstance.delete_by_query(**query_filters) |
49
|
|
|
except InvalidQueryError as e: |
50
|
|
|
msg = ('Bad query (%s) used to delete trigger instances: %s' |
51
|
|
|
'Please contact support.' % (query_filters, str(e))) |
52
|
|
|
raise InvalidQueryError(msg) |
53
|
|
|
except: |
54
|
|
|
logger.exception('Deleting instances using query_filters %s failed.', query_filters) |
55
|
|
|
|
56
|
|
|
# Print stats |
57
|
|
|
logger.info('All trigger instance models older than timestamp %s were deleted.', timestamp) |
58
|
|
|
|