Passed
Push — develop ( 3fca4f...bf1cdd )
by Plexxi
07:34 queued 02:53
created

publish_update()   A

Complexity

Conditions 3

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
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
import eventlet
17
import traceback
18
19
from st2actions import worker
20
from st2actions import scheduler
21
from st2actions.notifier import notifier
22
from st2common.constants import action as action_constants
23
from st2common.models.db.liveaction import LiveActionDB
24
25
__all__ = [
26
    'MockLiveActionPublisher'
27
]
28
29
30
class MockLiveActionPublisher(object):
31
32
    @classmethod
33
    def publish_create(cls, payload):
34
        try:
35
            if isinstance(payload, LiveActionDB):
36
                scheduler.get_scheduler().process(payload)
37
        except Exception:
38
            traceback.print_exc()
39
            print(payload)
40
41
    @classmethod
42
    def publish_state(cls, payload, state):
43
        try:
44
            if isinstance(payload, LiveActionDB):
45
                if state == action_constants.LIVEACTION_STATUS_REQUESTED:
46
                    scheduler.get_scheduler().process(payload)
47
                else:
48
                    worker.get_worker().process(payload)
49
        except Exception:
50
            traceback.print_exc()
51
            print(payload)
52
53
    @classmethod
54
    def publish_update(cls, payload):
55
        try:
56
            if isinstance(payload, LiveActionDB):
57
                notifier.get_notifier().process(payload)
58
        except Exception:
59
            traceback.print_exc()
60
            print(payload)
61
62
63
class MockLiveActionPublisherNonBlocking(object):
64
65
    @classmethod
66
    def publish_create(cls, payload):
67
        try:
68
            if isinstance(payload, LiveActionDB):
69
                eventlet.spawn(scheduler.get_scheduler().process, payload)
70
        except Exception:
71
            traceback.print_exc()
72
            print(payload)
73
74
    @classmethod
75
    def publish_state(cls, payload, state):
76
        try:
77
            if isinstance(payload, LiveActionDB):
78
                if state == action_constants.LIVEACTION_STATUS_REQUESTED:
79
                    eventlet.spawn(scheduler.get_scheduler().process, payload)
80
                else:
81
                    eventlet.spawn(worker.get_worker().process, payload)
82
        except Exception:
83
            traceback.print_exc()
84
            print(payload)
85
86
    @classmethod
87
    def publish_update(cls, payload):
88
        try:
89
            if isinstance(payload, LiveActionDB):
90
                eventlet.spawn(notifier.get_notifier().process, payload)
91
        except Exception:
92
            traceback.print_exc()
93
            print(payload)
94