Delay connecting to RabbitMQ until it's necessary

Previously we were connecting at import-time.

(imported from commit 9a638f0d238f3b6b00feb4aa524098a64953cb92)
This commit is contained in:
Zev Benjamin 2013-10-23 16:33:59 -04:00
parent 96b4103c80
commit 807116e955
2 changed files with 13 additions and 6 deletions

View File

@ -230,10 +230,16 @@ class TornadoQueueClient(SimpleQueueClient):
lambda: self.channel.basic_consume(wrapped_consumer, queue=queue_name,
consumer_tag=self._generate_ctag(queue_name)))
if settings.RUNNING_INSIDE_TORNADO and settings.USING_RABBITMQ:
queue_client = TornadoQueueClient()
elif settings.USING_RABBITMQ:
queue_client = SimpleQueueClient()
queue_client = None
def get_queue_client():
global queue_client
if queue_client is None:
if settings.RUNNING_INSIDE_TORNADO and settings.USING_RABBITMQ:
queue_client = TornadoQueueClient()
elif settings.USING_RABBITMQ:
queue_client = SimpleQueueClient()
return queue_client
def setup_tornado_rabbitmq():
# When tornado is shut down, disconnect cleanly from rabbitmq
@ -250,7 +256,7 @@ queue_lock = threading.RLock()
def queue_json_publish(queue_name, event, processor):
with queue_lock:
if settings.USING_RABBITMQ:
queue_client.json_publish(queue_name, event)
get_queue_client().json_publish(queue_name, event)
else:
processor(event)

View File

@ -26,7 +26,7 @@ from zerver.lib.socket import get_sockjs_router, respond_send_message
from zerver.middleware import async_request_stop
if settings.USING_RABBITMQ:
from zerver.lib.queue import queue_client
from zerver.lib.queue import get_queue_client
class Command(BaseCommand):
option_list = BaseCommand.option_list + (
@ -80,6 +80,7 @@ class Command(BaseCommand):
print "Quit the server with %s." % (quit_command,)
if settings.USING_RABBITMQ:
queue_client = get_queue_client()
# Process notifications received via RabbitMQ
def process_notification(chan, method, props, data):
tornado_callbacks.process_notification(data)