diff --git a/Gemfile b/Gemfile index d8d9ceeec..bd32b1897 100644 --- a/Gemfile +++ b/Gemfile @@ -21,7 +21,9 @@ gem 'pg', '0.14.0' gem 'compass-rails' gem 'rabl' # for JSON API development gem 'gon' # for passthrough of Ruby variables to Javascript variables - +gem 'eventmachine' +gem 'amqp' +gem 'logging-rails', :require => 'logging/rails' group :development, :test do gem 'rspec-rails', '2.11.0' diff --git a/app/controllers/api_music_sessions_controller.rb b/app/controllers/api_music_sessions_controller.rb index be6756944..7e946e641 100644 --- a/app/controllers/api_music_sessions_controller.rb +++ b/app/controllers/api_music_sessions_controller.rb @@ -5,6 +5,11 @@ class ApiMusicSessionsController < ApplicationController respond_to :json + def initialize + @mq_router = MQRouter.new + @message_factory = MessageFactory.new + end + def index @music_sessions = MusicSession.paginate(page: params[:page]) end @@ -35,16 +40,18 @@ class ApiMusicSessionsController < ApplicationController def participant_create @music_session = MusicSession.find(params[:id]) + client_id = params[:client_id] @music_session_client = MusicSessionClient.new() @music_session_client.ip_address = params[:ip_address] - @music_session_client.client_id = params[:client_id] + @music_session_client.client_id = client_id @music_session_client.music_session = @music_session @music_session_client.user = current_user - @music_session_client.save + saved = @music_session_client.save - unless has_errors? + unless saved # send out notification to queue to the rest of the session - + user_joined = @message_factory.user_joined_music_session(current_user.id, current_user.username) + @mq_router.user_publish_to_session(@music_session, current_user, user_joined, sender = {:client_id => client_id}) end respond_with @music_session_client, responder: ApiResponder, :location => api_session_participant_detail_url(@music_session_client) diff --git a/config/environments/development.rb b/config/environments/development.rb index aa53a0aab..6125a237d 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -34,4 +34,10 @@ SampleApp::Application.configure do # Expands the lines which load the assets config.assets.debug = true + + # Set the logging destination(s) + config.log_to = %w[stdout file] + + # Show the logging configuration on STDOUT + config.show_log_configuration = true end diff --git a/config/environments/production.rb b/config/environments/production.rb index 47abeeb2b..fe7b0ac91 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -64,4 +64,10 @@ SampleApp::Application.configure do # Log the query plan for queries taking more than this (works # with SQLite, MySQL, and PostgreSQL) # config.active_record.auto_explain_threshold_in_seconds = 0.5 + + # Set the logging destination(s) + config.log_to = %w[file] + + # Show the logging configuration on STDOUT + config.show_log_configuration = false end diff --git a/config/logging.rb b/config/logging.rb new file mode 100644 index 000000000..42cb34eff --- /dev/null +++ b/config/logging.rb @@ -0,0 +1,111 @@ + +Logging::Rails.configure do |config| + + # Objects will be converted to strings using the :format_as method. + Logging.format_as :inspect + + # The default layout used by the appenders. + layout = Logging.layouts.pattern(:pattern => '[%d] %-5l %c : %m\n') + + # Setup a color scheme called 'bright' than can be used to add color codes + # to the pattern layout. Color schemes should only be used with appenders + # that write to STDOUT or STDERR; inserting terminal color codes into a file + # is generally considered bad form. + # + Logging.color_scheme( 'bright', + :levels => { + :info => :green, + :warn => :yellow, + :error => :red, + :fatal => [:white, :on_red] + }, + :date => :blue, + :logger => :cyan, + :message => :magenta + ) + + # Configure an appender that will write log events to STDOUT. A colorized + # pattern layout is used to format the log events into strings before + # writing. + # + Logging.appenders.stdout( 'stdout', + :auto_flushing => true, + :layout => Logging.layouts.pattern( + :pattern => '[%d] %-5l %c : %m\n', + :color_scheme => 'bright' + ) + ) if config.log_to.include? 'stdout' + + # Configure an appender that will write log events to a file. The file will + # be rolled on a daily basis, and the past 7 rolled files will be kept. + # Older files will be deleted. The default pattern layout is used when + # formatting log events into strings. + # + Logging.appenders.rolling_file( 'file', + :filename => config.paths['log'].first, + :keep => 7, + :age => 'daily', + :truncate => false, + :auto_flushing => true, + :layout => layout + ) if config.log_to.include? 'file' + + # Configure an appender that will send an email for "error" and "fatal" log + # events. All other log events will be ignored. Furthermore, log events will + # be buffered for one minute (or 200 events) before an email is sent. This + # is done to prevent a flood of messages. + # + Logging.appenders.email( 'email', + :from => "server@#{config.action_mailer.smtp_settings[:domain]}", + :to => "developers@#{config.action_mailer.smtp_settings[:domain]}", + :subject => "Rails Error [#{%x(uname -n).strip}]", + :server => config.action_mailer.smtp_settings[:address], + :domain => config.action_mailer.smtp_settings[:domain], + :acct => config.action_mailer.smtp_settings[:user_name], + :passwd => config.action_mailer.smtp_settings[:password], + :authtype => config.action_mailer.smtp_settings[:authentication], + + :auto_flushing => 200, # send an email after 200 messages have been buffered + :flush_period => 60, # send an email after one minute + :level => :error, # only process log events that are "error" or "fatal" + :layout => layout + ) if config.log_to.include? 'email' + + # Setup the root logger with the Rails log level and the desired set of + # appenders. The list of appenders to use should be set in the environment + # specific configuration file. + # + # For example, in a production application you would not want to log to + # STDOUT, but you would want to send an email for "error" and "fatal" + # messages: + # + # => config/environments/production.rb + # + # config.log_to = %w[file email] + # + # In development you would want to log to STDOUT and possibly to a file: + # + # => config/environments/development.rb + # + # config.log_to = %w[stdout file] + # + Logging.logger.root.level = config.log_level + Logging.logger.root.appenders = config.log_to unless config.log_to.empty? + + # Under Phusion Passenger smart spawning, we need to reopen all IO streams + # after workers have forked. + # + # The rolling file appender uses shared file locks to ensure that only one + # process will roll the log file. Each process writing to the file must have + # its own open file descriptor for `flock` to function properly. Reopening + # the file descriptors after forking ensures that each worker has a unique + # file descriptor. + # + if defined?(PhusionPassenger) + PhusionPassenger.on_event(:starting_worker_process) do |forked| + Logging.reopen if forked + end + end + +end +