cleanup some log messages
[unicorn.git] / bin / unicorn_rails
blobb3c3631e865d7256e6ad38b038c5cbbf4c6aadc9
1 #!/home/ew/bin/ruby
2 require 'unicorn/launcher'
3 require 'optparse'
4 require 'fileutils'
6 rails_pid = File.join(Unicorn::HttpServer::DEFAULT_START_CTX[:cwd],
7 "/tmp/pids/unicorn.pid")
8 cmd = File.basename($0)
9 daemonize = false
10 listeners = []
11 options = { :listeners => listeners }
12 host, port = Unicorn::Const::DEFAULT_HOST, Unicorn::Const::DEFAULT_PORT
13 set_listener = false
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:"
22 lineno = 1
23 opts.on("-e", "--eval LINE", "evaluate a LINE of code") do |line|
24 eval line, TOPLEVEL_BINDING, "-e", lineno
25 lineno += 1
26 end
28 opts.on("-d", "--debug", "set debugging flags (set $DEBUG to true)") do
29 $DEBUG = true
30 end
32 opts.on("-w", "--warn", "turn warnings on for your script") do
33 $-w = true
34 end
36 opts.on("-I", "--include PATH",
37 "specify $LOAD_PATH (may be used more than once)") do |path|
38 $LOAD_PATH.unshift(*path.split(/:/))
39 end
41 opts.on("-r", "--require LIBRARY",
42 "require the library, before executing your script") do |library|
43 require library
44 end
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|
52 host = h
53 set_listener = true
54 end
56 opts.on("-p", "--port PORT", "use PORT (default: #{port})") do |p|
57 port = p.to_i
58 set_listener = true
59 end
61 opts.on("-E", "--env ENVIRONMENT",
62 "use ENVIRONMENT for defaults (default: development)") do |e|
63 ENV['RAILS_ENV'] = e
64 end
66 opts.on("-D", "--daemonize", "run daemonized in the background") do |d|
67 daemonize = d ? true : false
68 end
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|
75 listeners << address
76 end
78 opts.on("-c", "--config-file FILE", "Unicorn-specific config file") do |f|
79 options[:config_file] = File.expand_path(f)
80 end
82 opts.on("-P", "--path PATH", "Runs Rails app mounted at a specific path.",
83 "(default: /") do |v|
84 ENV['RAILS_RELATIVE_URL_ROOT'] = map_path = v
85 end
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
95 puts opts
96 exit
97 end
99 opts.on_tail("-v", "--version", "Show version") do
100 puts " v#{Unicorn::Const::UNICORN_VERSION}"
101 exit
104 opts.parse! ARGV
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 ||
122 begin
123 require 'config/boot'
124 rescue LoadError => err
125 abort "#$0 must be run inside RAILS_ROOT: #{err.inspect}"
127 defined?(::RAILS_ROOT) or abort "RAILS_ROOT not defined by config/boot"
128 defined?(::RAILS_ENV) or abort "RAILS_ENV not defined by config/boot"
129 defined?(::Rails::VERSION::STRING) or
130 abort "Rails::VERSION::STRING not defined by config/boot"
132 case config
133 when nil
134 require 'config/environment'
136 # it seems Rails >=2.2 support Rack, but only >=2.3 requires it
137 old_rails = case ::Rails::VERSION::MAJOR
138 when 0, 1 then true
139 when 2 then Rails::VERSION::MINOR < 3 ? true : false
140 else
141 false
144 if old_rails
145 require 'rack'
146 require 'unicorn/app/old_rails'
147 Unicorn::App::OldRails.new
148 else
149 ActionController::Dispatcher.new
151 when /\.ru$/
152 raw = File.open(config, "rb") { |fp| fp.sysread(fp.stat.size) }
153 eval("Rack::Builder.new {(#{raw}\n)}.to_app", nil, config)
154 else
155 require config
156 Object.const_get(File.basename(config, '.rb').capitalize)
160 # this won't run until after forking if preload_app is false
161 app = lambda do ||
162 map_path ||= '/'
163 inner_app = rails_loader.call
164 Rack::Builder.new do
165 if inner_app.class.to_s == "Unicorn::App::OldRails"
166 if map_path != '/'
167 # patches + tests welcome, but I really cbf to deal with this
168 # since all apps I've ever dealt with just use "/" ...
169 $stderr.puts "relative URL roots may not work for older Rails"
171 $stderr.puts "LogTailer not available for Rails < 2.3" unless daemonize
172 $stderr.puts "Debugger not available" if $DEBUG
173 map(map_path) do
174 require 'unicorn/app/old_rails/static'
175 use Unicorn::App::OldRails::Static
176 run inner_app
178 else
179 use Rails::Rack::LogTailer unless daemonize
180 use Rails::Rack::Debugger if $DEBUG
181 map(map_path) do
182 use Rails::Rack::Static
183 run inner_app
186 end.to_app
189 listeners << "#{host}:#{port}" if set_listener
191 if $DEBUG
192 pp({
193 :unicorn_options => options,
194 :app => app,
195 :daemonize => daemonize,
199 # ensure Rails standard tmp paths exist
200 %w(cache pids sessions sockets).each do |dir|
201 FileUtils.mkdir_p("tmp/#{dir}")
204 if daemonize
205 options[:pid] = rails_pid
206 Unicorn::Launcher.daemonize!
208 Unicorn.run(app, options)