From 1c8eef7dbdc8f9875195258351fb9e11b063b1c1 Mon Sep 17 00:00:00 2001 From: Sam Date: Mon, 11 Mar 2013 05:33:20 -0700 Subject: [PATCH] added basic passenger support, no long polling but functions clean up initializers so they are ordered properly --- config/application.rb | 6 ++---- config/initializers/01-redis.rb | 20 +++++++++++++++++++ ...eedom_patches.rb => 02-freedom_patches.rb} | 0 .../{sql_builder.rb => 03-sql_builder.rb} | 0 .../{site_settings.rb => 04-site_settings.rb} | 0 config/initializers/sidekiq.rb | 11 +--------- lib/discourse_redis.rb | 7 +++++++ .../lib/message_bus/rack/middleware.rb | 8 +++++--- 8 files changed, 35 insertions(+), 17 deletions(-) create mode 100644 config/initializers/01-redis.rb rename config/initializers/{freedom_patches.rb => 02-freedom_patches.rb} (100%) rename config/initializers/{sql_builder.rb => 03-sql_builder.rb} (100%) rename config/initializers/{site_settings.rb => 04-site_settings.rb} (100%) diff --git a/config/application.rb b/config/application.rb index d2d3e0821..b27a51805 100644 --- a/config/application.rb +++ b/config/application.rb @@ -88,11 +88,9 @@ module Discourse # Our templates shouldn't start with 'discourse/templates' config.handlebars.templates_root = 'discourse/templates' + require 'discourse_redis' # Use redis for our cache - redis_config = YAML::load(File.open("#{Rails.root}/config/redis.yml"))[Rails.env] - redis_store = ActiveSupport::Cache::RedisStore.new "redis://#{redis_config['host']}:#{redis_config['port']}/#{redis_config['cache_db']}" - redis_store.options[:namespace] = -> { DiscourseRedis.namespace } - config.cache_store = redis_store + config.cache_store = DiscourseRedis.new_redis_store # Test with rack::cache disabled. Nginx does this for us config.action_dispatch.rack_cache = nil diff --git a/config/initializers/01-redis.rb b/config/initializers/01-redis.rb new file mode 100644 index 000000000..9fcc439fa --- /dev/null +++ b/config/initializers/01-redis.rb @@ -0,0 +1,20 @@ +require "#{Rails.root}/lib/discourse_redis" + +$redis = DiscourseRedis.new + +if Rails.env.development? && !ENV['DO_NOT_FLUSH_REDIS'] + puts "Flushing redis (development mode)" + $redis.flushall +end + +if defined?(PhusionPassenger) + PhusionPassenger.on_event(:starting_worker_process) do |forked| + if forked + # We're in smart spawning mode. + $redis = DiscourseRedis.new + Discourse::Application.config.cache_store.reconnect# = DiscourseRedis.new_redis_store + else + # We're in conservative spawning mode. We don't need to do anything. + end + end +end diff --git a/config/initializers/freedom_patches.rb b/config/initializers/02-freedom_patches.rb similarity index 100% rename from config/initializers/freedom_patches.rb rename to config/initializers/02-freedom_patches.rb diff --git a/config/initializers/sql_builder.rb b/config/initializers/03-sql_builder.rb similarity index 100% rename from config/initializers/sql_builder.rb rename to config/initializers/03-sql_builder.rb diff --git a/config/initializers/site_settings.rb b/config/initializers/04-site_settings.rb similarity index 100% rename from config/initializers/site_settings.rb rename to config/initializers/04-site_settings.rb diff --git a/config/initializers/sidekiq.rb b/config/initializers/sidekiq.rb index 571a034f1..81bce9cf2 100644 --- a/config/initializers/sidekiq.rb +++ b/config/initializers/sidekiq.rb @@ -1,16 +1,7 @@ -require "#{Rails.root}/lib/discourse_redis" - -$redis = DiscourseRedis.new - -if Rails.env.development? && !ENV['DO_NOT_FLUSH_REDIS'] - puts "Flushing redis (development mode)" - $redis.flushall -end - Sidekiq.configure_server do |config| config.redis = { :url => $redis.url, :namespace => 'sidekiq' } end Sidekiq.configure_client do |config| config.redis = { :url => $redis.url, :namespace => 'sidekiq' } -end \ No newline at end of file +end diff --git a/lib/discourse_redis.rb b/lib/discourse_redis.rb index e6abbb9ca..4bdc05596 100644 --- a/lib/discourse_redis.rb +++ b/lib/discourse_redis.rb @@ -35,6 +35,13 @@ class DiscourseRedis RailsMultisite::ConnectionManagement.current_db end + def self.new_redis_store + redis_config = YAML::load(File.open("#{Rails.root}/config/redis.yml"))[Rails.env] + redis_store = ActiveSupport::Cache::RedisStore.new "redis://#{redis_config['host']}:#{redis_config['port']}/#{redis_config['cache_db']}" + redis_store.options[:namespace] = -> { DiscourseRedis.namespace } + redis_store + end + def url "redis://#{@config['host']}:#{@config['port']}/#{@config['db']}" end diff --git a/vendor/gems/message_bus/lib/message_bus/rack/middleware.rb b/vendor/gems/message_bus/lib/message_bus/rack/middleware.rb index ec16a3d61..d7e7d8ff7 100644 --- a/vendor/gems/message_bus/lib/message_bus/rack/middleware.rb +++ b/vendor/gems/message_bus/lib/message_bus/rack/middleware.rb @@ -9,8 +9,10 @@ class MessageBus::Rack::Middleware def self.start_listener unless @started_listener MessageBus.subscribe do |msg| - EM.next_tick do - @@connection_manager.notify_clients(msg) if @@connection_manager + if EM.reactor_running? + EM.next_tick do + @@connection_manager.notify_clients(msg) if @@connection_manager + end end end @started_listener = true @@ -73,7 +75,7 @@ class MessageBus::Rack::Middleware if backlog.length > 0 [200, headers, [self.class.backlog_to_json(backlog)] ] - elsif MessageBus.long_polling_enabled? && env['QUERY_STRING'] !~ /dlp=t/ + elsif MessageBus.long_polling_enabled? && env['QUERY_STRING'] !~ /dlp=t/ && EM.reactor_running? response = Thin::AsyncResponse.new(env) response.headers["Cache-Control"] = "must-revalidate, private, max-age=0" response.headers["Content-Type"] ="application/json; charset=utf-8"