diff options
-rw-r--r-- | TODO | 3 | ||||
-rw-r--r-- | bin/mongrel_rails | 284 | ||||
-rw-r--r-- | lib/mongrel.rb | 36 | ||||
-rw-r--r-- | lib/mongrel/cgi.rb | 5 | ||||
-rw-r--r-- | lib/mongrel/command.rb | 219 | ||||
-rw-r--r-- | lib/mongrel/handlers.rb | 199 | ||||
-rw-r--r-- | lib/mongrel/http_request.rb | 4 | ||||
-rw-r--r-- | lib/mongrel/stats.rb | 89 |
8 files changed, 31 insertions, 808 deletions
@@ -1,4 +1,5 @@ -Ruby 1.9 compat + +X Ruby 1.9 compat QA Rack compat PID-aware mongrel binary (like thin) Timestamped logger diff --git a/bin/mongrel_rails b/bin/mongrel_rails deleted file mode 100644 index 42ecf31..0000000 --- a/bin/mongrel_rails +++ /dev/null @@ -1,284 +0,0 @@ -#!/usr/bin/env ruby -# -# Copyright (c) 2005 Zed A. Shaw -# You can redistribute it and/or modify it under the same terms as Ruby. -# -# Additional work donated by contributors. See http://mongrel.rubyforge.org/attributions.html -# for more information. - -require 'yaml' -require 'etc' - -$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../lib" -require 'mongrel' -require 'mongrel/rails' - -Mongrel::Gems.require 'gem_plugin' - -# require 'ruby-debug' -# Debugger.start - -module Mongrel - class Start < GemPlugin::Plugin "/commands" - include Mongrel::Command::Base - - def configure - options [ - ["-e", "--environment ENV", "Rails environment to run as", :@environment, ENV['RAILS_ENV'] || "development"], - ["-d", "--daemonize", "Run daemonized in the background", :@daemon, false], - ['-p', '--port PORT', "Which port to bind to", :@port, 3000], - ['-a', '--address ADDR', "Address to bind to", :@address, "0.0.0.0"], - ['-l', '--log FILE', "Where to write log messages", :@log_file, "log/mongrel.log"], - ['-P', '--pid FILE', "Where to write the PID", :@pid_file, "log/mongrel.pid"], - ['-n', '--num-processors INT', "Number of processors active before clients denied", :@num_processors, 1024], - ['-o', '--timeout TIME', "Time to wait (in seconds) before killing a stalled thread", :@timeout, 60], - ['-t', '--throttle TIME', "Time to pause (in hundredths of a second) between accepting clients", :@throttle, 0], - ['-m', '--mime PATH', "A YAML file that lists additional MIME types", :@mime_map, nil], - ['-c', '--chdir PATH', "Change to dir before starting (will be expanded)", :@cwd, Dir.pwd], - ['-r', '--root PATH', "Set the document root (default 'public')", :@docroot, "public"], - ['-B', '--debug', "Enable debugging mode", :@debug, false], - ['-C', '--config PATH', "Use a config file", :@config_file, nil], - ['-S', '--script PATH', "Load the given file as an extra config script", :@config_script, nil], - ['-G', '--generate PATH', "Generate a config file for use with -C", :@generate, nil], - ['', '--user USER', "User to run as", :@user, nil], - ['', '--group GROUP', "Group to run as", :@group, nil], - ['', '--prefix PATH', "URL prefix for Rails app", :@prefix, nil] - ] - end - - def validate - if @config_file - valid_exists?(@config_file, "Config file not there: #@config_file") - return false unless @valid - @config_file = File.expand_path(@config_file) - load_config - return false unless @valid - end - - @cwd = File.expand_path(@cwd) - valid_dir? @cwd, "Invalid path to change to during daemon mode: #@cwd" - - # Change there to start, then we'll have to come back after daemonize - Dir.chdir(@cwd) - - valid?(@prefix[0] == ?/ && @prefix[-1] != ?/, "Prefix must begin with / and not end in /") if @prefix - valid_dir? File.dirname(@log_file), "Path to log file not valid: #@log_file" - valid_dir? File.dirname(@pid_file), "Path to pid file not valid: #@pid_file" - valid_dir? @docroot, "Path to docroot not valid: #@docroot" - valid_exists? @mime_map, "MIME mapping file does not exist: #@mime_map" if @mime_map - valid_exists? @config_file, "Config file not there: #@config_file" if @config_file - valid_dir? File.dirname(@generate), "Problem accessing directory to #@generate" if @generate - valid_user? @user if @user - valid_group? @group if @group - - return @valid - end - - def run - if @generate - @generate = File.expand_path(@generate) - STDERR.puts "** Writing config to \"#@generate\"." - open(@generate, "w") {|f| f.write(settings.to_yaml) } - STDERR.puts "** Finished. Run \"mongrel_rails start -C #@generate\" to use the config file." - exit 0 - end - - config = Mongrel::Rails::RailsConfigurator.new(settings) do - if defaults[:daemon] - if File.exist? defaults[:pid_file] - log "!!! PID file #{defaults[:pid_file]} already exists. Mongrel could be running already. Check your #{defaults[:log_file]} for errors." - log "!!! Exiting with error. You must stop mongrel and clear the .pid before I'll attempt a start." - exit 1 - end - - daemonize - write_pid_file - log "Daemonized, any open files are closed. Look at #{defaults[:pid_file]} and #{defaults[:log_file]} for info." - log "Settings loaded from #{@config_file} (they override command line)." if @config_file - end - - log "Starting Mongrel listening at #{defaults[:host]}:#{defaults[:port]}" - - listener do - mime = {} - if defaults[:mime_map] - log "Loading additional MIME types from #{defaults[:mime_map]}" - mime = load_mime_map(defaults[:mime_map], mime) - end - - if defaults[:debug] - log "Installing debugging prefixed filters. Look in log/mongrel_debug for the files." - debug "/" - end - - log "Starting Rails with #{defaults[:environment]} environment..." - log "Mounting Rails at #{defaults[:prefix]}..." if defaults[:prefix] - uri defaults[:prefix] || "/", :handler => rails(:mime => mime, :prefix => defaults[:prefix]) - log "Rails loaded." - - log "Loading any Rails specific GemPlugins" - load_plugins - - if defaults[:config_script] - log "Loading #{defaults[:config_script]} external config script" - run_config(defaults[:config_script]) - end - - setup_rails_signals - end - end - - config.run - config.log "Mongrel #{Mongrel::Const::MONGREL_VERSION} available at #{@address}:#{@port}" - - unless config.defaults[:daemon] - config.log "Use CTRL-C to stop." - end - - config.join - - if config.needs_restart - if RUBY_PLATFORM !~ /djgpp|(cyg|ms|bcc)win|mingw/ - cmd = "ruby #{__FILE__} start #{original_args.join(' ')}" - config.log "Restarting with arguments: #{cmd}" - config.stop(false, true) - config.remove_pid_file - - if config.defaults[:daemon] - system cmd - else - STDERR.puts "Can't restart unless in daemon mode." - exit 1 - end - else - config.log "Win32 does not support restarts. Exiting." - end - end - end - - def load_config - settings = {} - begin - settings = YAML.load_file(@config_file) - ensure - STDERR.puts "** Loading settings from #{@config_file} (they override command line)." unless @daemon || settings[:daemon] - end - - settings[:includes] ||= ["mongrel"] - - # Config file settings will override command line settings - settings.each do |key, value| - key = key.to_s - if config_keys.include?(key) - key = 'address' if key == 'host' - self.instance_variable_set("@#{key}", value) - else - failure "Unknown configuration setting: #{key}" - @valid = false - end - end - end - - def config_keys - @config_keys ||= - %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) - end - - def settings - config_keys.inject({}) do |hash, key| - value = self.instance_variable_get("@#{key}") - key = 'host' if key == 'address' - hash[key.to_sym] ||= value - hash - end - end - end - - def Mongrel::send_signal(signal, pid_file) - pid = File.read(pid_file).to_i - print "Sending #{signal} to Mongrel at PID #{pid}..." - begin - Process.kill(signal, pid) - rescue Errno::ESRCH - puts "Process does not exist. Not running." - end - - puts "Done." - end - - - class Stop < GemPlugin::Plugin "/commands" - include Mongrel::Command::Base - - def configure - options [ - ['-c', '--chdir PATH', "Change to dir before starting (will be expanded).", :@cwd, "."], - ['-f', '--force', "Force the shutdown (kill -9).", :@force, false], - ['-w', '--wait SECONDS', "Wait SECONDS before forcing shutdown", :@wait, "0"], - ['-P', '--pid FILE', "Where the PID file is located (cannot be changed via soft restart).", :@pid_file, "log/mongrel.pid"] - ] - end - - def validate - @cwd = File.expand_path(@cwd) - valid_dir? @cwd, "Invalid path to change to during daemon mode: #@cwd" - - Dir.chdir @cwd - - valid_exists? @pid_file, "PID file #@pid_file does not exist. Not running?" - return @valid - end - - def run - if @force - @wait.to_i.times do |waiting| - exit(0) if not File.exist? @pid_file - sleep 1 - end - - Mongrel::send_signal("KILL", @pid_file) if File.exist? @pid_file - else - Mongrel::send_signal("TERM", @pid_file) - end - end - end - - - class Restart < GemPlugin::Plugin "/commands" - include Mongrel::Command::Base - - def configure - options [ - ['-c', '--chdir PATH', "Change to dir before starting (will be expanded)", :@cwd, '.'], - ['-s', '--soft', "Do a soft restart rather than a process exit restart", :@soft, false], - ['-P', '--pid FILE', "Where the PID file is located", :@pid_file, "log/mongrel.pid"] - ] - end - - def validate - @cwd = File.expand_path(@cwd) - valid_dir? @cwd, "Invalid path to change to during daemon mode: #@cwd" - - Dir.chdir @cwd - - valid_exists? @pid_file, "PID file #@pid_file does not exist. Not running?" - return @valid - end - - def run - if @soft - Mongrel::send_signal("HUP", @pid_file) - else - Mongrel::send_signal("USR2", @pid_file) - end - end - end -end - - -GemPlugin::Manager.instance.load "mongrel" => GemPlugin::INCLUDE, "rails" => GemPlugin::EXCLUDE - - -if not Mongrel::Command::Registry.instance.run ARGV - exit 1 -end diff --git a/lib/mongrel.rb b/lib/mongrel.rb index 990a8eb..a97972b 100644 --- a/lib/mongrel.rb +++ b/lib/mongrel.rb @@ -8,6 +8,7 @@ require 'etc' require 'uri' require 'stringio' require 'fcntl' +require 'logger' # Compiled Mongrel extension require 'http11' @@ -30,6 +31,15 @@ require 'mongrel/http_response' # a Mongrel web server. It contains a minimalist HTTP server with just enough # functionality to service web application requests fast as possible. module Mongrel + class << self + # A logger instance that conforms to the API of stdlib's Logger. + attr_accessor :logger + + # By default, will return an instance of stdlib's Logger logging to STDERR + def logger + @logger ||= Logger.new STDERR + end + end # Used to stop the HttpServer via Thread.raise. class StopServer < Exception; end @@ -162,21 +172,21 @@ module Mongrel rescue EOFError,Errno::ECONNRESET,Errno::EPIPE,Errno::EINVAL,Errno::EBADF client.close rescue nil rescue HttpParserError => e - STDERR.puts "#{Time.now}: HTTP parse error, malformed request (#{params[Const::HTTP_X_FORWARDED_FOR] || client.peeraddr.last}): #{e.inspect}" - STDERR.puts "#{Time.now}: REQUEST DATA: #{data.inspect}\n---\nPARAMS: #{params.inspect}\n---\n" + Mongrel.logger.error "#{Time.now}: HTTP parse error, malformed request (#{params[Const::HTTP_X_FORWARDED_FOR] || client.peeraddr.last}): #{e.inspect}" + Mongrel.logger.error "#{Time.now}: REQUEST DATA: #{data.inspect}\n---\nPARAMS: #{params.inspect}\n---\n" rescue Errno::EMFILE reap_dead_workers('too many files') rescue Object => e - STDERR.puts "#{Time.now}: Read error: #{e.inspect}" - STDERR.puts e.backtrace.join("\n") + Mongrel.logger.error "#{Time.now}: Read error: #{e.inspect}" + Mongrel.logger.error e.backtrace.join("\n") ensure begin client.close rescue IOError # Already closed rescue Object => e - STDERR.puts "#{Time.now}: Client error: #{e.inspect}" - STDERR.puts e.backtrace.join("\n") + Mongrel.logger.error "#{Time.now}: Client error: #{e.inspect}" + Mongrel.logger.error e.backtrace.join("\n") end request.body.close! if request and request.body.class == Tempfile end @@ -188,14 +198,14 @@ module Mongrel # after the reap is done. It only runs if there are workers to reap. def reap_dead_workers(reason='unknown') if @workers.list.length > 0 - STDERR.puts "#{Time.now}: Reaping #{@workers.list.length} threads for slow workers because of '#{reason}'" + Mongrel.logger.info "#{Time.now}: Reaping #{@workers.list.length} threads for slow workers because of '#{reason}'" error_msg = "Mongrel timed out this thread: #{reason}" mark = Time.now @workers.list.each do |worker| worker[:started_on] = Time.now if not worker[:started_on] if mark - worker[:started_on] > @timeout + @throttle - STDERR.puts "Thread #{worker.inspect} is too old, killing." + Mongrel.logger.info "Thread #{worker.inspect} is too old, killing." worker.raise(TimeoutError.new(error_msg)) end end @@ -210,7 +220,7 @@ module Mongrel # that much longer. def graceful_shutdown while reap_dead_workers("shutdown") > 0 - STDERR.puts "Waiting for #{@workers.list.length} requests to finish, could take #{@timeout + @throttle} seconds." + Mongrel.logger.info "Waiting for #{@workers.list.length} requests to finish, could take #{@timeout + @throttle} seconds." sleep @timeout / 10 end end @@ -255,7 +265,7 @@ module Mongrel worker_list = @workers.list if worker_list.length >= @num_processors - STDERR.puts "Server overloaded with #{worker_list.length} processors (#@num_processors max). Dropping connection." + Mongrel.logger.error "Server overloaded with #{worker_list.length} processors (#@num_processors max). Dropping connection." client.close rescue nil reap_dead_workers("max processors") else @@ -274,14 +284,14 @@ module Mongrel # client closed the socket even before accept client.close rescue nil rescue Object => e - STDERR.puts "#{Time.now}: Unhandled listen loop exception #{e.inspect}." - STDERR.puts e.backtrace.join("\n") + Mongrel.logger.error "#{Time.now}: Unhandled listen loop exception #{e.inspect}." + Mongrel.logger.error e.backtrace.join("\n") end end graceful_shutdown ensure @socket.close - # STDERR.puts "#{Time.now}: Closed socket." + # Mongrel.logger.info "#{Time.now}: Closed socket." end end diff --git a/lib/mongrel/cgi.rb b/lib/mongrel/cgi.rb index 3957611..ed6fcf0 100644 --- a/lib/mongrel/cgi.rb +++ b/lib/mongrel/cgi.rb @@ -173,9 +173,8 @@ module Mongrel # The stdoutput should be completely bypassed but we'll drop a warning just in case def stdoutput - STDERR.puts "WARNING: Your program is doing something not expected. Please tell Zed that stdoutput was used and what software you are running. Thanks." + Mongrel.logger.warn "WARNING: Your program is doing something not expected. Please tell Zed that stdoutput was used and what software you are running. Thanks." @response.body - end - + end end end diff --git a/lib/mongrel/command.rb b/lib/mongrel/command.rb deleted file mode 100644 index c1fcca1..0000000 --- a/lib/mongrel/command.rb +++ /dev/null @@ -1,219 +0,0 @@ -# Copyright (c) 2005 Zed A. Shaw -# You can redistribute it and/or modify it under the same terms as Ruby. -# -# Additional work donated by contributors. See http://mongrel.rubyforge.org/attributions.html -# for more information. - -require 'singleton' -require 'optparse' - -module Mongrel - - # Contains all of the various commands that are used with - # Mongrel servers. - - module Command - - BANNER = "Usage: mongrel_rails <command> [options]" - - # A Command pattern implementation used to create the set of command available to the user - # from Mongrel. The script uses objects which implement this interface to do the - # user's bidding. - module Base - - attr_reader :valid, :done_validating, :original_args - - # Called by the implemented command to set the options for that command. - # Every option has a short and long version, a description, a variable to - # set, and a default value. No exceptions. - def options(opts) - # process the given options array - opts.each do |short, long, help, variable, default| - self.instance_variable_set(variable, default) - @opt.on(short, long, help) do |arg| - self.instance_variable_set(variable, arg) - end - end - end - - # Called by the subclass to setup the command and parse the argv arguments. - # The call is destructive on argv since it uses the OptionParser#parse! function. - def initialize(options={}) - argv = options[:argv] || [] - @opt = OptionParser.new - @opt.banner = Mongrel::Command::BANNER - @valid = true - # this is retarded, but it has to be done this way because -h and -v exit - @done_validating = false - @original_args = argv.dup - - configure - - # I need to add my own -h definition to prevent the -h by default from exiting. - @opt.on_tail("-h", "--help", "Show this message") do - @done_validating = true - puts @opt - end - - # I need to add my own -v definition to prevent the -v from exiting by default as well. - @opt.on_tail("--version", "Show version") do - @done_validating = true - if VERSION - puts "Version #{Mongrel::Const::MONGREL_VERSION}" - end - end - - @opt.parse! argv - end - - def configure - options [] - end - - # Returns true/false depending on whether the command is configured properly. - def validate - return @valid - end - - # Returns a help message. Defaults to OptionParser#help which should be good. - def help - @opt.help - end - - # Runs the command doing it's job. You should implement this otherwise it will - # throw a NotImplementedError as a reminder. - def run - raise NotImplementedError - end - - - # Validates the given expression is true and prints the message if not, exiting. - def valid?(exp, message) - if not @done_validating and (not exp) - failure message - @valid = false - @done_validating = true - end - end - - # Validates that a file exists and if not displays the message - def valid_exists?(file, message) - valid?(file != nil && File.exist?(file), message) - end - - - # Validates that the file is a file and not a directory or something else. - def valid_file?(file, message) - valid?(file != nil && File.file?(file), message) - end - - # Validates that the given directory exists - def valid_dir?(file, message) - valid?(file != nil && File.directory?(file), message) - end - - def valid_user?(user) - valid?(@group, "You must also specify a group.") - begin - Etc.getpwnam(user) - rescue - failure "User does not exist: #{user}" - @valid = false - end - end - - def valid_group?(group) - valid?(@user, "You must also specify a user.") - begin - Etc.getgrnam(group) - rescue - failure "Group does not exist: #{group}" - @valid = false - end - end - - # Just a simple method to display failure until something better is developed. - def failure(message) - STDERR.puts "!!! #{message}" - end - end - - # A Singleton class that manages all of the available commands - # and handles running them. - class Registry - include Singleton - - # Builds a list of possible commands from the Command derivates list - def commands - pmgr = GemPlugin::Manager.instance - list = pmgr.plugins["/commands"].keys - return list.sort - end - - # Prints a list of available commands. - def print_command_list - puts "#{Mongrel::Command::BANNER}\nAvailable commands are:\n\n" - - self.commands.each do |name| - if /mongrel::/ =~ name - name = name[9 .. -1] - end - - puts " - #{name[1 .. -1]}\n" - end - - puts "\nEach command takes -h as an option to get help." - - end - - - # Runs the args against the first argument as the command name. - # If it has any errors it returns a false, otherwise it return true. - def run(args) - # find the command - cmd_name = args.shift - - if !cmd_name or cmd_name == "?" or cmd_name == "help" - print_command_list - return true - elsif cmd_name == "--version" - puts "Mongrel Web Server #{Mongrel::Const::MONGREL_VERSION}" - return true - end - - begin - # quick hack so that existing commands will keep working but the Mongrel:: ones can be moved - if ["start", "stop", "restart"].include? cmd_name - cmd_name = "mongrel::" + cmd_name - end - - command = GemPlugin::Manager.instance.create("/commands/#{cmd_name}", :argv => args) - rescue OptionParser::InvalidOption - STDERR.puts "#$! for command '#{cmd_name}'" - STDERR.puts "Try #{cmd_name} -h to get help." - return false - rescue - STDERR.puts "ERROR RUNNING '#{cmd_name}': #$!" - STDERR.puts "Use help command to get help" - return false - end - - # Normally the command is NOT valid right after being created - # but sometimes (like with -h or -v) there's no further processing - # needed so the command is already valid so we can skip it. - if not command.done_validating - if not command.validate - STDERR.puts "#{cmd_name} reported an error. Use mongrel_rails #{cmd_name} -h to get help." - return false - else - command.run - end - end - - return true - end - - end - end -end - diff --git a/lib/mongrel/handlers.rb b/lib/mongrel/handlers.rb index ce24628..0ae3af6 100644 --- a/lib/mongrel/handlers.rb +++ b/lib/mongrel/handlers.rb @@ -4,21 +4,16 @@ # Additional work donated by contributors. See http://mongrel.rubyforge.org/attributions.html # for more information. -require 'mongrel/stats' require 'zlib' require 'yaml' module Mongrel - + # # You implement your application handler with this. It's very light giving # just the minimum necessary for you to handle a request and shoot back # a response. Look at the HttpRequest and HttpResponse objects for how # to use them. # - # This is used for very simple handlers that don't require much to operate. - # More extensive plugins or those you intend to distribute as GemPlugins - # should be implemented using the HttpHandlerPlugin mixin. - # class HttpHandler attr_reader :request_notify attr_accessor :listener @@ -38,195 +33,5 @@ module Mongrel def process(request, response) end - - end - - - # This is used when your handler is implemented as a GemPlugin. - # The plugin always takes an options hash which you can modify - # and then access later. They are stored by default for - # the process method later. - module HttpHandlerPlugin - attr_reader :options - attr_reader :request_notify - attr_accessor :listener - - def request_begins(params) - end - - def request_progress(params, clen, total) - end - - def initialize(options={}) - @options = options - @header_only = false - end - - def process(request, response) - end - - end - - - # - # The server normally returns a 404 response if an unknown URI is requested, but it - # also returns a lame empty message. This lets you do a 404 response - # with a custom message for special URIs. - # - class Error404Handler < HttpHandler - - # Sets the message to return. This is constructed once for the handler - # so it's pretty efficient. - def initialize(msg) - @response = Const::ERROR_404_RESPONSE + msg - end - - # Just kicks back the standard 404 response with your special message. - def process(request, response) - response.socket.write(@response) - end - - end - - # When added to a config script (-S in mongrel_rails) it will - # look at the client's allowed response types and then gzip - # compress anything that is going out. - # - # Valid option is :always_deflate => false which tells the handler to - # deflate everything even if the client can't handle it. - class DeflateFilter < HttpHandler - include Zlib - HTTP_ACCEPT_ENCODING = "HTTP_ACCEPT_ENCODING" - - def initialize(ops={}) - @options = ops - @always_deflate = ops[:always_deflate] || false - end - - def process(request, response) - accepts = request.params[HTTP_ACCEPT_ENCODING] - # only process if they support compression - if @always_deflate or (accepts and (accepts.include? "deflate" and not response.body_sent)) - response.header["Content-Encoding"] = "deflate" - response.body = deflate(response.body) - end - end - - private - def deflate(stream) - deflater = Deflate.new( - DEFAULT_COMPRESSION, - # drop the zlib header which causes both Safari and IE to choke - -MAX_WBITS, - DEF_MEM_LEVEL, - DEFAULT_STRATEGY) - - stream.rewind - gzout = StringIO.new(deflater.deflate(stream.read, FINISH)) - stream.close - gzout.rewind - gzout - end - end - - - # Implements a few basic statistics for a particular URI. Register it anywhere - # you want in the request chain and it'll quickly gather some numbers for you - # to analyze. It is pretty fast, but don't put it out in production. - # - # You should pass the filter to StatusHandler as StatusHandler.new(:stats_filter => stats). - # This lets you then hit the status URI you want and get these stats from a browser. - # - # StatisticsFilter takes an option of :sample_rate. This is a number that's passed to - # rand and if that number gets hit then a sample is taken. This helps reduce the load - # and keeps the statistics valid (since sampling is a part of how they work). - # - # The exception to :sample_rate is that inter-request time is sampled on every request. - # If this wasn't done then it wouldn't be accurate as a measure of time between requests. - class StatisticsFilter < HttpHandler - attr_reader :stats - - def initialize(ops={}) - @sample_rate = ops[:sample_rate] || 300 - - @processors = Mongrel::Stats.new("processors") - @reqsize = Mongrel::Stats.new("request Kb") - @headcount = Mongrel::Stats.new("req param count") - @respsize = Mongrel::Stats.new("response Kb") - @interreq = Mongrel::Stats.new("inter-request time") - end - - - def process(request, response) - if rand(@sample_rate)+1 == @sample_rate - @processors.sample(listener.workers.list.length) - @headcount.sample(request.params.length) - @reqsize.sample(request.body.length / 1024.0) - @respsize.sample((response.body.length + response.header.out.length) / 1024.0) - end - @interreq.tick - end - - def dump - "#{@processors.to_s}\n#{@reqsize.to_s}\n#{@headcount.to_s}\n#{@respsize.to_s}\n#{@interreq.to_s}" - end - end - - - # The :stats_filter is basically any configured stats filter that you've added to this same - # URI. This lets the status handler print out statistics on how Mongrel is doing. - class StatusHandler < HttpHandler - def initialize(ops={}) - @stats = ops[:stats_filter] - end - - def table(title, rows) - results = "<table border=\"1\"><tr><th colspan=\"#{rows[0].length}\">#{title}</th></tr>" - rows.each do |cols| - results << "<tr>" - cols.each {|col| results << "<td>#{col}</td>" } - results << "</tr>" - end - results + "</table>" - end - - def describe_listener - results = "" - results << "<h1>Listener #{listener.host}:#{listener.port}</h1>" - results << table("settings", [ - ["host",listener.host], - ["port",listener.port], - ["throttle",listener.throttle], - ["timeout",listener.timeout], - ["workers max",listener.num_processors], - ]) - - if @stats - results << "<h2>Statistics</h2><p>N means the number of samples, pay attention to MEAN, SD, MIN and MAX." - results << "<pre>#{@stats.dump}</pre>" - end - - results << "<h2>Registered Handlers</h2>" - handler_map = listener.classifier.handler_map - results << table("handlers", handler_map.map {|uri,handlers| - [uri, - "<pre>" + - handlers.map {|h| h.class.to_s }.join("\n") + - "</pre>" - ] - }) - - results - end - - def process(request, response) - response.start do |head,out| - out.write <<-END - <html><body><title>Mongrel Server Status</title> - #{describe_listener} - </body></html> - END - end - end - end + end end diff --git a/lib/mongrel/http_request.rb b/lib/mongrel/http_request.rb index 2416b04..70f236f 100644 --- a/lib/mongrel/http_request.rb +++ b/lib/mongrel/http_request.rb @@ -78,8 +78,8 @@ module Mongrel remain -= @body.write(@params.http_body) end rescue Object => e - STDERR.puts "#{Time.now}: Error reading HTTP body: #{e.inspect}" - STDERR.puts e.backtrace.join("\n") + Mongrel.logger.error "#{Time.now}: Error reading HTTP body: #{e.inspect}" + Mongrel.logger.error e.backtrace.join("\n") # any errors means we should delete the file, including if the file is dumped @socket.close rescue nil @body.close! if @body.class == Tempfile diff --git a/lib/mongrel/stats.rb b/lib/mongrel/stats.rb deleted file mode 100644 index f6cf5ab..0000000 --- a/lib/mongrel/stats.rb +++ /dev/null @@ -1,89 +0,0 @@ -# Copyright (c) 2005 Zed A. Shaw -# You can redistribute it and/or modify it under the same terms as Ruby. -# -# Additional work donated by contributors. See http://mongrel.rubyforge.org/attributions.html -# for more information. - -# A very simple little class for doing some basic fast statistics sampling. -# You feed it either samples of numeric data you want measured or you call -# Stats.tick to get it to add a time delta between the last time you called it. -# When you're done either call sum, sumsq, n, min, max, mean or sd to get -# the information. The other option is to just call dump and see everything. -# -# It does all of this very fast and doesn't take up any memory since the samples -# are not stored but instead all the values are calculated on the fly. -module Mongrel - class Stats - attr_reader :sum, :sumsq, :n, :min, :max - - def initialize(name) - @name = name - reset - end - - # Resets the internal counters so you can start sampling again. - def reset - @sum = 0.0 - @sumsq = 0.0 - @last_time = Time.new - @n = 0.0 - @min = 0.0 - @max = 0.0 - end - - # Adds a sampling to the calculations. - def sample(s) - @sum += s - @sumsq += s * s - if @n == 0 - @min = @max = s - else - @min = s if @min > s - @max = s if @max < s - end - @n+=1 - end - - # Dump this Stats object with an optional additional message. - def dump(msg = "", out=STDERR) - out.puts "#{msg}: #{self.to_s}" - end - - # Returns a common display (used by dump) - def to_s - "[#{@name}]: SUM=%0.4f, SUMSQ=%0.4f, N=%0.4f, MEAN=%0.4f, SD=%0.4f, MIN=%0.4f, MAX=%0.4f" % [@sum, @sumsq, @n, mean, sd, @min, @max] - end - - - # Calculates and returns the mean for the data passed so far. - def mean - @sum / @n - end - - # Calculates the standard deviation of the data so far. - def sd - # (sqrt( ((s).sumsq - ( (s).sum * (s).sum / (s).n)) / ((s).n-1) )) - begin - return Math.sqrt( (@sumsq - ( @sum * @sum / @n)) / (@n-1) ) - rescue Errno::EDOM - return 0.0 - end - end - - - # Adds a time delta between now and the last time you called this. This - # will give you the average time between two activities. - # - # An example is: - # - # t = Stats.new("do_stuff") - # 10000.times { do_stuff(); t.tick } - # t.dump("time") - # - def tick - now = Time.now - sample(now - @last_time) - @last_time = now - end - end -end |