1 #!/this/will/be/overwritten/or/wrapped/anyways/do/not/worry/ruby
2 # -*- encoding: binary -*-
3 require 'unicorn/launcher'
8 options
= { :listeners => [] }
9 host
, port
= Unicorn
::Const::DEFAULT_HOST, Unicorn
::Const::DEFAULT_PORT
11 ENV['RAILS_ENV'] ||= "development"
13 opts
= OptionParser
.new("", 24, ' ') do |opts
|
14 cmd
= File
.basename($0)
15 opts
.banner
= "Usage: #{cmd} " \
16 "[ruby options] [#{cmd} options] [rackup config file]"
17 opts
.separator
"Ruby options:"
20 opts
.on("-e", "--eval LINE", "evaluate a LINE of code") do |line
|
21 eval line
, TOPLEVEL_BINDING
, "-e", lineno
25 opts
.on("-d", "--debug", "set debugging flags (set $DEBUG to true)") do
29 opts
.on("-w", "--warn", "turn warnings on for your script") do
33 opts
.on("-I", "--include PATH",
34 "specify $LOAD_PATH (may be used more than once)") do |path
|
35 $LOAD_PATH.unshift(*path
.split(/:/))
38 opts
.on("-r", "--require LIBRARY",
39 "require the library, before executing your script") do |library
|
43 opts
.separator
"#{cmd} options:"
45 # some of these switches exist for rackup command-line compatibility,
47 opts
.on("-o", "--host HOST",
48 "listen on HOST (default: #{Unicorn::Const::DEFAULT_HOST})") do |h
|
53 opts
.on("-p", "--port PORT", "use PORT (default: #{port})") do |p
|
58 opts
.on("-E", "--env RAILS_ENV",
59 "use RAILS_ENV for defaults (default: development)") do |e
|
63 opts
.on("-D", "--daemonize", "run daemonized in the background") do |d
|
64 daemonize
= d
? true : false
67 # Unicorn-specific stuff
68 opts
.on("-l", "--listen {HOST:PORT|PATH}",
69 "listen on HOST:PORT or PATH",
70 "this may be specified multiple times",
71 "(default: #{Unicorn::Const::DEFAULT_LISTEN})") do |address
|
72 options
[:listeners] << address
75 opts
.on("-c", "--config-file FILE", "Unicorn-specific config file") do |f
|
76 options
[:config_file] = f
79 opts
.on("-P PATH", "DEPRECATED") do |v
|
80 warn
%q{Use of -P is ambiguous and discouraged}
81 warn %q{Use --path or RAILS_RELATIVE_URL_ROOT instead}
82 ENV['RAILS_RELATIVE_URL_ROOT'] = v
85 opts.on("--path PATH", "Runs Rails app mounted at a specific path.",
86 "(default: /)") do |v|
87 ENV['RAILS_RELATIVE_URL_ROOT'] = v
90 # I'm avoiding Unicorn-specific config options on the command-line.
91 # IMNSHO, config options on the command-line are redundant given
92 # config files and make things unnecessarily complicated with multiple
93 # places to look for a config option.
95 opts.separator "Common options:"
97 opts.on_tail("-h", "--help", "Show this message") do
98 puts opts.to_s.gsub(/^.*DEPRECATED.*$/s, '')
102 opts.on_tail("-v", "--version", "Show version") do
103 puts " v#{Unicorn::Const::UNICORN_VERSION}"
110 ru = ARGV[0] || (File.exist?('config.ru') ? 'config.ru' : nil)
113 if ::Rails::VERSION::MAJOR >= 3 && ::File.exist?('config/application.rb')
114 if ::File.read('config/application.rb') =~ /^module\s+([\w:]+)\s*$/
115 app_module = Object.const_get($1)
117 result = app_module::Application
123 if result.nil? && defined?(ActionController::Dispatcher)
124 result = ActionController::Dispatcher.new
127 result || abort("Unable to locate the application dispatcher class")
130 def rails_builder(daemonize)
131 # this lambda won't run until after forking if preload_app is false
133 # Load Rails and (possibly) the private version of Rack it bundles.
135 require 'config/boot'
136 require 'config/environment'
137 rescue LoadError => err
138 abort "#$0 must be run inside RAILS_ROOT: #{err.inspect}"
141 defined?(::Rails::VERSION::STRING) or
142 abort "Rails::VERSION::STRING not defined by config/{boot,environment}"
143 # it seems Rails >=2.2 support Rack, but only >=2.3 requires it
144 old_rails = case ::Rails::VERSION::MAJOR
146 when 2 then Rails::VERSION::MINOR < 3 ? true : false
152 map_path = ENV['RAILS_RELATIVE_URL_ROOT'] || '/'
155 # patches + tests welcome, but I really cbf to deal with this
156 # since all apps I've ever dealt with just use "/" ...
157 $stderr.puts "relative URL roots may not work for older Rails"
159 $stderr.puts "LogTailer not available for Rails < 2.3" unless daemonize
160 $stderr.puts "Debugger not available" if $DEBUG
161 require 'unicorn/app/old_rails'
163 use Unicorn::App::OldRails::Static
164 run Unicorn::App::OldRails.new
167 use Rails::Rack::LogTailer unless daemonize
168 use Rails::Rack::Debugger if $DEBUG
170 if defined?(ActionDispatch::Static)
171 use ActionDispatch::Static, "#{Rails.root}/public"
173 use Rails::Rack::Static
182 app = ru ? Unicorn.builder(ru, opts) : rails_builder(daemonize)
183 options[:listeners] << "#{host}:#{port}" if set_listener
188 :unicorn_options => options,
190 :daemonize => daemonize,
194 # ensure Rails standard tmp paths exist
195 options[:after_reload] = lambda do
196 FileUtils.mkdir_p(%w(cache pids sessions sockets).map! { |d| "tmp/#{d}" })
200 options[:pid] = "tmp/pids/unicorn.pid"
201 Unicorn::Launcher.daemonize!(options)
203 Unicorn.run(app, options)