From a1cb141fabfd2d5ed00ad5cf1d502d916c787a2e Mon Sep 17 00:00:00 2001 From: Seth Call Date: Mon, 15 Oct 2012 21:11:49 -0500 Subject: [PATCH] * VRFS-19; wip --- .../api_music_sessions_controller.rb | 5 +++ config/application.rb | 2 + config/initializers/eventmachine.rb | 41 +++++++++++++++++++ 3 files changed, 48 insertions(+) create mode 100644 config/initializers/eventmachine.rb diff --git a/app/controllers/api_music_sessions_controller.rb b/app/controllers/api_music_sessions_controller.rb index c0a1d9639..be6756944 100644 --- a/app/controllers/api_music_sessions_controller.rb +++ b/app/controllers/api_music_sessions_controller.rb @@ -42,6 +42,11 @@ class ApiMusicSessionsController < ApplicationController @music_session_client.user = current_user @music_session_client.save + unless has_errors? + # send out notification to queue to the rest of the session + + end + respond_with @music_session_client, responder: ApiResponder, :location => api_session_participant_detail_url(@music_session_client) end diff --git a/config/application.rb b/config/application.rb index 390f02ace..0a34552b6 100644 --- a/config/application.rb +++ b/config/application.rb @@ -71,5 +71,7 @@ module SampleApp # Add the assets/fonts directory to assets.paths config.assets.paths << "#{Rails.root}/app/assets/fonts" + config.rabbitmq_host = "localhost" + config.rabbitmq_port = 5672 end end diff --git a/config/initializers/eventmachine.rb b/config/initializers/eventmachine.rb new file mode 100644 index 000000000..34b964974 --- /dev/null +++ b/config/initializers/eventmachine.rb @@ -0,0 +1,41 @@ +require 'amqp' +require 'jam_ruby' + +# Creates a connection to RabbitMQ. +# On that single connection, a channel is created (which is a way to multiplex multiple queues/topics over the same TCP connection with rabbitmq) +# Then connections to the client_exchange and user_exchange are made, and put into the MQRouter static variables +# If this code completes (which implies that Rails can start to begin with, because this is in an initializer), +# then the Rails app itself is free to send messages over these exchanges +# TODO: reconnect logic if rabbitmq goes down... + +module JamWebEventMachine + + def self.start + + Thread.abort_on_exception = true + + # create a new thread separate from the Rails main thread that EventMachine can run on + Thread.new do + + EM.run do + AMQP.start(:host => Rails.application.config.rabbitmq_host, :port => Rails.application.config.rabbitmq_port) do |connection| + AMQP::Channel.new do |channel, open_ok| + Rails.logger.debug("Channel ##{channel.id} is now open!") + + AMQP::Exchange.new(channel, :topic, "clients") do |exchange| + Rails.logger.debug("#{exchange.name} is ready to go") + MQRouter.client_exchange = exchange + end + + AMQP::Exchange.new(channel, :topic, "users") do |exchange| + Rails.logger.debug("#{exchange.name} is ready to go") + MQRouter.user_exchange = exchange + end + end + end + end + end + end +end + +JamWebEventMachine.start \ No newline at end of file