2013-01-11 21:16:42 +01:00
|
|
|
from django.conf import settings
|
|
|
|
import pika
|
2013-01-18 19:16:34 +01:00
|
|
|
import logging
|
2013-01-11 21:16:42 +01:00
|
|
|
import simplejson
|
2013-02-15 17:01:28 +01:00
|
|
|
import random
|
2013-03-11 19:53:41 +01:00
|
|
|
import time
|
|
|
|
from collections import defaultdict
|
2013-01-11 21:16:42 +01:00
|
|
|
|
|
|
|
# This simple queuing library doesn't expose much of the power of
|
|
|
|
# rabbitmq/pika's queuing system; its purpose is to just provide an
|
|
|
|
# interface for external files to put things into queues and take them
|
|
|
|
# out from bots without having to import pika code all over our codebase.
|
2013-01-18 19:01:20 +01:00
|
|
|
class SimpleQueueClient(object):
|
2013-01-11 21:16:42 +01:00
|
|
|
def __init__(self):
|
2013-01-18 19:16:34 +01:00
|
|
|
self.log = logging.getLogger('humbug.queue')
|
2013-01-17 23:07:10 +01:00
|
|
|
self.queues = set()
|
2013-01-18 19:15:09 +01:00
|
|
|
self.channel = None
|
2013-03-11 19:53:41 +01:00
|
|
|
self.consumers = defaultdict(set)
|
2013-01-18 19:15:09 +01:00
|
|
|
self._connect()
|
2013-01-11 21:16:42 +01:00
|
|
|
|
2013-01-18 19:15:09 +01:00
|
|
|
def _connect(self):
|
|
|
|
self.connection = pika.BlockingConnection(self._get_parameters())
|
|
|
|
self.channel = self.connection.channel()
|
2013-01-18 19:16:34 +01:00
|
|
|
self.log.info('SimpleQueueClient connected')
|
2013-01-18 19:15:09 +01:00
|
|
|
|
2013-03-11 19:53:41 +01:00
|
|
|
def _reconnect(self):
|
|
|
|
self.connection = None
|
|
|
|
self.channel = None
|
|
|
|
self.queues = set()
|
|
|
|
self._connect()
|
|
|
|
|
2013-01-18 19:15:09 +01:00
|
|
|
def _get_parameters(self):
|
|
|
|
return pika.ConnectionParameters('localhost',
|
|
|
|
credentials = pika.PlainCredentials(
|
|
|
|
'humbug', settings.RABBITMQ_PASSWORD))
|
2013-01-17 23:15:40 +01:00
|
|
|
|
2013-02-15 17:01:28 +01:00
|
|
|
def _generate_ctag(self, queue_name):
|
|
|
|
return "%s_%s" % (queue_name, str(random.getrandbits(16)))
|
|
|
|
|
2013-03-11 19:53:41 +01:00
|
|
|
def _reconnect_callbacks(self):
|
|
|
|
for queue, consumers in self.consumers.items():
|
|
|
|
for consumer in consumers:
|
|
|
|
self.ensure_queue(queue, lambda: self.channel.basic_consume(
|
|
|
|
consumer,
|
|
|
|
queue=queue,
|
|
|
|
consumer_tag=self._generate_ctag(queue)))
|
|
|
|
|
2013-01-18 19:15:09 +01:00
|
|
|
def ready(self):
|
|
|
|
return self.channel is not None
|
|
|
|
|
2013-01-29 00:00:02 +01:00
|
|
|
def ensure_queue(self, queue_name, callback):
|
|
|
|
'''Ensure that a given queue has been declared, and then call
|
|
|
|
the callback with no arguments.'''
|
2013-03-11 19:53:41 +01:00
|
|
|
if not self.connection.is_open:
|
|
|
|
self._connect()
|
|
|
|
|
2013-01-29 00:00:02 +01:00
|
|
|
if queue_name not in self.queues:
|
|
|
|
self.channel.queue_declare(queue=queue_name, durable=True)
|
|
|
|
self.queues.add(queue_name)
|
|
|
|
callback()
|
2013-01-11 21:16:42 +01:00
|
|
|
|
|
|
|
def publish(self, queue_name, body):
|
2013-01-29 00:00:02 +01:00
|
|
|
self.ensure_queue(queue_name,
|
|
|
|
lambda: self.channel.basic_publish(
|
|
|
|
exchange='',
|
|
|
|
routing_key=queue_name,
|
|
|
|
properties=pika.BasicProperties(delivery_mode=2),
|
|
|
|
body=body))
|
2013-01-11 21:16:42 +01:00
|
|
|
|
|
|
|
def json_publish(self, queue_name, body):
|
2013-03-11 19:53:41 +01:00
|
|
|
try:
|
|
|
|
return self.publish(queue_name, simplejson.dumps(body))
|
|
|
|
except (AttributeError, pika.exceptions.AMQPConnectionError):
|
|
|
|
self.log.warning("Failed to send to rabbitmq, trying to reconnect and send again")
|
|
|
|
self._reconnect()
|
|
|
|
|
|
|
|
return self.publish(queue_name, simplejson.dumps(body))
|
2013-01-11 21:16:42 +01:00
|
|
|
|
2013-01-29 00:00:02 +01:00
|
|
|
def register_consumer(self, queue_name, consumer):
|
|
|
|
def wrapped_consumer(ch, method, properties, body):
|
|
|
|
consumer(ch, method, properties, body)
|
2013-01-11 21:16:42 +01:00
|
|
|
ch.basic_ack(delivery_tag=method.delivery_tag)
|
|
|
|
|
2013-03-11 19:53:41 +01:00
|
|
|
self.consumers[queue_name].add(wrapped_consumer)
|
2013-01-29 00:00:02 +01:00
|
|
|
self.ensure_queue(queue_name,
|
|
|
|
lambda: self.channel.basic_consume(wrapped_consumer, queue=queue_name,
|
|
|
|
consumer_tag=self._generate_ctag(queue_name)))
|
2013-01-11 21:16:42 +01:00
|
|
|
|
|
|
|
def register_json_consumer(self, queue_name, callback):
|
|
|
|
def wrapped_callback(ch, method, properties, body):
|
|
|
|
return callback(ch, method, properties, simplejson.loads(body))
|
|
|
|
return self.register_consumer(queue_name, wrapped_callback)
|
|
|
|
|
|
|
|
def start_consuming(self):
|
|
|
|
self.channel.start_consuming()
|
2013-02-15 17:03:28 +01:00
|
|
|
|
|
|
|
def stop_consuming(self):
|
|
|
|
self.channel.stop_consuming()
|
2013-01-18 23:15:23 +01:00
|
|
|
|
|
|
|
class TornadoQueueClient(SimpleQueueClient):
|
|
|
|
# Based on:
|
|
|
|
# https://pika.readthedocs.org/en/0.9.8/examples/asynchronous_consumer_example.html
|
|
|
|
|
2013-03-11 19:53:41 +01:00
|
|
|
def _connect(self, on_open_cb = None):
|
|
|
|
self.log.info("Beginning TornadoQueueClient connection")
|
2013-03-19 19:29:22 +01:00
|
|
|
self._on_open_cbs = []
|
|
|
|
if on_open_cb:
|
|
|
|
self._on_open_cbs.append(on_open_cb)
|
2013-01-18 23:15:23 +01:00
|
|
|
self.connection = pika.adapters.TornadoConnection(
|
|
|
|
self._get_parameters(),
|
2013-01-29 00:00:24 +01:00
|
|
|
on_open_callback = self._on_open,
|
|
|
|
stop_ioloop_on_close = False)
|
2013-01-18 23:15:23 +01:00
|
|
|
|
2013-03-11 19:53:41 +01:00
|
|
|
def _reconnect(self, on_open_cb = None):
|
|
|
|
self.connection = None
|
|
|
|
self.channel = None
|
|
|
|
self.queues = set()
|
|
|
|
self._connect(on_open_cb)
|
|
|
|
|
2013-01-18 23:15:23 +01:00
|
|
|
def _on_open(self, connection):
|
2013-03-11 19:53:41 +01:00
|
|
|
self.connection.add_on_close_callback(self._on_connection_closed)
|
2013-01-18 23:15:23 +01:00
|
|
|
self.connection.channel(
|
|
|
|
on_open_callback = self._on_channel_open)
|
|
|
|
|
|
|
|
def _on_channel_open(self, channel):
|
|
|
|
self.channel = channel
|
2013-03-19 19:29:22 +01:00
|
|
|
for callback in self._on_open_cbs:
|
|
|
|
callback()
|
|
|
|
self._on_open_cbs = []
|
2013-01-18 23:15:23 +01:00
|
|
|
self.log.info('TornadoQueueClient connected')
|
2013-01-29 00:00:02 +01:00
|
|
|
|
2013-03-11 19:53:41 +01:00
|
|
|
def _on_connection_closed(self, method_frame):
|
|
|
|
self.log.warning("TornadoQueueClient lost connection to RabbitMQ, reconnecting...")
|
|
|
|
from tornado import ioloop
|
|
|
|
|
|
|
|
# Try to reconnect in two seconds
|
|
|
|
retry_seconds = 2
|
|
|
|
def on_timeout():
|
|
|
|
try:
|
|
|
|
self._reconnect(self._reconnect_callbacks)
|
|
|
|
except pika.exceptions.AMQPConnectionError:
|
|
|
|
self.log.critical("Failed to reconnect to RabbitMQ, retrying...")
|
|
|
|
ioloop.IOLoop.instance().add_timeout(time.time() + retry_seconds, on_timeout)
|
|
|
|
|
|
|
|
ioloop.IOLoop.instance().add_timeout(time.time() + retry_seconds, on_timeout)
|
|
|
|
|
2013-01-29 00:00:02 +01:00
|
|
|
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()
|
2013-03-19 19:29:22 +01:00
|
|
|
|
|
|
|
def register_consumer(self, queue_name, consumer):
|
|
|
|
def wrapped_consumer(ch, method, properties, body):
|
|
|
|
consumer(ch, method, properties, body)
|
|
|
|
ch.basic_ack(delivery_tag=method.delivery_tag)
|
|
|
|
|
|
|
|
if not self.ready():
|
|
|
|
self._on_open_cbs.append(lambda: self.register_consumer(queue_name, consumer))
|
|
|
|
return
|
|
|
|
|
|
|
|
self.consumers[queue_name].add(wrapped_consumer)
|
|
|
|
self.ensure_queue(queue_name,
|
|
|
|
lambda: self.channel.basic_consume(wrapped_consumer, queue=queue_name,
|
|
|
|
consumer_tag=self._generate_ctag(queue_name)))
|