Passed
Push — master ( 7ddc3b...6d37f6 )
by
unknown
03:15
created

MockLiveActionPublisher.publish_update()   A

Complexity

Conditions 3

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
c 0
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 st2common.constants import action as action_constants
22
from st2common.models.db.liveaction import LiveActionDB
23
24
__all__ = [
25
    'MockLiveActionPublisher'
26
]
27
28
29
class MockLiveActionPublisher(object):
30
31
    @classmethod
32
    def publish_create(cls, payload):
33
        try:
34
            if isinstance(payload, LiveActionDB):
35
                scheduler.get_scheduler().process(payload)
36
        except Exception:
37
            traceback.print_exc()
38
            print(payload)
39
40
    @classmethod
41
    def publish_state(cls, payload, state):
42
        try:
43
            if isinstance(payload, LiveActionDB):
44
                if state == action_constants.LIVEACTION_STATUS_REQUESTED:
45
                    scheduler.get_scheduler().process(payload)
46
                else:
47
                    worker.get_worker().process(payload)
48
        except Exception:
49
            traceback.print_exc()
50
            print(payload)
51
52
53
class MockLiveActionPublisherNonBlocking(object):
54
55
    @classmethod
56
    def publish_create(cls, payload):
57
        try:
58
            if isinstance(payload, LiveActionDB):
59
                eventlet.spawn(scheduler.get_scheduler().process, payload)
60
        except Exception:
61
            traceback.print_exc()
62
            print(payload)
63
64
    @classmethod
65
    def publish_state(cls, payload, state):
66
        try:
67
            if isinstance(payload, LiveActionDB):
68
                if state == action_constants.LIVEACTION_STATUS_REQUESTED:
69
                    eventlet.spawn(scheduler.get_scheduler().process, payload)
70
                else:
71
                    eventlet.spawn(worker.get_worker().process, payload)
72
        except Exception:
73
            traceback.print_exc()
74
            print(payload)
75