Completed
Pull Request — master (#2965)
by Lakshmi
06:18
created

TimerController.__init__()   A

Complexity

Conditions 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
dl 0
loc 11
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
from pecan.rest import RestController
17
18
19
from st2common import log as logging
20
from st2common.constants.triggers import TIMER_TRIGGER_TYPES
21
from st2common.models.api.base import jsexpose
22
from st2common.models.api.trigger import TriggerAPI
23
import st2common.services.triggers as trigger_service
24
from st2common.services.triggerwatcher import TriggerWatcher
25
26
LOG = logging.getLogger(__name__)
27
28
29
class TimersHolder(object):
30
31
    def __init__(self):
32
        self._timers = {}
33
34
    def add_trigger(self, trigger):
35
        pass
36
37
    def remove_trigger(self, trigger):
38
        pass
39
40
    def get_all(self):
41
        pass
42
43
44
class TimerController(RestController):
45
    def __init__(self, *args, **kwargs):
46
        self._timers = TimersHolder()
47
        self._trigger_types = TIMER_TRIGGER_TYPES.keys()
48
        self._trigger_watcher = TriggerWatcher(create_handler=self._handle_create_trigger,
49
                                               update_handler=self._handle_update_trigger,
50
                                               delete_handler=self._handle_delete_trigger,
51
                                               trigger_types=self._trigger_types,
52
                                               queue_suffix=queue_suffix,
0 ignored issues
show
Comprehensibility Best Practice introduced by
Undefined variable 'queue_suffix'
Loading history...
53
                                               exclusive=True)
54
        self._trigger_watcher.start()
55
        self._register_timer_trigger_types()
56
57
    @jsexpose
58
    def get_one(self):
59
        pass
60
61
    @jsexpose()
62
    def get_all(self):
63
        pass
64
65
    def add_trigger(self, trigger):
66
        # Note: Permission checking for creating and deleting a webhook is done during rule
67
        # creation
68
        ref = self._get_timer_ref(trigger)
0 ignored issues
show
Bug introduced by
There seem to be too many positional arguments for this method call.
Loading history...
69
        LOG.info('Started timer %s with parameters %s', ref, trigger.parameters)
70
        self._timers.add_trigger(ref, trigger)
0 ignored issues
show
Bug introduced by
There seem to be too many positional arguments for this method call.
Loading history...
71
72
    def update_trigger(self, trigger):
73
        pass
74
75
    def remove_trigger(self, trigger):
76
        # Note: Permission checking for creating and deleting a webhook is done during rule
77
        # creation
78
        ref = self._get_timer_ref(trigger)
0 ignored issues
show
Bug introduced by
There seem to be too many positional arguments for this method call.
Loading history...
79
80
        removed = self._timers.remove_trigger(ref, trigger)
0 ignored issues
show
Bug introduced by
There seem to be too many positional arguments for this method call.
Loading history...
81
        if removed:
82
            LOG.info('Stopped timer: %s', ref)
83
84
    def _register_timer_trigger_types(self):
85
        for trigger_type in TIMER_TRIGGER_TYPES.values():
86
            trigger_service.create_trigger_type_db(trigger_type)
87
88
    def _get_timer_ref(self):
89
        pass
90
91
    ##############################################
92
    # Event handler methods for the trigger events
93
    ##############################################
94
95
    def _handle_create_trigger(self, trigger):
96
        LOG.debug('Calling "add_trigger" method (trigger.type=%s)' % (trigger.type))
97
        trigger = self._sanitize_trigger(trigger=trigger)
98
        self.add_trigger(trigger=trigger)
99
100
    def _handle_update_trigger(self, trigger):
101
        LOG.debug('Calling "update_trigger" method (trigger.type=%s)' % (trigger.type))
102
        trigger = self._sanitize_trigger(trigger=trigger)
103
        self.update_trigger(trigger=trigger)
104
105
    def _handle_delete_trigger(self, trigger):
106
        LOG.debug('Calling "remove_trigger" method (trigger.type=%s)' % (trigger.type))
107
        trigger = self._sanitize_trigger(trigger=trigger)
108
        self.remove_trigger(trigger=trigger)
109
110
    def _sanitize_trigger(self, trigger):
111
        sanitized = TriggerAPI.from_model(trigger).to_dict()
112
        return sanitized
113