diff options
Diffstat (limited to 'bin/unicorn')
-rwxr-xr-x | bin/unicorn | 87 |
1 files changed, 30 insertions, 57 deletions
diff --git a/bin/unicorn b/bin/unicorn index ebf57c3..a34d9bc 100755 --- a/bin/unicorn +++ b/bin/unicorn @@ -1,14 +1,13 @@ #!/home/ew/bin/ruby -$stdin.sync = $stdout.sync = $stderr.sync = true -require 'unicorn' # require this first to populate Unicorn::DEFAULT_START_CTX +require 'unicorn/launcher' require 'optparse' env = "development" daemonize = false listeners = [] options = { :listeners => listeners } -host = Unicorn::Const::DEFAULT_HOST -port = Unicorn::Const::DEFAULT_PORT +host, port = Unicorn::Const::DEFAULT_HOST, Unicorn::Const::DEFAULT_PORT +set_listener = false opts = OptionParser.new("", 24, ' ') do |opts| opts.banner = "Usage: #{File.basename($0)} " \ @@ -47,11 +46,13 @@ opts = OptionParser.new("", 24, ' ') do |opts| opts.on("-o", "--host HOST", "listen on HOST (default: #{Unicorn::Const::DEFAULT_HOST})") do |h| host = h + set_listener = true end opts.on("-p", "--port PORT", "use PORT (default: #{Unicorn::Const::DEFAULT_PORT})") do |p| port = p.to_i + set_listener = true end opts.on("-E", "--env ENVIRONMENT", @@ -104,86 +105,58 @@ opts = OptionParser.new("", 24, ' ') do |opts| opts.parse! ARGV end -require 'pp' if $DEBUG - -# require Rack as late as possible in case $LOAD_PATH is modified -# in config.ru or command-line -require 'rack' - config = ARGV[0] || "config.ru" abort "configuration file #{config} not found" unless File.exist?(config) -inner_app = case config -when /\.ru$/ - raw = File.open(config, "rb") { |fp| fp.sysread(fp.stat.size) } +if config =~ /\.ru$/ # parse embedded command-line options in config.ru comments - if raw[/^#\\(.*)/] + if File.open(config, "rb") { |fp| fp.sysread(fp.stat.size) } =~ /^#\\(.*)/ opts.parse! $1.split(/\s+/) end - lambda { || eval("Rack::Builder.new {(#{raw}\n)}.to_app", nil, config) } -else - lambda do || +end + +require 'pp' if $DEBUG + +app = lambda do || + # require Rack as late as possible in case $LOAD_PATH is modified + # in config.ru or command-line + require 'rack' + inner_app = case config + when /\.ru$/ + raw = File.open(config, "rb") { |fp| fp.sysread(fp.stat.size) } + eval("Rack::Builder.new {(#{raw}\n)}.to_app", nil, config) + else require config Object.const_get(File.basename(config, '.rb').capitalize) end -end - -app = case env -when "development" - lambda do || + pp({ :inner_app => inner_app }) if $DEBUG + case env + when "development" Rack::Builder.new do use Rack::CommonLogger, $stderr use Rack::ShowExceptions use Rack::Lint - run inner_app.call + run inner_app end.to_app - end -when "deployment" - lambda do || + when "deployment" Rack::Builder.new do use Rack::CommonLogger, $stderr - run inner_app.call + run inner_app end.to_app + else + inner_app end -else - inner_app end -if listeners.empty? - listener = "#{host}:#{port}" - listeners << listener -end +listeners << "#{host}:#{port}" if set_listener if $DEBUG pp({ :unicorn_options => options, :app => app, - :inner_app => inner_app, :daemonize => daemonize, }) end -# only daemonize if we're not inheriting file descriptors from our parent -if daemonize - - $stdin.reopen("/dev/null") - unless ENV['UNICORN_FD'] - exit if fork - Process.setsid - exit if fork - end - - # We don't do a lot of standard daemonization stuff: - # * $stderr/$stderr can/will be redirected separately - # * umask is whatever was set by the parent process at startup - # and can be set in config.ru and config_file, so making it - # 0000 and potentially exposing sensitive log data can be bad - # policy. - # * Don't bother to chdir here since Unicorn is designed to - # run inside APP_ROOT. Unicorn will also re-chdir() to - # the directory it was started in when being re-executed - # to pickup code changes if the original deployment directory - # is a symlink or otherwise got replaced. -end - +Unicorn::Launcher.daemonize! if daemonize Unicorn.run(app, options) |