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
Public Class Methods
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
# File lib/puma/events.rb, line 148 def self.null n = NullIO.new Events.new n, n end
# File lib/puma/events.rb, line 144 def self.stdio Events.new $stdout, $stderr end
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
# File lib/puma/events.rb, line 76 def debug(str) log("% #{str}") if @debug end
Write str
to +@stderr+
# File lib/puma/events.rb, line 82 def error(str) @stderr.puts format("ERROR: #{str}") exit 1 end
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
# File lib/puma/events.rb, line 131 def fire_on_booted! fire(:on_booted) end
# File lib/puma/events.rb, line 87 def format(str) formatter.call(str) end
Write str
to +@stdout+
# File lib/puma/events.rb, line 68 def log(str) @stdout.puts format(str) end
# File lib/puma/events.rb, line 127 def on_booted(&block) register(:on_booted, &block) end
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 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
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
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
# File lib/puma/events.rb, line 72 def write(str) @stdout.write format(str) end