Test Failed
Push — master ( e380d0...f5671d )
by W
02:58
created

st2common/garbage_collection/inquiries.py (10 issues)

Checks for cyclic imports.

Bug Minor
1
# Licensed to the StackStorm, Inc ('StackStorm') under one or more
0 ignored issues
show
There seems to be a cyclic import (st2common.models.db.action -> st2common.models.db.liveaction -> st2common.util.action_db -> st2common.persistence.action).

Cyclic imports may cause partly loaded modules to be returned. This might lead to unexpected runtime behavior which is hard to debug.

Loading history...
There seems to be a cyclic import (st2common.models.api.base -> st2common.util.schema -> st2common.util.action_db -> st2common.persistence.action -> st2common.persistence.executionstate -> st2common.transport -> st2common.transport.reactor -> st2common.models.api.trace).

Cyclic imports may cause partly loaded modules to be returned. This might lead to unexpected runtime behavior which is hard to debug.

Loading history...
There seems to be a cyclic import (st2common.models.api.base -> st2common.util.schema -> st2common.util.action_db -> st2common.persistence.runner -> st2common.persistence.base -> st2common.transport.reactor -> st2common.models.api.trace).

Cyclic imports may cause partly loaded modules to be returned. This might lead to unexpected runtime behavior which is hard to debug.

Loading history...
There seems to be a cyclic import (st2common.models.db.liveaction -> st2common.util.action_db -> st2common.persistence.liveaction).

Cyclic imports may cause partly loaded modules to be returned. This might lead to unexpected runtime behavior which is hard to debug.

Loading history...
There seems to be a cyclic import (st2common.models.db.liveaction -> st2common.util.action_db -> st2common.persistence.action -> st2common.persistence.liveaction).

Cyclic imports may cause partly loaded modules to be returned. This might lead to unexpected runtime behavior which is hard to debug.

Loading history...
There seems to be a cyclic import (st2common.models.api.base -> st2common.util.schema -> st2common.util.action_db -> st2common.persistence.action -> st2common.persistence.actionalias -> st2common.persistence.base -> st2common.transport.reactor -> st2common.models.api.trace).

Cyclic imports may cause partly loaded modules to be returned. This might lead to unexpected runtime behavior which is hard to debug.

Loading history...
There seems to be a cyclic import (st2common.models.api.base -> st2common.util.schema -> st2common.util.action_db -> st2common.persistence.action -> st2common.persistence.liveaction -> st2common.transport -> st2common.transport.reactor -> st2common.models.api.trace).

Cyclic imports may cause partly loaded modules to be returned. This might lead to unexpected runtime behavior which is hard to debug.

Loading history...
There seems to be a cyclic import (st2common.models.api.base -> st2common.util.schema -> st2common.util.action_db -> st2common.persistence.liveaction -> st2common.transport -> st2common.transport.reactor -> st2common.models.api.trace).

Cyclic imports may cause partly loaded modules to be returned. This might lead to unexpected runtime behavior which is hard to debug.

Loading history...
There seems to be a cyclic import (st2common.models.api.base -> st2common.util.schema -> st2common.util.action_db -> st2common.persistence.action -> st2common.persistence.executionstate -> st2common.persistence.base -> st2common.transport.reactor -> st2common.models.api.trace).

Cyclic imports may cause partly loaded modules to be returned. This might lead to unexpected runtime behavior which is hard to debug.

Loading history...
There seems to be a cyclic import (st2common.models.api.base -> st2common.util.schema -> st2common.util.action_db -> st2common.persistence.action -> st2common.persistence.execution -> st2common.transport -> st2common.transport.reactor -> st2common.models.api.trace).

Cyclic imports may cause partly loaded modules to be returned. This might lead to unexpected runtime behavior which is hard to debug.

Loading history...
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
from __future__ import absolute_import
17
from oslo_config import cfg
18
19
from st2common.constants import action as action_constants
20
from st2common.models.db.auth import UserDB
21
from st2common.persistence.execution import ActionExecution
22
from st2common.services import action as action_service
23
from st2common.services import executions
24
from st2common.runners.utils import invoke_post_run
25
from st2common.util import action_db as action_utils
26
from st2common.util.action_db import get_action_by_ref
27
from st2common.util.date import get_datetime_utc_now
28
29
__all__ = [
30
    'purge_inquiries',
31
]
32
33
34
def purge_inquiries(logger):
35
    """Purge Inquiries that have exceeded their configured TTL
36
37
    At the moment, Inquiries do not have their own database model, so this function effectively
38
    is another, more specialized GC for executions. It will look for executions with a 'pending'
39
    status that use the 'inquirer' runner, which is the current definition for an Inquiry.
40
41
    Then it will mark those that have a nonzero TTL have existed longer than their TTL as
42
    "timed out". It will then request that the parent workflow(s) resume, where the failure
43
    can be handled as the user desires.
44
    """
45
46
    # Get all existing Inquiries
47
    filters = {'runner__name': 'inquirer', 'status': action_constants.LIVEACTION_STATUS_PENDING}
48
    inquiries = list(ActionExecution.query(**filters))
49
50
    gc_count = 0
51
52
    # Inspect each Inquiry, and determine if TTL is expired
53
    for inquiry in inquiries:
54
55
        ttl = int(inquiry.result.get('ttl'))
56
        if ttl <= 0:
57
            logger.debug("Inquiry %s has a TTL of %s. Skipping." % (inquiry.id, ttl))
58
            continue
59
60
        min_since_creation = int(
61
            (get_datetime_utc_now() - inquiry.start_timestamp).total_seconds() / 60
62
        )
63
64
        logger.debug("Inquiry %s has a TTL of %s and was started %s minute(s) ago" % (
65
                     inquiry.id, ttl, min_since_creation))
66
67
        if min_since_creation > ttl:
68
            gc_count += 1
69
            logger.info("TTL expired for Inquiry %s. Marking as timed out." % inquiry.id)
70
71
            liveaction_db = action_utils.update_liveaction_status(
72
                status=action_constants.LIVEACTION_STATUS_TIMED_OUT,
73
                result=inquiry.result,
74
                liveaction_id=inquiry.liveaction.get('id'))
75
            executions.update_execution(liveaction_db)
76
77
            # Call Inquiry runner's post_run to trigger callback to workflow
78
            action_db = get_action_by_ref(liveaction_db.action)
79
            invoke_post_run(liveaction_db=liveaction_db, action_db=action_db)
80
81
            if liveaction_db.context.get("parent"):
82
                # Request that root workflow resumes
83
                root_liveaction = action_service.get_root_liveaction(liveaction_db)
84
                action_service.request_resume(
85
                    root_liveaction,
86
                    UserDB(cfg.CONF.system_user.user)
87
                )
88
89
    logger.info('Marked %s ttl-expired Inquiries as "timed out".' % (gc_count))
90