Permalink
Browse files

Method to build RabbitMQ connection_parameters.

  • Loading branch information...
calumhalcrow committed Dec 19, 2011
1 parent d0e6fd8 commit fa1f7da1c39765cbc08324ce5e99779d47f0ec39
Showing with 33 additions and 27 deletions.
  1. +26 −20 chat/chatroom.py
  2. +4 −1 frontend/events/signals.py
  3. +3 −6 server.py
View
@@ -1,5 +1,6 @@
#! /usr/bin/env python
-from os import path as op
+import os
+import os.path as op
import signal
import pika
from pika.adapters.tornado_connection import TornadoConnection
@@ -11,6 +12,7 @@
from lib.observer import Observable
import django.core.handlers.wsgi
from urlparse import urlparse
+from django.conf import settings
ROOT = op.normpath(op.join(op.dirname(__file__), '../'))
@@ -33,13 +35,11 @@ def send_message(self, message):
class PikaClient(Observable):
- def __init__(self, queue_name, rabbitmq_url):
+ def __init__(self):
Observable.__init__(self)
- self.queue_name = queue_name
-
- # amqp://uname:pwd@host.heroku.srs.rabbitmq.com:13029/vhost
- self.rabbitmq_url = urlparse(rabbitmq_url)
+ #self.queue_name = settings.RABBITMQ_QUEUE_NAME
+ self.queue_name = 'hello'
# States
self.connected = False
@@ -52,25 +52,31 @@ def __init__(self, queue_name, rabbitmq_url):
# Message caches
self.messages = list()
+ # amqp://uname:pwd@host.heroku.srs.rabbitmq.com:13029/vhost
+ def connection_parameters(self):
+ rabbitmq_url = urlparse('RABBITMQ_URL' in os.environ and os.environ['RABBITMQ_URL'] or 'amqp://localhost')
+
+ connection_parameters = {'host': rabbitmq_url.hostname}
+ if rabbitmq_url.port:
+ connection_parameters['port'] = rabbitmq_url.port
+ if rabbitmq_url.username:
+ connection_parameters['credentials'] = pika.PlainCredentials(
+ rabbitmq_url.username,
+ rabbitmq_url.password)
+ if rabbitmq_url.path:
+ connection_parameters['virtual_host'] = rabbitmq_url.path
+
+ return connection_parameters
+
def connect(self):
if self.connecting:
pika.log.info('Already connecting to RabbitMQ')
return
pika.log.info('Connecting to RabbitMQ')
self.connecting = True
- # build RabbitMQ connection parameters:
- connection_params = {'host': self.rabbitmq_url.hostname}
- if self.rabbitmq_url.port:
- connection_params['port'] = self.rabbitmq_url.port
- if self.rabbitmq_url.username:
- connection_params['credentials'] = pika.PlainCredentials(
- self.rabbitmq_url.username,
- self.rabbitmq_url.password)
- if self.rabbitmq_url.path:
- connection_params['virtual_host'] = self.rabbitmq_url.path
-
- param = pika.ConnectionParameters(**connection_params)
+ connection_parameters = self.connection_parameters()
+ param = pika.ConnectionParameters(**connection_parameters)
self.connection = TornadoConnection(param,
on_open_callback=self.on_connected)
@@ -110,7 +116,7 @@ def get_messages(self):
class Application():
- def __init__(self, port, rabbitmq_url):
+ def __init__(self, port):
settings = {
'debug': True,
'enabled_protocols': ['websocket',
@@ -130,7 +136,7 @@ def __init__(self, port, rabbitmq_url):
(r'.*', tornado.web.FallbackHandler, {'fallback': wsgi_app}),
], **settings)
- self.message_queue = PikaClient('hello', rabbitmq_url)
+ self.message_queue = PikaClient()
self.message_queue.attach(self)
self.server = None
@@ -5,8 +5,11 @@
from events.models import Message, Checkin
from django.utils import simplejson
+from chat.chatroom import PikaClient
+
def publish(data):
- connection = pika.BlockingConnection(pika.ConnectionParameters(**settings.RABBITMQ_CONN))
+ connection_parameters = PikaClient().connection_parameters()
+ connection = pika.BlockingConnection(pika.ConnectionParameters(**connection_parameters))
channel = connection.channel()
channel.queue_declare(queue=settings.RABBITMQ_QUEUE_NAME)
View
@@ -11,13 +11,13 @@
from chat.chatroom import Application
ROOT = op.normpath(op.dirname(__file__))
-def main(port, rabbitmq_url):
+def main(port):
# Starting Django
sys.path.append(op.join(ROOT, 'frontend'))
os.environ['DJANGO_SETTINGS_MODULE'] = 'frontend.settings'
# Tornadio app part
- app = Application(port, rabbitmq_url)
+ app = Application(port)
# Cleanup code
def shutdown(sig, frame):
app.stop()
@@ -36,7 +36,4 @@ def shutdown(sig, frame):
parser.add_option('-p', '--port', dest='port', help='Specify the socket IO port')
(options, args) = parser.parse_args()
- rabbitmq_url = 'RABBITMQ_URL' in os.environ and os.environ['RABBITMQ_URL'] or 'amqp://localhost'
-
- main(port=options.port or 8001,
- rabbitmq_url=rabbitmq_url)
+ main(port=options.port or 8001)

0 comments on commit fa1f7da

Please sign in to comment.