2 require 'unicorn/launcher'
6 rails_pid
= File
.join(Unicorn
::HttpServer::DEFAULT_START_CTX[:cwd],
7 "/tmp/pids/unicorn.pid")
8 cmd
= File
.basename($0)
11 options
= { :listeners => listeners
}
12 host
, port
= Unicorn
::Const::DEFAULT_HOST, Unicorn
::Const::DEFAULT_PORT
14 ENV['RAILS_ENV'] ||= "development"
15 map_path
= ENV['RAILS_RELATIVE_URL_ROOT']
17 opts
= OptionParser
.new("", 24, ' ') do |opts
|
18 opts
.banner
= "Usage: #{cmd} " \
19 "[ruby options] [#{cmd} options] [rackup config file]"
20 opts
.separator
"Ruby options:"
23 opts
.on("-e", "--eval LINE", "evaluate a LINE of code") do |line
|
24 eval line
, TOPLEVEL_BINDING
, "-e", lineno
28 opts
.on("-d", "--debug", "set debugging flags (set $DEBUG to true)") do
32 opts
.on("-w", "--warn", "turn warnings on for your script") do
36 opts
.on("-I", "--include PATH",
37 "specify $LOAD_PATH (may be used more than once)") do |path
|
38 $LOAD_PATH.unshift(*path
.split(/:/))
41 opts
.on("-r", "--require LIBRARY",
42 "require the library, before executing your script") do |library
|
46 opts
.separator
"#{cmd} options:"
48 # some of these switches exist for rackup command-line compatibility,
50 opts
.on("-o", "--host HOST",
51 "listen on HOST (default: #{Unicorn::Const::DEFAULT_HOST})") do |h
|
56 opts
.on("-p", "--port PORT", "use PORT (default: #{port})") do |p
|
61 opts
.on("-E", "--env ENVIRONMENT",
62 "use ENVIRONMENT for defaults (default: development)") do |e
|
66 opts
.on("-D", "--daemonize", "run daemonized in the background") do |d
|
67 daemonize
= d
? true : false
70 # Unicorn-specific stuff
71 opts
.on("-l", "--listen {HOST:PORT|PATH}",
72 "listen on HOST:PORT or PATH",
73 "this may be specified multiple times",
74 "(default: #{Unicorn::Const::DEFAULT_LISTEN})") do |address
|
78 opts
.on("-c", "--config-file FILE", "Unicorn-specific config file") do |f
|
79 options
[:config_file] = File
.expand_path(f
)
82 opts
.on("-P", "--path PATH", "Runs Rails app mounted at a specific path.",
84 ENV['RAILS_RELATIVE_URL_ROOT'] = map_path
= v
87 # I'm avoiding Unicorn-specific config options on the command-line.
88 # IMNSHO, config options on the command-line are redundant given
89 # config files and make things unnecessarily complicated with multiple
90 # places to look for a config option.
92 opts
.separator
"Common options:"
94 opts
.on_tail("-h", "--help", "Show this message") do
99 opts
.on_tail("-v", "--version", "Show version") do
100 puts
" v#{Unicorn::Const::UNICORN_VERSION}"
107 config
= ARGV[0] || (File
.exist
?('config.ru') ? 'config.ru' : nil)
109 if config
&& config
=~
/\.ru$/
110 # parse embedded command-line options in config.ru comments
111 if File
.open(config
, "rb") { |fp
| fp
.sysread(fp
.stat
.size
) } =~
/^#\\(.*)/
112 opts
.parse
! $1.split(/\s+/)
116 require 'pp' if $DEBUG
118 # Loads Rails and the private version of Rack it bundles. Returns a
119 # lambda of arity==0 that will return *another* lambda of arity==1
120 # suitable for using inside Rack::Builder.new block.
121 rails_loader
= lambda
do ||
123 require 'config/boot'
124 defined?(::RAILS_ROOT) or abort
"RAILS_ROOT not defined by config/boot"
125 defined?(::RAILS_ENV) or abort
"RAILS_ENV not defined by config/boot"
126 defined?(::Rails::VERSION::STRING) or
127 abort
"Rails::VERSION::STRING not defined by config/boot"
128 rescue LoadError
=> err
129 abort
"#$0 must be run inside RAILS_ROOT: #{err.inspect}"
131 defined?(::RAILS_ROOT) or abort
"RAILS_ROOT not defined by config/boot"
132 defined?(::RAILS_ENV) or abort
"RAILS_ENV not defined by config/boot"
133 defined?(::Rails::VERSION::STRING) or
134 abort
"Rails::VERSION::STRING not defined by config/boot"
139 require 'config/environment'
141 # it seems Rails >=2.2 support Rack, but only >=2.3 requires it
142 old_rails
= case ::Rails::VERSION::MAJOR
144 when 2 then Rails
::VERSION::MINOR < 3 ? true : false
151 require 'unicorn/app/old_rails'
152 Unicorn
::App::OldRails.new
154 ActionController
::Dispatcher.new
158 raw
= File
.open(config
, "rb") { |fp
| fp
.sysread(fp
.stat
.size
) }
159 lambda
{ || eval("Rack::Builder.new {(#{raw}\n)}.to_app", nil, config
) }
163 Object
.const_get(File
.basename(config
, '.rb').capitalize
)
168 # this won't run until after forking if preload_app is false
170 inner_app
= rails_loader
.call
171 require 'active_support'
172 require 'action_controller'
174 inner_app
= inner_app
.call
176 if inner_app
.class.to_s
== "Unicorn::App::OldRails"
178 # patches + tests welcome, but I really cbf to deal with this
179 # since all apps I've ever dealt with just use "/" ...
180 $stderr.puts
"relative URL roots may not work for older Rails"
182 $stderr.puts
"LogTailer not available for Rails < 2.3" unless daemonize
183 $stderr.puts
"Debugger not available" if $DEBUG
185 require 'unicorn/app/old_rails/static'
186 use Unicorn
::App::OldRails::Static
190 use Rails
::Rack::LogTailer unless daemonize
191 use Rails
::Rack::Debugger if $DEBUG
193 use Rails
::Rack::Static
200 listeners
<< "#{host}:#{port}" if set_listener
204 :unicorn_options => options
,
206 :daemonize => daemonize
,
210 # ensure Rails standard tmp paths exist
211 %w(cache pids sessions sockets
).each
do |dir
|
212 FileUtils
.mkdir_p("tmp/#{dir}")
216 options
[:pid] = rails_pid
217 Unicorn
::Launcher.daemonize
!
219 Unicorn
.run(app
, options
)