about summary refs log tree commit homepage
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2009-03-01 14:43:04 -0800
committerEric Wong <normalperson@yhbt.net>2009-03-03 11:13:12 -0800
commit723254df772154e2790b26923d4827a096c53428 (patch)
tree4f790b95a910e3f379dd5199341bd877c4f7e6ae
parent9a7e1f1a7e986a59eb4be9b6a4d093f3366c9c72 (diff)
downloadunicorn-723254df772154e2790b26923d4827a096c53428.tar.gz
Some applications do not handle loading before forking
out-of-the-box very gracefully, this starts adding support
to build the Rack(-ish) application later in the process.
-rwxr-xr-xbin/unicorn39
-rw-r--r--lib/unicorn.rb5
2 files changed, 28 insertions, 16 deletions
diff --git a/bin/unicorn b/bin/unicorn
index e03c713..1ea3d1f 100755
--- a/bin/unicorn
+++ b/bin/unicorn
@@ -105,30 +105,37 @@ require 'pp' if $DEBUG
 config = ARGV[0] || "config.ru"
 abort "configuration file #{config} not found" unless File.exist?(config)
 
-if config =~ /\.ru$/
-  cfgfile = File.read(config)
-  if cfgfile[/^#\\(.*)/]
+inner_app = case config
+when /\.ru$/
+  raw = File.open(config, "rb") { |fp| fp.sysread(fp.stat.size) }
+  if raw[/^#\\(.*)/]
     warn %(not parsing embedded command-line options: "#$1")
   end
-  inner_app = eval "Rack::Builder.new {(#{cfgfile}\n)}.to_app", nil, config
+  lambda { || eval("Rack::Builder.new {(#{raw}\n)}.to_app", nil, config) }
 else
-  require config
-  inner_app = Object.const_get(File.basename(config, '.rb').capitalize)
+  lambda do ||
+    require config
+    Object.const_get(File.basename(config, '.rb').capitalize)
+  end
 end
 
 app = case env
 when "development"
-  Rack::Builder.new do
-    use Rack::CommonLogger, STDERR
-    use Rack::ShowExceptions
-    use Rack::Lint
-    run inner_app
-  end.to_app
+  lambda do ||
+    Rack::Builder.new do
+      use Rack::CommonLogger, STDERR
+      use Rack::ShowExceptions
+      use Rack::Lint
+      run inner_app.call
+    end.to_app
+  end
 when "deployment"
-  Rack::Builder.new do
-    use Rack::CommonLogger, STDERR
-    run inner_app
-  end.to_app
+  lambda do ||
+    Rack::Builder.new do
+      use Rack::CommonLogger, STDERR
+      run inner_app.call
+    end.to_app
+  end
 else
   inner_app
 end
diff --git a/lib/unicorn.rb b/lib/unicorn.rb
index aad4c3d..fa2ebe8 100644
--- a/lib/unicorn.rb
+++ b/lib/unicorn.rb
@@ -86,6 +86,7 @@ module Unicorn
       config_listeners.each { |addr| listen(addr) }
       listen(Const::DEFAULT_LISTENER) if @listeners.empty?
       self.pid = @config[:pid]
+      build_app!
       spawn_missing_workers
       self
     end
@@ -529,5 +530,9 @@ module Unicorn
       listeners.map { |io| sock_name(io) }
     end
 
+    def build_app!
+      @app = @app.call if @app.respond_to?(:arity) && @app.arity == 0
+    end
+
   end
 end