Changeset 899
- Timestamp:
- 12/15/07 00:02:04 (9 months ago)
- Files:
-
- trunk/bin/mongrel_rails (modified) (3 diffs)
- trunk/examples/builder.rb (modified) (1 diff)
- trunk/lib/mongrel.rb (modified) (14 diffs)
- trunk/lib/mongrel/cgi.rb (modified) (1 diff)
- trunk/lib/mongrel/configurator.rb (modified) (6 diffs)
- trunk/lib/mongrel/const.rb (modified) (1 diff)
- trunk/lib/mongrel/debug.rb (modified) (6 diffs)
- trunk/lib/mongrel/log.rb (added)
- trunk/projects/mongrel_cluster/lib/mongrel_cluster/init.rb (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/bin/mongrel_rails
r891 r899 76 76 if @generate 77 77 @generate = File.expand_path(@generate) 78 STDERR.puts "** Writing config to \"#@generate\"."78 log(:error, "** Writing config to \"#@generate\".") 79 79 open(@generate, "w") {|f| f.write(settings.to_yaml) } 80 STDERR.puts "** Finished. Run \"mongrel_rails start -C #@generate\" to use the config file."80 log(:error, "** Finished. Run \"mongrel_rails start -C #@generate\" to use the config file.") 81 81 exit 0 82 82 end … … 147 147 system cmd 148 148 else 149 STDERR.puts "Can't restart unless in daemon mode."149 log(:error, "Can't restart unless in daemon mode.") 150 150 exit 1 151 151 end … … 161 161 settings = YAML.load_file(@config_file) 162 162 ensure 163 STDERR.puts "** Loading settings from #{@config_file} (they override command line)." unless @daemon || settings[:daemon]163 log(:error, "** Loading settings from #{@config_file} (they override command line)." unless @daemon || settings[:daemon] ) 164 164 end 165 165 trunk/examples/builder.rb
r121 r899 5 5 6 6 def process(request, response) 7 STDERR.puts "My options are: #{options.inspect}"8 STDERR.puts "Request Was:"9 STDERR.puts request.params.to_yaml7 log(:error, "My options are: #{options.inspect}") 8 log(:error, "Request Was:") 9 log(:error, request.params.to_yaml) 10 10 end 11 11 end trunk/lib/mongrel.rb
r889 r899 1 2 1 require 'socket' 3 2 require 'tempfile' … … 15 14 16 15 require 'http11' 16 require 'mongrel/log' 17 17 require 'mongrel/cgi' 18 18 require 'mongrel/handlers' … … 41 41 attr_accessor :http_body 42 42 end 43 44 43 45 44 # This is the main driver of Mongrel, while the Mongrel::HttpParser and Mongrel::URIClassifier … … 84 83 # socket.accept calls in order to give the server a cheap throttle time. It defaults to 0 and 85 84 # actually if it is 0 then the sleep is not done at all. 86 def initialize(host, port, num_processors=950, throttle=0, timeout=60 )85 def initialize(host, port, num_processors=950, throttle=0, timeout=60, log=nil) 87 86 88 87 tries = 0 … … 96 95 @num_processors = num_processors 97 96 @timeout = timeout 97 Mongrel::Logger = Mongrel::Log.new(log || "mongrel-#{port}.log") 98 98 end 99 99 … … 181 181 client.close rescue nil 182 182 rescue HttpParserError => e 183 STDERR.puts "#{Time.now.httpdate}: HTTP parse error, malformed request (#{params[Const::HTTP_X_FORWARDED_FOR] || client.peeraddr.last}): #{e.inspect}"184 STDERR.puts "#{Time.now.httpdate}: REQUEST DATA: #{data.inspect}\n---\nPARAMS: #{params.inspect}\n---\n"183 log(:error, "#{Time.now.httpdate}: HTTP parse error, malformed request (#{params[Const::HTTP_X_FORWARDED_FOR] || client.peeraddr.last}): #{e.inspect}") 184 log(:error, "#{Time.now.httpdate}: REQUEST DATA: #{data.inspect}\n---\nPARAMS: #{params.inspect}\n---\n") 185 185 # http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4 186 186 client.write(Const::ERROR_400_RESPONSE) … … 188 188 reap_dead_workers('too many files') 189 189 rescue Object => e 190 STDERR.puts "#{Time.now.httpdate}: Read error: #{e.inspect}"191 STDERR.puts e.backtrace.join("\n")190 log(:error, "#{Time.now.httpdate}: Read error: #{e.inspect}") 191 log(:error, e.backtrace.join("\n")) 192 192 ensure 193 193 begin … … 196 196 # Already closed 197 197 rescue Object => e 198 STDERR.puts "#{Time.now.httpdate}: Client error: #{e.inspect}"199 STDERR.puts e.backtrace.join("\n")198 log(:error, "#{Time.now.httpdate}: Client error: #{e.inspect}") 199 log(:error, e.backtrace.join("\n")) 200 200 end 201 201 request.body.delete if request and request.body.class == Tempfile … … 209 209 def reap_dead_workers(reason='unknown') 210 210 if @workers.list.length > 0 211 STDERR.puts "#{Time.now.httpdate}: Reaping #{@workers.list.length} threads for slow workers because of '#{reason}'"211 log(:error, "#{Time.now.httpdate}: Reaping #{@workers.list.length} threads for slow workers because of '#{reason}'") 212 212 error_msg = "#{Time.now.httpdate}: Mongrel timed out this thread: #{reason}" 213 213 mark = Time.now … … 216 216 217 217 if mark - worker[:started_on] > @timeout + @throttle 218 STDERR.puts "#{Time.now.httpdate}: Thread #{worker.inspect} is too old, killing."218 log(:error, "#{Time.now.httpdate}: Thread #{worker.inspect} is too old, killing.") 219 219 worker.raise(TimeoutError.new(error_msg)) 220 220 end … … 231 231 def graceful_shutdown 232 232 while reap_dead_workers("shutdown") > 0 233 STDERR.puts "#{Time.now.httpdate}: Waiting for #{@workers.list.length} requests to finish, could take #{@timeout + @throttle} seconds."233 log(:error, "#{Time.now.httpdate}: Waiting for #{@workers.list.length} requests to finish, could take #{@timeout + @throttle} seconds.") 234 234 sleep @timeout / 10 235 235 end … … 277 277 278 278 if worker_list.length >= @num_processors 279 STDERR.puts "#{Time.now.httpdate}: Server overloaded with #{worker_list.length} processors (#@num_processors max). Dropping connection."279 log(:error, "#{Time.now.httpdate}: Server overloaded with #{worker_list.length} processors (#@num_processors max). Dropping connection.") 280 280 client.close rescue nil 281 281 reap_dead_workers("max processors") … … 296 296 client.close rescue nil 297 297 rescue Object => e 298 STDERR.puts "#{Time.now.httpdate}: Unhandled listen loop exception #{e.inspect}."299 STDERR.puts e.backtrace.join("\n")298 log(:error, "#{Time.now.httpdate}: Unhandled listen loop exception #{e.inspect}.") 299 log(:error, e.backtrace.join("\n")) 300 300 end 301 301 end … … 303 303 ensure 304 304 @socket.close 305 # STDERR.puts "#{Time.now.httpdate}: Closed socket."305 # log(:error, "#{Time.now.httpdate}: Closed socket.") 306 306 end 307 307 end trunk/lib/mongrel/cgi.rb
r889 r899 174 174 # The stdoutput should be completely bypassed but we'll drop a warning just in case 175 175 def stdoutput 176 STDERR.puts "#{Time.now. httpdate}: WARNING: Your program is doing something not expected. Please tell Zed that stdoutput was used and what software you are running. Thanks."176 STDERR.puts "#{Time.now.rfc822}: WARNING: Your program is doing something not expected. Please tell Zed that stdoutput was used and what software you are running. Thanks." 177 177 @response.body 178 178 end 179 179 180 180 end 181 181 182 end trunk/lib/mongrel/configurator.rb
r891 r899 60 60 61 61 if uid != target_uid or gid != target_gid 62 log "Initiating groups for #{user.inspect}:#{group.inspect}."62 log(:info, "Initiating groups for #{user.inspect}:#{group.inspect}.") 63 63 Process.initgroups(user, target_gid) 64 64 65 log "Changing group to #{group.inspect}."65 log(:info, "Changing group to #{group.inspect}.") 66 66 Process::GID.change_privilege(target_gid) 67 67 68 log "Changing user to #{user.inspect}."68 log(:info, "Changing user to #{user.inspect}." ) 69 69 Process::UID.change_privilege(target_uid) 70 70 end 71 71 rescue Errno::EPERM => e 72 log "Couldn't change user and group to #{user.inspect}:#{group.inspect}: #{e.to_s}."73 log "Mongrel failed to start."72 log(:critical, "Couldn't change user and group to #{user.inspect}:#{group.inspect}: #{e.to_s}.") 73 log(:critical, "Mongrel failed to start.") 74 74 exit 1 75 75 end … … 83 83 def write_pid_file 84 84 unless RUBY_PLATFORM =~ /djgpp|(cyg|ms|bcc)win|mingw/ 85 log "Writing PID file to #{@pid_file}"85 log(:info, "Writing PID file to #{@pid_file}") 86 86 open(@pid_file,"w") {|f| f.write(Process.pid) } 87 87 open(@pid_file,"w") do |f| … … 193 193 logfile = File.join(ops[:cwd],logfile) 194 194 if not File.exist?(File.dirname(logfile)) 195 log "!!! Log file directory not found at full path #{File.dirname(logfile)}. Update your configuration to use a full path."195 log(:critical, "!!! Log file directory not found at full path #{File.dirname(logfile)}. Update your configuration to use a full path.") 196 196 exit 1 197 197 end … … 204 204 205 205 else 206 log "WARNING: Win32 does not support daemon mode."206 log(:warning, "WARNING: Win32 does not support daemon mode.") 207 207 end 208 208 end … … 250 250 251 251 # check all the mime types to make sure they are the right format 252 mime.each {|k,v| log "WARNING: MIME type #{k} must start with '.'"if k.index(".") != 0 }252 mime.each {|k,v| log(, "WARNING: MIME type #{k} must start with '.'") if k.index(".") != 0 } 253 253 254 254 return mime … … 362 362 363 363 # forced shutdown, even if previously restarted (actually just like TERM but for CTRL-C) 364 trap("INT") { log "INT signal received."; stop(false) }365 366 # clean up the pid file always364 trap("INT") { log(:notice, "INT signal received."; stop(false) } 365 366 # always clean up the pid file 367 367 at_exit { remove_pid_file } 368 368 369 369 unless RUBY_PLATFORM =~ /djgpp|(cyg|ms|bcc)win|mingw/ 370 370 # graceful shutdown 371 trap("TERM") { log "TERM signal received."; stop } 372 trap("USR1") { log "USR1 received, toggling $mongrel_debug_client to #{!$mongrel_debug_client}"; $mongrel_debug_client = !$mongrel_debug_client } 371 trap("TERM") { log(:notice, "TERM signal received."; stop) } 372 # debug mode 373 trap("USR1") { log(:notice, "USR1 received, toggling $mongrel_debug_client to #{!$mongrel_debug_client}"; $mongrel_debug_client = !$mongrel_debug_client) } 373 374 # restart 374 trap("USR2") { log "USR2 signal received."; stop(true) }375 376 log "Signals ready. TERM => stop. USR2 => restart. INT => stop (no restart)."375 trap("USR2") { log(:notice, "USR2 signal received."; stop(true)) } 376 377 log(:notice, "Signals ready. TERM => stop. USR2 => restart. INT => stop (no restart).") 377 378 else 378 log "Signals ready. INT => stop (no restart)." 379 end 380 end 381 382 # Logs a simple message to STDERR (or the mongrel log if in daemon mode). 383 def log(msg) 384 STDERR.print "#{Time.now.httpdate}: #{msg}\n" 379 log(:notice, "Signals ready. INT => stop (no restart).") 380 end 385 381 end 386 382 387 383 end 384 388 385 end trunk/lib/mongrel/const.rb
r886 r899 111 111 HOST = "HOST".freeze 112 112 end 113 113 114 end trunk/lib/mongrel/debug.rb
r889 r899 87 87 MongrelDbg::trace(:files, open_counts.to_yaml) 88 88 end 89 end 90 89 end 91 90 92 91 93 92 module RequestLog 94 93 95 # Just logs whatever requests it gets to STDERR (which ends up in the mongrel96 # log when daemonized).97 94 class Access < GemPlugin::Plugin "/handlers" 98 95 include Mongrel::HttpHandlerPlugin … … 100 97 def process(request,response) 101 98 p = request.params 102 STDERR.puts "#{p['REMOTE_ADDR']} - [#{Time.now.httpdate}] \"#{p['REQUEST_METHOD']} #{p["REQUEST_URI"]} HTTP/1.1\"" 99 #STDERR.puts "#{p['REMOTE_ADDR']} - [#{Time.now.httpdate}] \"#{p['REQUEST_METHOD']} #{p["REQUEST_URI"]} HTTP/1.1\"" 100 log(:info, "#{p['REMOTE_ADDR']} \"#{p['REQUEST_METHOD']} #{p["REQUEST_URI"]} HTTP/1.1\"") 103 101 end 104 102 end … … 114 112 115 113 end 114 116 115 117 116 # stolen from Robert Klemme … … 126 125 ObjectSpace.each_object do |o| 127 126 begin 128 if o.respond_to? :length127 if o.respond_to?(:length) 129 128 len = o.length 130 129 lengths[o.class] ||= Mongrel::Stats.new(o.class) … … 160 159 end 161 160 161 162 162 class Params < GemPlugin::Plugin "/handlers" 163 163 include Mongrel::HttpHandlerPlugin … … 169 169 170 170 end 171 171 172 172 173 class Threads < GemPlugin::Plugin "/handlers" trunk/projects/mongrel_cluster/lib/mongrel_cluster/init.rb
r889 r899 14 14 @valid 15 15 end 16 16 17 17 def read_options 18 18 @options = { … … 97 97 log_verbose exec_cmd 98 98 output = `#{exec_cmd}` 99 log _error outputunless $?.success?99 log(:error, output) unless $?.success? 100 100 end 101 101 end … … 126 126 log_verbose exec_cmd 127 127 output = `#{exec_cmd}` 128 log _error outputunless $?.success?128 log(:error, output) unless $?.success? 129 129 130 130 end … … 139 139 pid = check_process(port) 140 140 unless pid_file_exists?(port) 141 log "missing pid_file: #{port_pid_file(port)}"141 log(:error, "missing pid_file: #{port_pid_file(port)}") 142 142 status = STATUS_ERROR 143 143 else 144 log "found pid_file: #{port_pid_file(port)}"144 log(:info, "found pid_file: #{port_pid_file(port)}") 145 145 end 146 146 if pid 147 log "found mongrel_rails: port #{port}, pid #{pid}"147 log(:info, "found mongrel_rails: port #{port}, pid #{pid}") 148 148 else 149 log "missing mongrel_rails: port #{port}"149 log(:error, "missing mongrel_rails: port #{port}") 150 150 status = STATUS_ERROR 151 151 end 152 puts ""152 log(:info, "") 153 153 end 154 154 … … 212 212 end 213 213 214 def log_error(message)215 log(message)216 end217 218 214 def log_verbose(message) 219 log(message) if @verbose 220 end 221 222 def log(message) 223 puts("#{Time.now.httpdate}: #{message}") 224 end 225 end 215 log(:info, message) if @verbose 216 end 217 218 end 219 226 220 class Start < GemPlugin::Plugin "/commands" 227 221 include ExecBase
