class Puma::Events

The default implement of an event sink object used by Server for when certain kinds of events occur in the life of the server.

The methods available are the events that the Server fires.

Constants

DEFAULT

Attributes

formatter[RW]
stderr[R]
stdout[R]

Public Class Methods

new(stdout, stderr) click to toggle source

Create an Events object that prints to stdout and stderr.

# File lib/puma/events.rb, line 30
def initialize(stdout, stderr)
  @formatter = DefaultFormatter.new
  @stdout = stdout
  @stderr = stderr

  @stdout.sync = true
  @stderr.sync = true

  @debug = ENV.key? 'PUMA_DEBUG'

  @hooks = Hash.new { |h,k| h[k] = [] }
end
null() click to toggle source
# File lib/puma/events.rb, line 148
def self.null
  n = NullIO.new
  Events.new n, n
end
stdio() click to toggle source
# File lib/puma/events.rb, line 144
def self.stdio
  Events.new $stdout, $stderr
end
strings() click to toggle source

Returns an Events object which writes its status to 2 StringIO objects.

# File lib/puma/events.rb, line 140
def self.strings
  Events.new StringIO.new, StringIO.new
end

Public Instance Methods

debug(str) click to toggle source
# File lib/puma/events.rb, line 76
def debug(str)
  log("% #{str}") if @debug
end
error(str) click to toggle source

Write str to +@stderr+

# File lib/puma/events.rb, line 82
def error(str)
  @stderr.puts format("ERROR: #{str}")
  exit 1
end
fire(hook, *args) click to toggle source

Fire callbacks for the named hook

# File lib/puma/events.rb, line 48
def fire(hook, *args)
  @hooks[hook].each { |t| t.call(*args) }
end
fire_on_booted!() click to toggle source
# File lib/puma/events.rb, line 131
def fire_on_booted!
  fire(:on_booted)
end
format(str) click to toggle source
# File lib/puma/events.rb, line 87
def format(str)
  formatter.call(str)
end
log(str) click to toggle source

Write str to +@stdout+

# File lib/puma/events.rb, line 68
def log(str)
  @stdout.puts format(str)
end
on_booted(&block) click to toggle source
# File lib/puma/events.rb, line 127
def on_booted(&block)
  register(:on_booted, &block)
end
parse_error(server, env, error) click to toggle source

An HTTP parse error has occurred. server is the Server object, env the request, and error a parsing exception.

# File lib/puma/events.rb, line 95
def parse_error(server, env, error)
  @stderr.puts "#{Time.now}: HTTP parse error, malformed request (#{env[HTTP_X_FORWARDED_FOR] || env[REMOTE_ADDR]}): #{error.inspect}\n---\n"
end
register(hook, obj=nil, &blk) click to toggle source

Register a callback for a given hook

# File lib/puma/events.rb, line 54
def register(hook, obj=nil, &blk)
  if obj and blk
    raise "Specify either an object or a block, not both"
  end

  h = obj || blk

  @hooks[hook] << h

  h
end
ssl_error(server, peeraddr, peercert, error) click to toggle source

An SSL error has occurred. server is the Server object, peeraddr peer address, peercert any peer certificate (if present), and error an exception object.

# File lib/puma/events.rb, line 103
def ssl_error(server, peeraddr, peercert, error)
  subject = peercert ? peercert.subject : nil
  @stderr.puts "#{Time.now}: SSL error, peer: #{peeraddr}, peer cert: #{subject}, #{error.inspect}"
end
unknown_error(server, error, kind="Unknown", env=nil) click to toggle source

An unknown error has occurred. server is the Server object, error an exception object, kind some additional info, and env the request.

# File lib/puma/events.rb, line 112
def unknown_error(server, error, kind="Unknown", env=nil)
  if error.respond_to? :render
    error.render "#{Time.now}: #{kind} error", @stderr
  else
    if env
      string_block = [ "#{Time.now}: #{kind} error handling request { #{env['REQUEST_METHOD']} #{env['PATH_INFO']} }" ]
      string_block << error.inspect
    else
      string_block = [ "#{Time.now}: #{kind} error: #{error.inspect}" ]
    end
    string_block << error.backtrace
    @stderr.puts string_block.join("\n")
  end
end
write(str) click to toggle source
# File lib/puma/events.rb, line 72
def write(str)
  @stdout.write format(str)
end