1 #!/this/will/be/overwritten/or/wrapped/anyways/do/not/worry/ruby
2 # -*- encoding: binary -*-
3 require 'unicorn/launcher'
7 ENV['RAILS_ENV'] ||= "development"
8 rackup_opts
= Unicorn
::Configurator::RACKUP
9 options
= rackup_opts
[:options]
11 op
= OptionParser
.new("", 24, ' ') do |opts
|
12 cmd
= File
.basename($0)
13 opts
.banner
= "Usage: #{cmd} " \
14 "[ruby options] [#{cmd} options] [rackup config file]"
15 opts
.separator
"Ruby options:"
18 opts
.on("-e", "--eval LINE", "evaluate a LINE of code") do |line
|
19 eval line
, TOPLEVEL_BINDING
, "-e", lineno
23 opts
.on("-d", "--debug", "set debugging flags (set $DEBUG to true)") do
27 opts
.on("-w", "--warn", "turn warnings on for your script") do
31 opts
.on("-I", "--include PATH",
32 "specify $LOAD_PATH (may be used more than once)") do |path
|
33 $LOAD_PATH.unshift(*path
.split(':'))
36 opts
.on("-r", "--require LIBRARY",
37 "require the library, before executing your script") do |library
|
41 opts
.separator
"#{cmd} options:"
43 # some of these switches exist for rackup command-line compatibility,
45 opts
.on("-o", "--host HOST",
46 "listen on HOST (default: #{Unicorn::Const::DEFAULT_HOST})") do |h
|
47 rackup_opts
[:host] = h
48 rackup_opts
[:set_listener] = true
51 opts
.on("-p", "--port PORT", Integer
,
52 "use PORT (default: #{Unicorn::Const::DEFAULT_PORT})") do |port
|
53 rackup_opts
[:port] = port
54 rackup_opts
[:set_listener] = true
57 opts
.on("-E", "--env RAILS_ENV",
58 "use RAILS_ENV for defaults (default: development)") do |e
|
62 opts
.on("-D", "--daemonize", "run daemonized in the background") do |d
|
63 rackup_opts
[:daemonize] = !!d
66 # Unicorn-specific stuff
67 opts
.on("-l", "--listen {HOST:PORT|PATH}",
68 "listen on HOST:PORT or PATH",
69 "this may be specified multiple times",
70 "(default: #{Unicorn::Const::DEFAULT_LISTEN})") do |address
|
71 options
[:listeners] << address
74 opts
.on("-c", "--config-file FILE", "Unicorn-specific config file") do |f
|
75 options
[:config_file] = f
78 opts
.on("-P PATH", "DEPRECATED") do |v
|
79 warn
%q{Use of -P is ambiguous and discouraged}
80 warn %q{Use --path or RAILS_RELATIVE_URL_ROOT instead}
81 ENV['RAILS_RELATIVE_URL_ROOT'] = v
84 opts.on("--path PATH", "Runs Rails app mounted at a specific path.",
85 "(default: /)") do |v|
86 ENV['RAILS_RELATIVE_URL_ROOT'] = v
89 # I'm avoiding Unicorn-specific config options on the command-line.
90 # IMNSHO, config options on the command-line are redundant given
91 # config files and make things unnecessarily complicated with multiple
92 # places to look for a config option.
94 opts.separator "Common options:"
96 opts.on_tail("-h", "--help", "Show this message") do
97 puts opts.to_s.gsub(/^.*DEPRECATED.*$/s, '')
101 opts.on_tail("-v", "--version", "Show version") do
102 puts "#{cmd} v#{Unicorn::Const::UNICORN_VERSION}"
110 if ::Rails::VERSION::MAJOR >= 3 && ::File.exist?('config/application.rb')
111 if ::File.read('config/application.rb') =~ /^module\s+([\w:]+)\s*$/
112 app_module = Object.const_get($1)
114 result = app_module::Application
120 if result.nil? && defined?(ActionController::Dispatcher)
121 result = ActionController::Dispatcher.new
124 result || abort("Unable to locate the application dispatcher class")
127 def rails_builder(ru, op, daemonize)
128 return Unicorn.builder(ru, op) if ru
130 # allow Configurator to parse cli switches embedded in the ru file
131 Unicorn::Configurator::RACKUP.update(:file => :rails, :optparse => op)
133 # this lambda won't run until after forking if preload_app is false
134 # this runs after config file reloading
136 # Rails 3 includes a config.ru, use it if we find it after
137 # working_directory is bound.
138 ::File.exist?('config.ru') and
139 return Unicorn.builder('config.ru', op).call
141 # Load Rails and (possibly) the private version of Rack it bundles.
143 require ::File.expand_path('config/boot')
144 require ::File.expand_path('config/environment')
145 rescue LoadError => err
146 abort "#$0 must be run inside RAILS_ROOT: #{err.inspect}"
149 defined?(::Rails::VERSION::STRING) or
150 abort "Rails::VERSION::STRING not defined by config/{boot,environment}"
151 # it seems Rails >=2.2 support Rack, but only >=2.3 requires it
152 old_rails = case ::Rails::VERSION::MAJOR
154 when 2 then Rails::VERSION::MINOR < 3 ? true : false
160 map_path = ENV['RAILS_RELATIVE_URL_ROOT'] || '/'
163 # patches + tests welcome, but I really cbf to deal with this
164 # since all apps I've ever dealt with just use "/" ...
165 warn "relative URL roots may not work for older Rails"
167 warn "LogTailer not available for Rails < 2.3" unless daemonize
168 warn "Debugger not available" if $DEBUG
169 require 'unicorn/app/old_rails'
171 use Unicorn::App::OldRails::Static
172 run Unicorn::App::OldRails.new
175 use Rails::Rack::LogTailer unless daemonize
176 use Rails::Rack::Debugger if $DEBUG
178 unless defined?(ActionDispatch::Static)
179 use Rails::Rack::Static
188 app = rails_builder(ARGV[0], op, rackup_opts[:daemonize])
194 :unicorn_options => options,
196 :daemonize => rackup_opts[:daemonize],
200 # ensure Rails standard tmp paths exist
201 options[:after_reload] = lambda do
202 FileUtils.mkdir_p(%w(cache pids sessions sockets).map! { |d| "tmp/#{d}" })
205 if rackup_opts[:daemonize]
206 options[:pid] = "tmp/pids/unicorn.pid"
207 Unicorn::Launcher.daemonize!(options)
209 Unicorn::HttpServer.new(app, options).start.join