mirror of
https://github.com/zulip/zulip.git
synced 2025-11-06 15:03:34 +00:00
TornadoQueueClient: Provide a callback when declaring queues
This is required by Pika 0.9.8. We need at least 0.9.6 for the next commit; I had been testing with 0.9.5 previously. Anyway this way seems more correct as well. (imported from commit bfb9e9e78938073001f70c4d28a5e07cc4ebac32)
This commit is contained in:
committed by
Leo Franchi
parent
c346abe6e2
commit
6ffc2bbe65
@@ -31,32 +31,33 @@ class SimpleQueueClient(object):
|
|||||||
def ready(self):
|
def ready(self):
|
||||||
return self.channel is not None
|
return self.channel is not None
|
||||||
|
|
||||||
def create_queue(self, queue_name):
|
def ensure_queue(self, queue_name, callback):
|
||||||
# Initialize the queues we need
|
'''Ensure that a given queue has been declared, and then call
|
||||||
|
the callback with no arguments.'''
|
||||||
|
if queue_name not in self.queues:
|
||||||
self.channel.queue_declare(queue=queue_name, durable=True)
|
self.channel.queue_declare(queue=queue_name, durable=True)
|
||||||
self.queues.add(queue_name)
|
self.queues.add(queue_name)
|
||||||
|
callback()
|
||||||
|
|
||||||
def publish(self, queue_name, body):
|
def publish(self, queue_name, body):
|
||||||
if queue_name not in self.queues:
|
self.ensure_queue(queue_name,
|
||||||
self.create_queue(queue_name)
|
lambda: self.channel.basic_publish(
|
||||||
self.channel.basic_publish(exchange='',
|
exchange='',
|
||||||
routing_key=queue_name,
|
routing_key=queue_name,
|
||||||
properties=pika.BasicProperties(delivery_mode = 2,),
|
properties=pika.BasicProperties(delivery_mode=2),
|
||||||
body=body)
|
body=body))
|
||||||
|
|
||||||
def json_publish(self, queue_name, body):
|
def json_publish(self, queue_name, body):
|
||||||
return self.publish(queue_name, simplejson.dumps(body))
|
return self.publish(queue_name, simplejson.dumps(body))
|
||||||
|
|
||||||
def register_consumer(self, queue_name, callback):
|
def register_consumer(self, queue_name, consumer):
|
||||||
if queue_name not in self.queues:
|
def wrapped_consumer(ch, method, properties, body):
|
||||||
self.create_queue(queue_name)
|
consumer(ch, method, properties, body)
|
||||||
|
|
||||||
def wrapped_callback(ch, method, properties, body):
|
|
||||||
callback(ch, method, properties, body)
|
|
||||||
ch.basic_ack(delivery_tag=method.delivery_tag)
|
ch.basic_ack(delivery_tag=method.delivery_tag)
|
||||||
|
|
||||||
self.channel.basic_consume(wrapped_callback, queue=queue_name,
|
self.ensure_queue(queue_name,
|
||||||
consumer_tag=self._generate_ctag(queue_name))
|
lambda: self.channel.basic_consume(wrapped_consumer, queue=queue_name,
|
||||||
|
consumer_tag=self._generate_ctag(queue_name)))
|
||||||
|
|
||||||
def register_json_consumer(self, queue_name, callback):
|
def register_json_consumer(self, queue_name, callback):
|
||||||
def wrapped_callback(ch, method, properties, body):
|
def wrapped_callback(ch, method, properties, body):
|
||||||
@@ -88,3 +89,14 @@ class TornadoQueueClient(SimpleQueueClient):
|
|||||||
def _on_channel_open(self, channel):
|
def _on_channel_open(self, channel):
|
||||||
self.channel = channel
|
self.channel = channel
|
||||||
self.log.info('TornadoQueueClient connected')
|
self.log.info('TornadoQueueClient connected')
|
||||||
|
|
||||||
|
def ensure_queue(self, queue_name, callback):
|
||||||
|
def finish(frame):
|
||||||
|
self.queues.add(queue_name)
|
||||||
|
callback()
|
||||||
|
|
||||||
|
if queue_name not in self.queues:
|
||||||
|
self.channel.queue_declare(queue=queue_name, durable=True,
|
||||||
|
callback=finish)
|
||||||
|
else:
|
||||||
|
callback()
|
||||||
|
|||||||
Reference in New Issue
Block a user