Passed
Push — master ( 4ea570...c730ee )
by
unknown
04:53
created

MockLiveActionPublisherNonBlocking.wait_all()   A

Complexity

Conditions 3

Size

Total Lines 9

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 9
rs 9.6666
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
    threads = []
55
56
    @classmethod
57
    def publish_create(cls, payload):
58
        try:
59
            if isinstance(payload, LiveActionDB):
60
                thread = eventlet.spawn(scheduler.get_scheduler().process, payload)
61
                cls.threads.append(thread)
62
        except Exception:
63
            traceback.print_exc()
64
            print(payload)
65
66
    @classmethod
67
    def publish_state(cls, payload, state):
68
        try:
69
            if isinstance(payload, LiveActionDB):
70
                if state == action_constants.LIVEACTION_STATUS_REQUESTED:
71
                    thread = eventlet.spawn(scheduler.get_scheduler().process, payload)
72
                    cls.threads.append(thread)
73
                else:
74
                    thread = eventlet.spawn(worker.get_worker().process, payload)
75
                    cls.threads.append(thread)
76
        except Exception:
77
            traceback.print_exc()
78
            print(payload)
79
80
    @classmethod
81
    def wait_all(cls):
82
        for thread in cls.threads:
83
            try:
84
                thread.wait()
85
            except Exception as e:
86
                print str(e)
87
            finally:
88
                cls.threads.remove(thread)
89