summary refs log tree commit homepage
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2010-02-24 18:21:07 -0800
committerEric Wong <normalperson@yhbt.net>2010-02-24 18:21:07 -0800
commita057e63abb6f1deab6971eb84f95101133ffb65a (patch)
tree711263541f4da138303fb739b52b0a06c47085e4
parent29661ccaaa03ab07f4de0d5c38f730f1d557b418 (diff)
We'll use our Rails-only version of Unicorn.builder so
the lambda is safe without another binding.
-rwxr-xr-xbin/unicorn_rails114
1 files changed, 59 insertions, 55 deletions
diff --git a/bin/unicorn_rails b/bin/unicorn_rails
index 8a7c21a..b327054 100755
--- a/bin/unicorn_rails
+++ b/bin/unicorn_rails
@@ -120,69 +120,73 @@ end
 
 require 'pp' if $DEBUG
 
-# this won't run until after forking if preload_app is false
-app = lambda do ||
-  # Load Rails and (possibly) the private version of Rack it bundles.
-  begin
-    require 'config/boot'
-  rescue LoadError => err
-    abort "#$0 must be run inside RAILS_ROOT: #{err.inspect}"
-  end
-
-  inner_app = case config
-  when nil
-    require 'config/environment'
-
-    defined?(::Rails::VERSION::STRING) or
-      abort "Rails::VERSION::STRING not defined by config/{boot,environment}"
-    # it seems Rails >=2.2 support Rack, but only >=2.3 requires it
-    old_rails = case ::Rails::VERSION::MAJOR
-    when 0, 1 then true
-    when 2 then Rails::VERSION::MINOR < 3 ? true : false
-    else
-      false
+def rails_builder(config, daemonize)
+  # this lambda won't run until after forking if preload_app is false
+  lambda do ||
+    # Load Rails and (possibly) the private version of Rack it bundles.
+    begin
+      require 'config/boot'
+    rescue LoadError => err
+      abort "#$0 must be run inside RAILS_ROOT: #{err.inspect}"
     end
 
-    if old_rails
-      require 'unicorn/app/old_rails'
-      Unicorn::App::OldRails.new
-    else
-      ActionController::Dispatcher.new
-    end
-  when /\.ru$/
-    raw = File.open(config, "rb") { |fp| fp.sysread(fp.stat.size) }
-    raw.sub!(/^__END__\n.*/, '')
-    eval("Rack::Builder.new {(#{raw}\n)}.to_app", nil, config)
-  else
-    require config
-    Object.const_get(File.basename(config, '.rb').capitalize)
-  end
-
-  Rack::Builder.new do
-    map_path = ENV['RAILS_RELATIVE_URL_ROOT'] || '/'
-    if inner_app.class.to_s == "Unicorn::App::OldRails"
-      if map_path != '/'
-        # patches + tests welcome, but I really cbf to deal with this
-        # since all apps I've ever dealt with just use "/" ...
-        $stderr.puts "relative URL roots may not work for older Rails"
+    inner_app = case config
+    when nil
+      require 'config/environment'
+
+      defined?(::Rails::VERSION::STRING) or
+        abort "Rails::VERSION::STRING not defined by config/{boot,environment}"
+      # it seems Rails >=2.2 support Rack, but only >=2.3 requires it
+      old_rails = case ::Rails::VERSION::MAJOR
+      when 0, 1 then true
+      when 2 then Rails::VERSION::MINOR < 3 ? true : false
+      else
+        false
       end
-      $stderr.puts "LogTailer not available for Rails < 2.3" unless daemonize
-      $stderr.puts "Debugger not available" if $DEBUG
-      map(map_path) do
-        use Unicorn::App::OldRails::Static
-        run inner_app
+
+      if old_rails
+        require 'unicorn/app/old_rails'
+        Unicorn::App::OldRails.new
+      else
+        ActionController::Dispatcher.new
       end
+    when /\.ru$/
+      raw = File.open(config, "rb") { |fp| fp.sysread(fp.stat.size) }
+      raw.sub!(/^__END__\n.*/, '')
+      eval("Rack::Builder.new {(#{raw}\n)}.to_app", nil, config)
     else
-      use Rails::Rack::LogTailer unless daemonize
-      use Rails::Rack::Debugger if $DEBUG
-      map(map_path) do
-        use Rails::Rack::Static
-        run inner_app
-      end
+      require config
+      Object.const_get(File.basename(config, '.rb').capitalize)
     end
-  end.to_app
+
+    Rack::Builder.new do
+      map_path = ENV['RAILS_RELATIVE_URL_ROOT'] || '/'
+      if inner_app.class.to_s == "Unicorn::App::OldRails"
+        if map_path != '/'
+          # patches + tests welcome, but I really cbf to deal with this
+          # since all apps I've ever dealt with just use "/" ...
+          $stderr.puts "relative URL roots may not work for older Rails"
+        end
+        $stderr.puts "LogTailer not available for Rails < 2.3" unless daemonize
+        $stderr.puts "Debugger not available" if $DEBUG
+        map(map_path) do
+          use Unicorn::App::OldRails::Static
+          run inner_app
+        end
+      else
+        use Rails::Rack::LogTailer unless daemonize
+        use Rails::Rack::Debugger if $DEBUG
+        map(map_path) do
+          use Rails::Rack::Static
+          run inner_app
+        end
+      end
+    end.to_app
+  end
 end
 
+app = rails_builder(config, daemonize)
+
 listeners << "#{host}:#{port}" if set_listener
 
 if $DEBUG