bin/*: remove redundant $DEBUG check
[unicorn.git] / bin / unicorn_rails
blob37ee0279a9eec6d79071c40fd7a2a8e89a1081c1
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 listeners = []
9 options = { :listeners => listeners }
10 host, port = Unicorn::Const::DEFAULT_HOST, Unicorn::Const::DEFAULT_PORT
11 set_listener = false
12 ENV['RAILS_ENV'] ||= "development"
14 opts = OptionParser.new("", 24, ' ') do |opts|
15 cmd = File.basename($0)
16 opts.banner = "Usage: #{cmd} " \
17 "[ruby options] [#{cmd} options] [rackup config file]"
18 opts.separator "Ruby options:"
20 lineno = 1
21 opts.on("-e", "--eval LINE", "evaluate a LINE of code") do |line|
22 eval line, TOPLEVEL_BINDING, "-e", lineno
23 lineno += 1
24 end
26 opts.on("-d", "--debug", "set debugging flags (set $DEBUG to true)") do
27 $DEBUG = true
28 end
30 opts.on("-w", "--warn", "turn warnings on for your script") do
31 $-w = true
32 end
34 opts.on("-I", "--include PATH",
35 "specify $LOAD_PATH (may be used more than once)") do |path|
36 $LOAD_PATH.unshift(*path.split(/:/))
37 end
39 opts.on("-r", "--require LIBRARY",
40 "require the library, before executing your script") do |library|
41 require library
42 end
44 opts.separator "#{cmd} options:"
46 # some of these switches exist for rackup command-line compatibility,
48 opts.on("-o", "--host HOST",
49 "listen on HOST (default: #{Unicorn::Const::DEFAULT_HOST})") do |h|
50 host = h
51 set_listener = true
52 end
54 opts.on("-p", "--port PORT", "use PORT (default: #{port})") do |p|
55 port = p.to_i
56 set_listener = true
57 end
59 opts.on("-E", "--env RAILS_ENV",
60 "use RAILS_ENV for defaults (default: development)") do |e|
61 ENV['RAILS_ENV'] = e
62 end
64 opts.on("-D", "--daemonize", "run daemonized in the background") do |d|
65 daemonize = d ? true : false
66 end
68 # Unicorn-specific stuff
69 opts.on("-l", "--listen {HOST:PORT|PATH}",
70 "listen on HOST:PORT or PATH",
71 "this may be specified multiple times",
72 "(default: #{Unicorn::Const::DEFAULT_LISTEN})") do |address|
73 listeners << address
74 end
76 opts.on("-c", "--config-file FILE", "Unicorn-specific config file") do |f|
77 options[:config_file] = f
78 end
80 opts.on("-P PATH", "DEPRECATED") do |v|
81 warn %q{Use of -P is ambiguous and discouraged}
82 warn %q{Use --path or RAILS_RELATIVE_URL_ROOT instead}
83 ENV['RAILS_RELATIVE_URL_ROOT'] = v
84 end
86 opts.on("--path PATH", "Runs Rails app mounted at a specific path.",
87 "(default: /)") do |v|
88 ENV['RAILS_RELATIVE_URL_ROOT'] = v
89 end
91 # I'm avoiding Unicorn-specific config options on the command-line.
92 # IMNSHO, config options on the command-line are redundant given
93 # config files and make things unnecessarily complicated with multiple
94 # places to look for a config option.
96 opts.separator "Common options:"
98 opts.on_tail("-h", "--help", "Show this message") do
99 puts opts.to_s.gsub(/^.*DEPRECATED.*$/s, '')
100 exit
103 opts.on_tail("-v", "--version", "Show version") do
104 puts " v#{Unicorn::Const::UNICORN_VERSION}"
105 exit
108 opts.parse! ARGV
111 config = ARGV[0] || (File.exist?('config.ru') ? 'config.ru' : nil)
113 if config && config =~ /\.ru\z/
114 # parse embedded command-line options in config.ru comments
115 /^#\\(.*)/ =~ File.read(config) and opts.parse!($1.split(/\s+/))
118 def rails_builder(config, daemonize)
119 # this lambda won't run until after forking if preload_app is false
120 lambda do ||
121 # Load Rails and (possibly) the private version of Rack it bundles.
122 begin
123 require 'config/boot'
124 rescue LoadError => err
125 abort "#$0 must be run inside RAILS_ROOT: #{err.inspect}"
128 inner_app = case config
129 when nil
130 require 'config/environment'
132 defined?(::Rails::VERSION::STRING) or
133 abort "Rails::VERSION::STRING not defined by config/{boot,environment}"
134 # it seems Rails >=2.2 support Rack, but only >=2.3 requires it
135 old_rails = case ::Rails::VERSION::MAJOR
136 when 0, 1 then true
137 when 2 then Rails::VERSION::MINOR < 3 ? true : false
138 else
139 false
142 if old_rails
143 require 'unicorn/app/old_rails'
144 Unicorn::App::OldRails.new
145 else
146 ActionController::Dispatcher.new
148 when /\.ru$/
149 raw = File.open(config, "rb") { |fp| fp.sysread(fp.stat.size) }
150 raw.sub!(/^__END__\n.*/, '')
151 eval("Rack::Builder.new {(#{raw}\n)}.to_app", nil, config)
152 else
153 require config
154 Object.const_get(File.basename(config, '.rb').capitalize)
157 Rack::Builder.new do
158 map_path = ENV['RAILS_RELATIVE_URL_ROOT'] || '/'
159 if inner_app.class.to_s == "Unicorn::App::OldRails"
160 if map_path != '/'
161 # patches + tests welcome, but I really cbf to deal with this
162 # since all apps I've ever dealt with just use "/" ...
163 $stderr.puts "relative URL roots may not work for older Rails"
165 $stderr.puts "LogTailer not available for Rails < 2.3" unless daemonize
166 $stderr.puts "Debugger not available" if $DEBUG
167 map(map_path) do
168 use Unicorn::App::OldRails::Static
169 run inner_app
171 else
172 use Rails::Rack::LogTailer unless daemonize
173 use Rails::Rack::Debugger if $DEBUG
174 map(map_path) do
175 use Rails::Rack::Static
176 run inner_app
179 end.to_app
183 app = rails_builder(config, daemonize)
184 listeners << "#{host}:#{port}" if set_listener
186 if $DEBUG
187 require 'pp'
188 pp({
189 :unicorn_options => options,
190 :app => app,
191 :daemonize => daemonize,
195 # ensure Rails standard tmp paths exist
196 options[:after_reload] = lambda do
197 FileUtils.mkdir_p(%w(cache pids sessions sockets).map! { |d| "tmp/#{d}" })
200 if daemonize
201 options[:pid] = "tmp/pids/unicorn.pid"
202 Unicorn::Launcher.daemonize!(options)
204 Unicorn.run(app, options)