1# This file is part of Buildbot.  Buildbot is free software: you can
2# redistribute it and/or modify it under the terms of the GNU General Public
3# License as published by the Free Software Foundation, version 2.
4#
5# This program is distributed in the hope that it will be useful, but WITHOUT
6# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
7# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
8# details.
9#
10# You should have received a copy of the GNU General Public License along with
11# this program; if not, write to the Free Software Foundation, Inc., 51
12# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
13#
14# Copyright Buildbot Team Members
15
16
17from twisted.internet import defer
18from twisted.python.reflect import namedObject
19
20from buildbot.util import service
21
22
23class MQConnector(service.ReconfigurableServiceMixin, service.AsyncMultiService):
24
25    classes = {
26        'simple': {
27            'class': "buildbot.mq.simple.SimpleMQ",
28            'keys': set(['debug']),
29        },
30        'wamp': {
31            'class': "buildbot.mq.wamp.WampMQ",
32            'keys': set(["router_url", "realm", "wamp_debug_level"]),
33        },
34    }
35    name = 'mq'
36
37    def __init__(self):
38        super().__init__()
39        self.impl = None  # set in setup
40        self.impl_type = None  # set in setup
41
42    @defer.inlineCallbacks
43    def setup(self):
44        assert not self.impl
45
46        # imports are done locally so that we don't try to import
47        # implementation-specific modules unless they're required.
48        typ = self.master.config.mq['type']
49        assert typ in self.classes  # this is checked by MasterConfig
50        self.impl_type = typ
51        cls = namedObject(self.classes[typ]['class'])
52        self.impl = cls()
53
54        # set up the impl as a child service
55        yield self.impl.setServiceParent(self)
56
57        # configure it (early)
58        self.impl.reconfigServiceWithBuildbotConfig(self.master.config)
59
60        # copy the methods onto this object for ease of access
61        self.produce = self.impl.produce
62        self.startConsuming = self.impl.startConsuming
63        self.waitUntilEvent = self.impl.waitUntilEvent
64
65    def reconfigServiceWithBuildbotConfig(self, new_config):
66        # double-check -- the master ensures this in config checks
67        assert self.impl_type == new_config.mq['type']
68
69        return super().reconfigServiceWithBuildbotConfig(new_config)
70
71    def produce(self, routing_key, data):
72        # will be patched after configuration to point to the running
73        # implementation's method
74        raise NotImplementedError
75
76    def startConsuming(self, callback, filter, persistent_name=None):
77        # will be patched after configuration to point to the running
78        # implementation's method
79        raise NotImplementedError
80