Fix unicorn_rails compatibility with the latest Rails 3 code
[unicorn.git] / bin / unicorn_rails
blobed235ba39ef2100966e0327c0eaff9e977f514e8
1 #!/this/will/be/overwritten/or/wrapped/anyways/do/not/worry/ruby
2 # -*- encoding: binary -*-
3 require 'unicorn/launcher'
4 require 'optparse'
5 require 'fileutils'
7 daemonize = false
8 options = { :listeners => [] }
9 host, port = Unicorn::Const::DEFAULT_HOST, Unicorn::Const::DEFAULT_PORT
10 set_listener = false
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:"
19 lineno = 1
20 opts.on("-e", "--eval LINE", "evaluate a LINE of code") do |line|
21 eval line, TOPLEVEL_BINDING, "-e", lineno
22 lineno += 1
23 end
25 opts.on("-d", "--debug", "set debugging flags (set $DEBUG to true)") do
26 $DEBUG = true
27 end
29 opts.on("-w", "--warn", "turn warnings on for your script") do
30 $-w = true
31 end
33 opts.on("-I", "--include PATH",
34 "specify $LOAD_PATH (may be used more than once)") do |path|
35 $LOAD_PATH.unshift(*path.split(/:/))
36 end
38 opts.on("-r", "--require LIBRARY",
39 "require the library, before executing your script") do |library|
40 require library
41 end
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|
49 host = h
50 set_listener = true
51 end
53 opts.on("-p", "--port PORT", "use PORT (default: #{port})") do |p|
54 port = p.to_i
55 set_listener = true
56 end
58 opts.on("-E", "--env RAILS_ENV",
59 "use RAILS_ENV for defaults (default: development)") do |e|
60 ENV['RAILS_ENV'] = e
61 end
63 opts.on("-D", "--daemonize", "run daemonized in the background") do |d|
64 daemonize = d ? true : false
65 end
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
73 end
75 opts.on("-c", "--config-file FILE", "Unicorn-specific config file") do |f|
76 options[:config_file] = f
77 end
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
83 end
85 opts.on("--path PATH", "Runs Rails app mounted at a specific path.",
86 "(default: /)") do |v|
87 ENV['RAILS_RELATIVE_URL_ROOT'] = v
88 end
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, '')
99 exit
102 opts.on_tail("-v", "--version", "Show version") do
103 puts " v#{Unicorn::Const::UNICORN_VERSION}"
104 exit
107 opts.parse! ARGV
110 ru = ARGV[0] || (File.exist?('config.ru') ? 'config.ru' : nil)
112 def rails_dispatcher
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)
116 begin
117 result = app_module::Application
118 rescue NameError
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
132 lambda do ||
133 # Load Rails and (possibly) the private version of Rack it bundles.
134 begin
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
145 when 0, 1 then true
146 when 2 then Rails::VERSION::MINOR < 3 ? true : false
147 else
148 false
151 Rack::Builder.new do
152 map_path = ENV['RAILS_RELATIVE_URL_ROOT'] || '/'
153 if old_rails
154 if map_path != '/'
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'
162 map(map_path) do
163 use Unicorn::App::OldRails::Static
164 run Unicorn::App::OldRails.new
166 else
167 use Rails::Rack::LogTailer unless daemonize
168 use Rails::Rack::Debugger if $DEBUG
169 map(map_path) do
170 if defined?(ActionDispatch::Static)
171 use ActionDispatch::Static, "#{Rails.root}/public"
172 else
173 use Rails::Rack::Static
175 run rails_dispatcher
178 end.to_app
182 app = ru ? Unicorn.builder(ru, opts) : rails_builder(daemonize)
183 options[:listeners] << "#{host}:#{port}" if set_listener
185 if $DEBUG
186 require 'pp'
187 pp({
188 :unicorn_options => options,
189 :app => app,
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}" })
199 if daemonize
200 options[:pid] = "tmp/pids/unicorn.pid"
201 Unicorn::Launcher.daemonize!(options)
203 Unicorn.run(app, options)