1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
3
# Copyright 2010 United States Government as represented by the
4
# Administrator of the National Aeronautics and Space Administration.
7
# Licensed under the Apache License, Version 2.0 (the "License"); you may
8
# not use this file except in compliance with the License. You may obtain
9
# a copy of the License at
11
# http://www.apache.org/licenses/LICENSE-2.0
13
# Unless required by applicable law or agreed to in writing, software
14
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16
# License for the specific language governing permissions and limitations
19
"""Based a bit on the carrot.backends.queue backend... but a lot better."""
23
from carrot.backends import base
24
from eventlet import greenthread
26
from nova import log as logging
29
LOG = logging.getLogger(__name__)
37
class Message(base.BaseMessage):
41
class Exchange(object):
42
def __init__(self, name, exchange_type):
44
self.exchange_type = exchange_type
45
self._queue = queue.Queue()
48
def publish(self, message, routing_key=None):
50
LOG.debug(_('(%(nm)s) publish (key: %(routing_key)s)'
51
' %(message)s') % locals())
52
if routing_key in self._routes:
53
for f in self._routes[routing_key]:
54
LOG.debug(_('Publishing to route %s'), f)
55
f(message, routing_key=routing_key)
57
def bind(self, callback, routing_key):
58
self._routes.setdefault(routing_key, [])
59
self._routes[routing_key].append(callback)
63
def __init__(self, name):
65
self._queue = queue.Queue()
68
return '<Queue: %s>' % self.name
70
def push(self, message, routing_key=None):
71
self._queue.put(message)
74
return self._queue.qsize()
77
return self._queue.get()
80
class Backend(base.BaseBackend):
81
def queue_declare(self, queue, **kwargs):
83
if queue not in QUEUES:
84
LOG.debug(_('Declaring queue %s'), queue)
85
QUEUES[queue] = Queue(queue)
87
def exchange_declare(self, exchange, type, *args, **kwargs):
89
if exchange not in EXCHANGES:
90
LOG.debug(_('Declaring exchange %s'), exchange)
91
EXCHANGES[exchange] = Exchange(exchange, type)
93
def queue_bind(self, queue, exchange, routing_key, **kwargs):
96
LOG.debug(_('Binding %(queue)s to %(exchange)s with'
97
' key %(routing_key)s') % locals())
98
EXCHANGES[exchange].bind(QUEUES[queue].push, routing_key)
100
def declare_consumer(self, queue, callback, consumer_tag, *args, **kwargs):
102
LOG.debug("Adding consumer %s", consumer_tag)
103
CONSUMERS[consumer_tag] = (queue, callback)
105
def cancel(self, consumer_tag):
107
LOG.debug("Removing consumer %s", consumer_tag)
108
del CONSUMERS[consumer_tag]
110
def consume(self, limit=None):
114
for (queue, callback) in CONSUMERS.itervalues():
115
item = self.get(queue)
120
if limit and num == limit:
121
raise StopIteration()
122
greenthread.sleep(0.1)
124
def get(self, queue, no_ack=False):
126
if not queue in QUEUES or not QUEUES[queue].size():
128
(message_data, content_type, content_encoding) = QUEUES[queue].pop()
129
message = Message(backend=self, body=message_data,
130
content_type=content_type,
131
content_encoding=content_encoding)
132
message.result = True
133
LOG.debug(_('Getting from %(queue)s: %(message)s') % locals())
136
def prepare_message(self, message_data, delivery_mode,
137
content_type, content_encoding, **kwargs):
138
"""Prepare message for sending."""
139
return (message_data, content_type, content_encoding)
141
def publish(self, message, exchange, routing_key, **kwargs):
143
if exchange in EXCHANGES:
144
EXCHANGES[exchange].publish(message, routing_key=routing_key)