Froze rails gems
[depot.git] / vendor / rails / railties / lib / rails / mongrel_server / commands.rb
1 # Copyright (c) 2005 Zed A. Shaw
2 # You can redistribute it and/or modify it under the same terms as Ruby.
3 #
4 # Additional work donated by contributors. See http://mongrel.rubyforge.org/attributions.html
5 # for more information.
6
7 require 'optparse'
8 require 'yaml'
9 require 'etc'
10
11 require 'mongrel'
12 require 'rails/mongrel_server/handler'
13
14 module Rails
15 module MongrelServer
16 def self.send_signal(signal, pid_file)
17 pid = open(pid_file).read.to_i
18 print "Sending #{signal} to Mongrel at PID #{pid}..."
19 begin
20 Process.kill(signal, pid)
21 rescue Errno::ESRCH
22 puts "Process does not exist. Not running."
23 end
24
25 puts "Done."
26 end
27
28 class RailsConfigurator < Mongrel::Configurator
29 def setup_mime_types
30 mime = {}
31
32 if defaults[:mime_map]
33 Mongrel.log("Loading additional MIME types from #{defaults[:mime_map]}")
34 mime = load_mime_map(defaults[:mime_map], mime)
35 end
36
37 mime.each {|k,v| Mongrel::DirHandler::add_mime_type(k,v) }
38 end
39
40 def mount_rails(prefix)
41 ENV['RAILS_ENV'] = defaults[:environment]
42 ::RAILS_ENV.replace(defaults[:environment]) if defined?(::RAILS_ENV)
43
44 env_location = "#{defaults[:cwd]}/config/environment"
45 require env_location
46
47 ActionController::Base.relative_url_root = defaults[:prefix]
48 uri prefix, :handler => Rails::MongrelServer::RailsHandler.new
49 end
50 end
51
52 class Start < GemPlugin::Plugin "/commands"
53 include Mongrel::Command::Base
54
55 def configure
56 options [
57 ["-e", "--environment ENV", "Rails environment to run as", :@environment, ENV['RAILS_ENV'] || "development"],
58 ["-d", "--daemonize", "Run daemonized in the background", :@daemon, false],
59 ['-p', '--port PORT', "Which port to bind to", :@port, 3000],
60 ['-a', '--address ADDR', "Address to bind to", :@address, "0.0.0.0"],
61 ['-l', '--log FILE', "Where to write log messages", :@log_file, "log/mongrel.log"],
62 ['-P', '--pid FILE', "Where to write the PID", :@pid_file, "tmp/pids/mongrel.pid"],
63 ['-n', '--num-procs INT', "Number of processors active before clients denied", :@num_procs, 1024],
64 ['-o', '--timeout TIME', "Time to wait (in seconds) before killing a stalled thread", :@timeout, 60],
65 ['-t', '--throttle TIME', "Time to pause (in hundredths of a second) between accepting clients", :@throttle, 0],
66 ['-m', '--mime PATH', "A YAML file that lists additional MIME types", :@mime_map, nil],
67 ['-c', '--chdir PATH', "Change to dir before starting (will be expanded)", :@cwd, RAILS_ROOT],
68 ['-r', '--root PATH', "Set the document root (default 'public')", :@docroot, "public"],
69 ['-B', '--debug', "Enable debugging mode", :@debug, false],
70 ['-C', '--config PATH', "Use a config file", :@config_file, nil],
71 ['-S', '--script PATH', "Load the given file as an extra config script", :@config_script, nil],
72 ['-G', '--generate PATH', "Generate a config file for use with -C", :@generate, nil],
73 ['', '--user USER', "User to run as", :@user, nil],
74 ['', '--group GROUP', "Group to run as", :@group, nil],
75 ['', '--prefix PATH', "URL prefix for Rails app", :@prefix, nil],
76
77 ['-b', '--binding ADDR', "Address to bind to (deprecated, use -a)", :@address, "0.0.0.0"],
78 ['-u', '--debugger', "Enable debugging mode (deprecated, use -B)", :@debug, false]
79 ]
80 end
81
82 def validate
83 if @config_file
84 valid_exists?(@config_file, "Config file not there: #@config_file")
85 return false unless @valid
86 @config_file = File.expand_path(@config_file)
87 load_config
88 return false unless @valid
89 end
90
91 @cwd = File.expand_path(@cwd)
92 valid_dir? @cwd, "Invalid path to change to during daemon mode: #@cwd"
93
94 # Change there to start, then we'll have to come back after daemonize
95 Dir.chdir(@cwd)
96
97 valid?(@prefix[0] == ?/ && @prefix[-1] != ?/, "Prefix must begin with / and not end in /") if @prefix
98 valid_dir? File.dirname(@log_file), "Path to log file not valid: #@log_file"
99 valid_dir? File.dirname(@pid_file), "Path to pid file not valid: #@pid_file"
100 valid_dir? @docroot, "Path to docroot not valid: #@docroot"
101 valid_exists? @mime_map, "MIME mapping file does not exist: #@mime_map" if @mime_map
102 valid_exists? @config_file, "Config file not there: #@config_file" if @config_file
103 valid_dir? File.dirname(@generate), "Problem accessing directory to #@generate" if @generate
104 valid_user? @user if @user
105 valid_group? @group if @group
106
107 return @valid
108 end
109
110 def run
111 if @generate
112 @generate = File.expand_path(@generate)
113 Mongrel.log(:error, "** Writing config to \"#@generate\".")
114 open(@generate, "w") {|f| f.write(settings.to_yaml) }
115 Mongrel.log(:error, "** Finished. Run \"mongrel_rails start -C #@generate\" to use the config file.")
116 exit 0
117 end
118
119 config = RailsConfigurator.new(settings) do
120 defaults[:log] = $stdout if defaults[:environment] == 'development'
121
122 Mongrel.log("=> Rails #{Rails.version} application starting on http://#{defaults[:host]}:#{defaults[:port]}")
123
124 unless defaults[:daemon]
125 Mongrel.log("=> Call with -d to detach")
126 Mongrel.log("=> Ctrl-C to shutdown server")
127 start_debugger if defaults[:debug]
128 end
129
130 if defaults[:daemon]
131 if File.exist? defaults[:pid_file]
132 Mongrel.log(:error, "!!! PID file #{defaults[:pid_file]} already exists. Mongrel could be running already. Check your #{defaults[:log_file]} for errors.")
133 Mongrel.log(:error, "!!! Exiting with error. You must stop mongrel and clear the .pid before I'll attempt a start.")
134 exit 1
135 end
136
137 daemonize
138
139 Mongrel.log("Daemonized, any open files are closed. Look at #{defaults[:pid_file]} and #{defaults[:log_file]} for info.")
140 Mongrel.log("Settings loaded from #{@config_file} (they override command line).") if @config_file
141 end
142
143 Mongrel.log("Starting Mongrel listening at #{defaults[:host]}:#{defaults[:port]}, further information can be found in log/mongrel-#{defaults[:host]}-#{defaults[:port]}.log")
144
145 listener do
146 prefix = defaults[:prefix] || '/'
147
148 if defaults[:debug]
149 Mongrel.log("Installing debugging prefixed filters. Look in log/mongrel_debug for the files.")
150 debug(prefix)
151 end
152
153 setup_mime_types
154 dir_handler = Mongrel::DirHandler.new(defaults[:docroot], false)
155 dir_handler.passthrough_missing_files = true
156
157 unless defaults[:environment] == 'production'
158 Mongrel.log("Mounting DirHandler at #{prefix}...")
159 uri prefix, :handler => dir_handler
160 end
161
162
163 Mongrel.log("Starting Rails with #{defaults[:environment]} environment...")
164 Mongrel.log("Mounting Rails at #{prefix}...")
165 mount_rails(prefix)
166 Mongrel.log("Rails loaded.")
167
168
169 Mongrel.log("Loading any Rails specific GemPlugins" )
170 load_plugins
171
172 if defaults[:config_script]
173 Mongrel.log("Loading #{defaults[:config_script]} external config script")
174 run_config(defaults[:config_script])
175 end
176
177 setup_signals
178 end
179 end
180
181 config.run
182 Mongrel.log("Mongrel #{Mongrel::Const::MONGREL_VERSION} available at #{@address}:#{@port}")
183
184 if config.defaults[:daemon]
185 config.write_pid_file
186 else
187 Mongrel.log("Use CTRL-C to stop.")
188 tail "log/#{config.defaults[:environment]}.log"
189 end
190
191 config.join
192
193 if config.needs_restart
194 unless RUBY_PLATFORM =~ /djgpp|(cyg|ms|bcc)win|mingw/
195 cmd = "ruby #{__FILE__} start #{original_args.join(' ')}"
196 Mongrel.log("Restarting with arguments: #{cmd}")
197 config.stop(false, true)
198 config.remove_pid_file
199
200 if config.defaults[:daemon]
201 system cmd
202 else
203 Mongrel.log(:error, "Can't restart unless in daemon mode.")
204 exit 1
205 end
206 else
207 Mongrel.log("Win32 does not support restarts. Exiting.")
208 end
209 end
210 end
211
212 def load_config
213 settings = {}
214 begin
215 settings = YAML.load_file(@config_file)
216 ensure
217 Mongrel.log(:error, "** Loading settings from #{@config_file} (they override command line).") unless @daemon || settings[:daemon]
218 end
219
220 settings[:includes] ||= ["mongrel"]
221
222 # Config file settings will override command line settings
223 settings.each do |key, value|
224 key = key.to_s
225 if config_keys.include?(key)
226 key = 'address' if key == 'host'
227 self.instance_variable_set("@#{key}", value)
228 else
229 failure "Unknown configuration setting: #{key}"
230 @valid = false
231 end
232 end
233 end
234
235 def config_keys
236 @config_keys ||=
237 %w(address host port cwd log_file pid_file environment docroot mime_map daemon debug includes config_script num_processors timeout throttle user group prefix)
238 end
239
240 def settings
241 config_keys.inject({}) do |hash, key|
242 value = self.instance_variable_get("@#{key}")
243 key = 'host' if key == 'address'
244 hash[key.to_sym] ||= value
245 hash
246 end
247 end
248
249 def start_debugger
250 require_library_or_gem 'ruby-debug'
251 Debugger.start
252 Debugger.settings[:autoeval] = true if Debugger.respond_to?(:settings)
253 Mongrel.log("=> Debugger enabled")
254 rescue Exception
255 Mongrel.log(:error, "You need to install ruby-debug to run the server in debugging mode. With gems, use 'gem install ruby-debug'")
256 exit
257 end
258
259 def tail(log_file)
260 cursor = File.size(log_file)
261 last_checked = Time.now
262 tail_thread = Thread.new do
263 File.open(log_file, 'r') do |f|
264 loop do
265 f.seek cursor
266 if f.mtime > last_checked
267 last_checked = f.mtime
268 contents = f.read
269 cursor += contents.length
270 print contents
271 end
272 sleep 1
273 end
274 end
275 end
276 tail_thread
277 end
278 end
279
280 class Stop < GemPlugin::Plugin "/commands"
281 include Mongrel::Command::Base
282
283 def configure
284 options [
285 ['-c', '--chdir PATH', "Change to dir before starting (will be expanded).", :@cwd, "."],
286 ['-f', '--force', "Force the shutdown (kill -9).", :@force, false],
287 ['-w', '--wait SECONDS', "Wait SECONDS before forcing shutdown", :@wait, "0"],
288 ['-P', '--pid FILE', "Where the PID file is located.", :@pid_file, "log/mongrel.pid"]
289 ]
290 end
291
292 def validate
293 @cwd = File.expand_path(@cwd)
294 valid_dir? @cwd, "Invalid path to change to during daemon mode: #@cwd"
295
296 Dir.chdir @cwd
297
298 valid_exists? @pid_file, "PID file #@pid_file does not exist. Not running?"
299 return @valid
300 end
301
302 def run
303 if @force
304 @wait.to_i.times do |waiting|
305 exit(0) if not File.exist? @pid_file
306 sleep 1
307 end
308
309 Mongrel::send_signal("KILL", @pid_file) if File.exist? @pid_file
310 else
311 Mongrel::send_signal("TERM", @pid_file)
312 end
313 end
314 end
315
316
317 class Restart < GemPlugin::Plugin "/commands"
318 include Mongrel::Command::Base
319
320 def configure
321 options [
322 ['-c', '--chdir PATH', "Change to dir before starting (will be expanded)", :@cwd, '.'],
323 ['-P', '--pid FILE', "Where the PID file is located", :@pid_file, "log/mongrel.pid"]
324 ]
325 end
326
327 def validate
328 @cwd = File.expand_path(@cwd)
329 valid_dir? @cwd, "Invalid path to change to during daemon mode: #@cwd"
330
331 Dir.chdir @cwd
332
333 valid_exists? @pid_file, "PID file #@pid_file does not exist. Not running?"
334 return @valid
335 end
336
337 def run
338 MongrelServer::send_signal("USR2", @pid_file)
339 end
340 end
341 end
342 end