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 27
def initialize(stdout, stderr)
  @formatter = DefaultFormatter.new
  @stdout = stdout
  @stderr = stderr

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

  @debug = ENV.key? 'PUMA_DEBUG'
  @error_logger = ErrorLogger.new(@stderr)

  @hooks = Hash.new { |h,k| h[k] = [] }
end
null() click to toggle source
# File lib/puma/events.rb, line 172
def self.null
  n = NullIO.new
  Events.new n, n
end
stdio() click to toggle source
# File lib/puma/events.rb, line 168
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 164
def self.strings
  Events.new StringIO.new, StringIO.new
end

Public Instance Methods

connection_error(error, req, text="HTTP connection error") click to toggle source

An HTTP connection error has occurred. error a connection exception, req the request, and text additional info @version 5.0.0

# File lib/puma/events.rb, line 95
def connection_error(error, req, text="HTTP connection error")
  @error_logger.info(error: error, req: req, text: text)
end
debug(str) click to toggle source
# File lib/puma/events.rb, line 75
def debug(str)
  log("% #{str}") if @debug
end
debug_error(error, req=nil, text="") click to toggle source

Log occurred error debug dump. error an exception object, req the request, and text additional info @version 5.0.0

# File lib/puma/events.rb, line 131
def debug_error(error, req=nil, text="")
  @error_logger.debug(error: error, req: req, text: text)
end
error(str) click to toggle source

Write str to +@stderr+

# File lib/puma/events.rb, line 81
def error(str)
  @error_logger.info(text: 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 46
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 147
def fire_on_booted!
  fire(:on_booted)
end
fire_on_restart!() click to toggle source
# File lib/puma/events.rb, line 151
def fire_on_restart!
  fire(:on_restart)
end
fire_on_stopped!() click to toggle source
# File lib/puma/events.rb, line 155
def fire_on_stopped!
  fire(:on_stopped)
end
format(str) click to toggle source
# File lib/puma/events.rb, line 86
def format(str)
  formatter.call(str)
end
log(str) click to toggle source

Write str to +@stdout+

# File lib/puma/events.rb, line 66
def log(str)
  @stdout.puts format(str) if @stdout.respond_to? :puts
rescue Errno::EPIPE
end
on_booted(&block) click to toggle source
# File lib/puma/events.rb, line 135
def on_booted(&block)
  register(:on_booted, &block)
end
on_restart(&block) click to toggle source
# File lib/puma/events.rb, line 139
def on_restart(&block)
  register(:on_restart, &block)
end
on_stopped(&block) click to toggle source
# File lib/puma/events.rb, line 143
def on_stopped(&block)
  register(:on_stopped, &block)
end
parse_error(error, req) click to toggle source

An HTTP parse error has occurred. error a parsing exception, and req the request.

# File lib/puma/events.rb, line 103
def parse_error(error, req)
  @error_logger.info(error: error, req: req, text: 'HTTP parse error, malformed request')
end
register(hook, obj=nil, &blk) click to toggle source

Register a callback for a given hook

# File lib/puma/events.rb, line 52
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(error, ssl_socket) click to toggle source

An SSL error has occurred. @param error <Puma::MiniSSL::SSLError> @param ssl_socket <Puma::MiniSSL::Socket>

# File lib/puma/events.rb, line 111
def ssl_error(error, ssl_socket)
  peeraddr = ssl_socket.peeraddr.last rescue "<unknown>"
  peercert = ssl_socket.peercert
  subject = peercert ? peercert.subject : nil
  @error_logger.info(error: error, text: "SSL error, peer: #{peeraddr}, peer cert: #{subject}")
end
unknown_error(error, req=nil, text="Unknown error") click to toggle source

An unknown error has occurred. error an exception object, req the request, and text additional info

# File lib/puma/events.rb, line 122
def unknown_error(error, req=nil, text="Unknown error")
  @error_logger.info(error: error, req: req, text: text)
end
write(str) click to toggle source
# File lib/puma/events.rb, line 71
def write(str)
  @stdout.write format(str)
end