forked from cern-mig/python-amqpclt
/
kombu.py
312 lines (282 loc) · 11.2 KB
/
kombu.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
"""
Kombu modules.
"""
import logging
import socket
import ssl
from mtb.modules import md5_hash
from mtb.string import get_uuid
import auth.credential as credential
from messaging.message import Message
from amqpclt import common
from amqpclt.errors import AmqpcltError
LOGGER = logging.getLogger("amqpclt")
class KombuAdapter(object):
""" Kombu library adapter. """
direction = "unknown"
def __init__(self, config):
""" Initialize Kombu adapter """
self._kombu = __import__("kombu")
self._config = config
self._connection = None
self._channel = None
self._exchange = dict()
self._queue = dict()
self._bind = dict()
def _maybe_declare_exchange(self, exch_props):
""" May be declare an exchange. """
exch_name = exch_props.get("name", "")
if (exch_name and
not exch_name.startswith("amq.") and
exch_name not in self._exchange):
LOGGER.debug(
"declaring %s exchange: %s" % (self.direction, exch_name))
self._exchange[exch_name] = 1
self._channel.exchange_declare(
exchange=exch_name,
durable=exch_props.get("durable", False),
type=exch_props.get("type", "direct"),
auto_delete=exch_props.get("auto_delete", False),
arguments=exch_props.get("arguments", dict())
)
return exch_name
def _maybe_declare_queue(self, queue_props):
""" May be declare a queue. """
queue_name = queue_props.get("name", "")
if queue_name and queue_name in self._queue:
return queue_name
if not queue_name:
queue_name = get_uuid()
self._channel.queue_declare(
queue=queue_name,
durable=queue_props.get("durable", False),
exclusive=queue_props.get("exclusive", False),
auto_delete=queue_props.get("auto_delete", False),
arguments=queue_props.get("arguments", dict()))
# if not queue_name:
# # amq generated queue
# queue_name = result.method.queue
LOGGER.debug("incoming queue declared: %s" % (queue_name, ))
self._queue[queue_name] = 1
return queue_name
def _maybe_bind(self, queue_name, exchange, routing_key=""):
""" May be bind a queue to an exchange. """
bind_id = md5_hash(queue_name + exchange + routing_key).hexdigest()
if bind_id in self._bind:
return
LOGGER.debug(
"binding incoming queue: queue=%s, exchange=%s, routing_key=%s" %
(queue_name, exchange, routing_key))
self._channel.queue_bind(queue=queue_name,
exchange=exchange,
routing_key=routing_key)
self._bind[bind_id] = 1
def connect(self):
""" Create a kombu AMQP connection and channel. """
direction = self.direction
config = self._config[direction]["broker"]
params = common.parse_amqp_uri(config["uri"])
cred = config.get("auth")
if cred is None:
cred = credential.new(scheme="none")
if cred.scheme == "x509":
ssl_options = dict()
cred_dict = cred.dict()
for key, keyval in {"cert": "certfile",
"key": "keyfile", "ca": "ca_certs"}.items():
if key in cred_dict:
ssl_options[keyval] = cred_dict[key]
ssl_options["cert_reqs"] = ssl.CERT_REQUIRED
ssl_options["ssl_version"] = ssl.PROTOCOL_SSLv3
extra = {"ssl": ssl_options,
"transport_options": {"login_method": "EXTERNAL"}}
elif cred.scheme == "plain":
extra = {
"userid": cred['name'],
"password": cred['pass'], }
else:
# none
extra = dict()
#if self._config.get("heartbeat") is not None:
# extra["heartbeat"] = self._config["heartbeat"]
parameters = {
"hostname": params["host"],
"port": int(params["port"]),
"transport": "amqplib",
"virtual_host": params.get("virtual_host", "rabbitmq")
}
timeout_connect = self._config.get("timeout-connect")
if timeout_connect is not None:
parameters["connect_timeout"] = timeout_connect
parameters.update(extra)
self._connection = connection = self._kombu.BrokerConnection(
**parameters)
self._channel = connection.channel()
LOGGER.debug(
"%s broker %s:%s: %s" %
(direction, params['host'], params['port'], self.amqp_type(),))
if self._config.get("%s-broker-type" % direction) is None:
self._config["%s-broker-type" % direction] = self.amqp_type()
return True
def amqp_type(self):
""" Return the broker type. """
if self._connection is None:
return None
return "RabbitMQ"
class KombuIncomingBroker(KombuAdapter):
""" Kombu incoming broker object. """
direction = "incoming"
def __init__(self, config):
""" Initialize kombu incoming broker module. """
super(KombuIncomingBroker, self).__init__(config)
self._msgbuf = list()
self._pending = list()
self._consume = dict()
def _maybe_subscribe(self, subscription):
""" May be subscribe to queue. """
if "queue" in subscription:
queue_name = self._maybe_declare_queue(subscription["queue"])
else:
raise AmqpcltError("subscription must contain a queue")
exchange_name = None
if "exchange" in subscription:
exchange_name = self._maybe_declare_exchange(
subscription["exchange"])
if exchange_name:
self._maybe_bind(queue_name,
exchange_name,
subscription.get("routing_key", ""))
if queue_name not in self._consume:
LOGGER.debug("incoming consume from queue: %s" % (queue_name, ))
tag = get_uuid()
params = {"callback": self._handle_message,
"no_ack": False,
"queue": queue_name,
"consumer_tag": tag}
if not self._config["reliable"]:
params["no_ack"] = True
self._channel.basic_consume(**params)
self._consume[queue_name] = tag
def _handle_message(self, msg):
""" Handle delivery. """
self._msgbuf.append((msg.delivery_info,
msg.properties, msg.body))
def _drain_events(self, timeout=0.1):
""" Drain events. """
try:
self._connection.drain_events(timeout=timeout)
except socket.timeout:
pass
except socket.error:
pass
def start(self):
""" Start the incoming broker module. """
self.connect()
if self._config.get("prefetch") >= 0:
self._channel.basic_qos(
prefetch_count=int(self._config["prefetch"]),
prefetch_size=0,
a_global=False)
subscribe = self._config.get("subscribe", [])
for sub in subscribe:
self._maybe_subscribe(sub)
def get(self):
""" Get a message. """
if len(self._msgbuf) == 0:
self._drain_events()
if len(self._msgbuf) == 0:
return "no messages received", None
(info, header, body) = self._msgbuf.pop(0)
if header.get("content_type") is not None and \
(header["content_type"].startswith("text/") or
"charset=" in header["content_type"]):
body = body.decode("utf-8")
headers = header["application_headers"]
for header_name, header_value in headers.items():
try:
headers[header_name] = header_value.encode("utf-8")
except UnicodeDecodeError:
headers[header_name] = header_value.decode("utf-8")
msg = Message(header=headers, body=body)
if self._config["reliable"]:
self._pending.append(info.get("delivery_tag"))
return msg, info.get("delivery_tag")
else:
return msg, None
def ack(self, delivery_tag):
""" Ack a message. """
LOGGER.debug("acking incoming message: %d" % (delivery_tag, ))
self._channel.basic_ack(delivery_tag=delivery_tag)
self._pending.remove(delivery_tag)
def idle(self):
""" Idle. """
self._drain_events()
def stop(self):
""" Stop. """
self._channel.close()
self._connection.close()
self._connection = None
class KombuOutgoingBroker(KombuAdapter):
""" Kombu outgoing broker object. """
direction = "outgoing"
def __init__(self, config):
""" Initialize the kombu outgoing broker module. """
super(KombuOutgoingBroker, self).__init__(config)
self._producer = None
def start(self):
""" Start the kombu outgoing broker module. """
self.connect()
self._producer = self._kombu.Producer(self._channel)
def put(self, msg, msg_id=None):
""" Put a message. """
delivery_mode = 1
if msg.header.get("persistent", "false") == "true":
delivery_mode = 2
# Send the message
if "destination" not in msg.header:
raise AmqpcltError("message doesn't have a destination: %s" % msg)
destination = common.parse_sender_destination(
msg.header["destination"])
if "queue" in destination:
queue_name = self._maybe_declare_queue(destination["queue"])
exch_name = self._maybe_declare_exchange(
destination.get("exchange", dict()))
if exch_name and "queue" in destination:
self._maybe_bind(queue_name,
exch_name,
destination.get("routing_key", ""))
extra = dict()
if "content-type" in msg.header:
content_type = msg.header["content-type"]
if content_type.startswith("text/") or \
"charset=" in content_type:
if not msg.is_text():
raise AmqpcltError("unexpected text content-type "
"for binary message: %s" % content_type)
else:
if msg.is_text():
raise AmqpcltError("unexpected binary content-type for "
"text message: %s" % content_type)
extra["content_type"] = content_type
elif msg.is_text():
extra["content_type"] = "text/unknown"
self._producer.publish(
exchange=exch_name,
routing_key=destination.get("routing_key", ""),
delivery_mode=delivery_mode,
serializer=None,
compression=None,
headers=msg.header,
body=msg.body, **extra)
if msg_id is None:
return list()
else:
return [msg_id, ]
def idle(self):
""" Idle. """
return list()
def stop(self):
""" Stop. """
self._connection.close()
self._channel = None
self._connection = None