unicorn_rails cleanup (possible fix for Rails3) pushed

Eric Wong normalperson at yhbt.net
Tue Jun 8 15:20:34 EDT 2010


Hongli Lai <hongli at phusion.nl> wrote:
> Hi Eric.
> 
> It would appear that recent Rails 3 changes have broken unicorn_rails,
> just like they broke Phusion Passenger. Here's a patch which fixes the
> problem.
> http://gist.github.com/429944

Thanks Hongli, so this only affects people who remove the
config.ru that Rails 3 creates for them?  Yikes...

A few comments:

+  if ::Rails::VERSION::MAJOR >= 3 && ::File.exist?('config/application.rb')
+    ::File.read('config/application.rb') =~ /^module (.+)$/

Maybe a more flexible regexp like this: /^module\s+([\w:]+)\s*$/ (or
maybe even starting with: "^\s*") would be more reliable for folks who
leave extra spaces around.

Unfortunately, Ruby is not Python :)

@@ -148,9 +167,9 @@ def rails_builder(daemonize)
       else
         use Rails::Rack::LogTailer unless daemonize
         use Rails::Rack::Debugger if $DEBUG
+        use Rails::Rack::Static, map_path
         map(map_path) do
-          use Rails::Rack::Static
-          run ActionController::Dispatcher.new
+          run rails_dispatcher

Changing the call to use Rails::Rack::Static there is wrong.  map_path
is the URI prefix (RAILS_RELATIVE_URL_ROOT) and not the static file
path we serve from.  It appears the deprecation in Rails 3 broke some
things and ActionDispatch::Static is configured slightly differently.

Let me know if the edited patch below looks alright to you.

I'll also push out a few Rails 3 tests that exercise the missing
config.ru cases.

>From 222ae0a353eda446a480e5c4b473a218304f9594 Mon Sep 17 00:00:00 2001
From: Hongli Lai (Phusion) <hongli at phusion.nl>
Date: Tue, 8 Jun 2010 13:22:25 +0200
Subject: [PATCH] Fix unicorn_rails compatibility with the latest Rails 3 code

This allows us to properly detect Rails 3 installations
in cases where config.ru is not present.

[ew: expanded commit message
 fixed static file serving,
 more flexible regexp for matching module ]

ref: mid.gmane.org/AANLkTiksBxIo_PFWoiPTWi1entXZRb7D2uE-Rl7H3lbw at mail.gmail.com
Acked-by: Eric Wong <normalperson at yhbt.net>
---
 bin/unicorn_rails |   26 ++++++++++++++++++++++++--
 1 files changed, 24 insertions(+), 2 deletions(-)

diff --git a/bin/unicorn_rails b/bin/unicorn_rails
index 45a9b11..ed235ba 100755
--- a/bin/unicorn_rails
+++ b/bin/unicorn_rails
@@ -109,6 +109,24 @@ end
 
 ru = ARGV[0] || (File.exist?('config.ru') ? 'config.ru' : nil)
 
+def rails_dispatcher
+  if ::Rails::VERSION::MAJOR >= 3 && ::File.exist?('config/application.rb')
+    if ::File.read('config/application.rb') =~ /^module\s+([\w:]+)\s*$/
+      app_module = Object.const_get($1)
+      begin
+        result = app_module::Application
+      rescue NameError
+      end
+    end
+  end
+
+  if result.nil? && defined?(ActionController::Dispatcher)
+    result = ActionController::Dispatcher.new
+  end
+
+  result || abort("Unable to locate the application dispatcher class")
+end
+
 def rails_builder(daemonize)
   # this lambda won't run until after forking if preload_app is false
   lambda do ||
@@ -149,8 +167,12 @@ def rails_builder(daemonize)
         use Rails::Rack::LogTailer unless daemonize
         use Rails::Rack::Debugger if $DEBUG
         map(map_path) do
-          use Rails::Rack::Static
-          run ActionController::Dispatcher.new
+          if defined?(ActionDispatch::Static)
+            use ActionDispatch::Static, "#{Rails.root}/public"
+          else
+            use Rails::Rack::Static
+          end
+          run rails_dispatcher
         end
       end
     end.to_app
-- 
Eric Wong


More information about the mongrel-unicorn mailing list