From 807116e95577a84efab895b28f5cd9916a7a252d Mon Sep 17 00:00:00 2001 From: Zev Benjamin Date: Wed, 23 Oct 2013 16:33:59 -0400 Subject: [PATCH] Delay connecting to RabbitMQ until it's necessary Previously we were connecting at import-time. (imported from commit 9a638f0d238f3b6b00feb4aa524098a64953cb92) --- zerver/lib/queue.py | 16 +++++++++++----- zerver/management/commands/runtornado.py | 3 ++- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/zerver/lib/queue.py b/zerver/lib/queue.py index 5ed55b5232..ebf2123acd 100644 --- a/zerver/lib/queue.py +++ b/zerver/lib/queue.py @@ -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) diff --git a/zerver/management/commands/runtornado.py b/zerver/management/commands/runtornado.py index bd39f63170..64f5692d45 100644 --- a/zerver/management/commands/runtornado.py +++ b/zerver/management/commands/runtornado.py @@ -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)