diff --git a/README.md b/README.md index b91bfa8..3f66ee4 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,7 @@ If you plan to deploy this app to a production environment: * Change the secret token at /config/initializers/secret_token.rb * Remove "= render "layouts/forkapp"" at /app/views/layouts/application.html.haml * precompile the assets -> RAILS_ENV=production rake assets:precompile +* enable "config.middleware" option at /config/application.rb to enable async http calls Testing ------- diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index e22b2bb..58283b7 100755 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -40,7 +40,7 @@ def cloudfoundry_client(cf_target_url, cf_auth_token = nil) end def which_faraday_adapter? - if defined?(EM::Synchrony) && EM.reactor_running? + if Utils::ModuleLoaded.synchrony? && Utils::ModuleLoaded.fiberpool? :em_synchrony else :net_http diff --git a/config/application.rb b/config/application.rb index 0cd1830..7f2c357 100644 --- a/config/application.rb +++ b/config/application.rb @@ -65,7 +65,8 @@ class Application < Rails::Application # Version of your assets, change this if you want to expire all your assets config.assets.version = '1.0' + # Enable this option if you want async http calls # Place the FiberPool early in the pipeline (you can see your app's current pipeline using `rake middleware`) - config.middleware.insert_before Rails::Rack::Logger, Rack::FiberPool + #config.middleware.insert_before Rails::Rack::Logger, Rack::FiberPool end end diff --git a/lib/utils.rb b/lib/utils.rb index e59ab4e..e614d74 100644 --- a/lib/utils.rb +++ b/lib/utils.rb @@ -1,4 +1,14 @@ module Utils + module ModuleLoaded + def self.synchrony? + defined?(EM::Synchrony) && EM.reactor_running? + end + + def self.fiberpool? + CfConsole::Application.config.middleware.middlewares.include?(Rack::FiberPool) + end + end + module EMDeferredBlock def self.defer_block(&blk) f = Fiber.current @@ -40,7 +50,7 @@ def self.each(list, concurrency = 1, &blk) error = ex end end - if defined?(EM::Synchrony) && EM.reactor_running? + if Utils::ModuleLoaded.synchrony? && Utils::ModuleLoaded.fiberpool? begin result = EM::Synchrony::FiberIterator.new(list, concurrency).each(foreach) rescue => ex @@ -69,7 +79,7 @@ def self.map(list, concurrency = 1, &blk) end }.resume end - if defined?(EM::Synchrony) && EM.reactor_running? + if Utils::ModuleLoaded.synchrony? && Utils::ModuleLoaded.fiberpool? begin result = EM::Synchrony::Iterator.new(list, concurrency).map(&foreach) rescue => ex