tee_input: safer record separator ($/) handling
[unicorn.git] / bin / unicorn_rails
blob3614fd4037a7e5802af632ababd906d3add736f7
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 def rails_dispatcher
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)
114 begin
115 result = app_module::Application
116 rescue NameError
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
136 lambda do ||
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.
143 begin
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
154 when 0, 1 then true
155 when 2 then Rails::VERSION::MINOR < 3 ? true : false
156 else
157 false
160 Rack::Builder.new do
161 map_path = ENV['RAILS_RELATIVE_URL_ROOT'] || '/'
162 if old_rails
163 if map_path != '/'
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'
171 map(map_path) do
172 use Unicorn::App::OldRails::Static
173 run Unicorn::App::OldRails.new
175 else
176 use Rails::Rack::LogTailer unless daemonize
177 use Rails::Rack::Debugger if $DEBUG
178 map(map_path) do
179 unless defined?(ActionDispatch::Static)
180 use Rails::Rack::Static
182 run rails_dispatcher
185 end.to_app
189 app = rails_builder(ARGV[0], opts, daemonize)
190 options[:listeners] << "#{host}:#{port}" if set_listener
192 if $DEBUG
193 require 'pp'
194 pp({
195 :unicorn_options => options,
196 :app => app,
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}" })
206 if daemonize
207 options[:pid] = "tmp/pids/unicorn.pid"
208 Unicorn::Launcher.daemonize!(options)
210 Unicorn.run(app, options)