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}"
111 if ::Rails::VERSION::MAJOR >= 3 && ::File.exist?('config/application.rb')
112 if ::File.read('config/application.rb') =~ /^module\s+([\w:]+)\s*$/
113 app_module = Object.const_get($1)
115 result = app_module::Application
121 if result.nil? && defined?(ActionController::Dispatcher)
122 result = ActionController::Dispatcher.new
125 result || abort("Unable to locate the application dispatcher class")
128 def rails_builder(ru, opts, daemonize)
129 return Unicorn.builder(ru, opts) if ru
131 # allow Configurator to parse cli switches embedded in the ru file
132 Unicorn::Configurator::RACKUP.update(:file => :rails, :optparse => opts)
134 # this lambda won't run until after forking if preload_app is false
135 # this runs after config file reloading
137 # Rails 3 includes a config.ru, use it if we find it after
138 # working_directory is bound.
139 ::File.exist?('config.ru') and
140 return Unicorn.builder('config.ru', opts).call
142 # Load Rails and (possibly) the private version of Rack it bundles.
144 require ::File.expand_path('config/boot')
145 require ::File.expand_path('config/environment')
146 rescue LoadError => err
147 abort "#$0 must be run inside RAILS_ROOT: #{err.inspect}"
150 defined?(::Rails::VERSION::STRING) or
151 abort "Rails::VERSION::STRING not defined by config/{boot,environment}"
152 # it seems Rails >=2.2 support Rack, but only >=2.3 requires it
153 old_rails = case ::Rails::VERSION::MAJOR
155 when 2 then Rails::VERSION::MINOR < 3 ? true : false
161 map_path = ENV['RAILS_RELATIVE_URL_ROOT'] || '/'
164 # patches + tests welcome, but I really cbf to deal with this
165 # since all apps I've ever dealt with just use "/" ...
166 warn "relative URL roots may not work for older Rails"
168 warn "LogTailer not available for Rails < 2.3" unless daemonize
169 warn "Debugger not available" if $DEBUG
170 require 'unicorn/app/old_rails'
172 use Unicorn::App::OldRails::Static
173 run Unicorn::App::OldRails.new
176 use Rails::Rack::LogTailer unless daemonize
177 use Rails::Rack::Debugger if $DEBUG
179 unless defined?(ActionDispatch::Static)
180 use Rails::Rack::Static
189 app = rails_builder(ARGV[0], opts, daemonize)
190 options[:listeners] << "#{host}:#{port}" if set_listener
195 :unicorn_options => options,
197 :daemonize => daemonize,
201 # ensure Rails standard tmp paths exist
202 options[:after_reload] = lambda do
203 FileUtils.mkdir_p(%w(cache pids sessions sockets).map! { |d| "tmp/#{d}" })
207 options[:pid] = "tmp/pids/unicorn.pid"
208 Unicorn::Launcher.daemonize!(options)
210 Unicorn.run(app, options)