Class: Puma::Events
- Inherits:
-
Object
- Object
- Puma::Events
- Defined in:
- lib/puma/events.rb
Overview
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.
Defined Under Namespace
Classes: DefaultFormatter, PidFormatter
Constant Summary collapse
- DEFAULT =
new(STDOUT, STDERR)
Instance Attribute Summary collapse
-
#formatter ⇒ Object
Returns the value of attribute formatter.
-
#stderr ⇒ Object
readonly
Returns the value of attribute stderr.
-
#stdout ⇒ Object
readonly
Returns the value of attribute stdout.
Class Method Summary collapse
- .null ⇒ Object
- .stdio ⇒ Object
-
.strings ⇒ Object
Returns an Events object which writes its status to 2 StringIO objects.
Instance Method Summary collapse
-
#connection_error(error, req, text = "HTTP connection error") ⇒ Object
An HTTP connection error has occurred.
- #debug(str) ⇒ Object
-
#debug_error(error, req = nil, text = "") ⇒ Object
Log occurred error debug dump.
-
#error(str) ⇒ Object
Write
str
to @stderr. -
#fire(hook, *args) ⇒ Object
Fire callbacks for the named hook.
- #fire_on_booted! ⇒ Object
- #fire_on_restart! ⇒ Object
- #fire_on_stopped! ⇒ Object
- #format(str) ⇒ Object
-
#initialize(stdout, stderr) ⇒ Events
constructor
Create an Events object that prints to
stdout
andstderr
. -
#log(str) ⇒ Object
Write
str
to @stdout. - #on_booted(&block) ⇒ Object
- #on_restart(&block) ⇒ Object
- #on_stopped(&block) ⇒ Object
-
#parse_error(error, req) ⇒ Object
An HTTP parse error has occurred.
-
#register(hook, obj = nil, &blk) ⇒ Object
Register a callback for a given hook.
-
#ssl_error(error, ssl_socket) ⇒ Object
An SSL error has occurred.
-
#unknown_error(error, req = nil, text = "Unknown error") ⇒ Object
An unknown error has occurred.
- #write(str) ⇒ Object
Constructor Details
#initialize(stdout, stderr) ⇒ Events
Create an Events object that prints to stdout
and stderr
.
28 29 30 31 32 33 34 35 36 37 |
# File 'lib/puma/events.rb', line 28 def initialize(stdout, stderr) @formatter = DefaultFormatter.new @stdout = stdout @stderr = stderr @debug = ENV.key? 'PUMA_DEBUG' @error_logger = ErrorLogger.new(@stderr) @hooks = Hash.new { |h,k| h[k] = [] } end |
Instance Attribute Details
#formatter ⇒ Object
Returns the value of attribute formatter.
40 41 42 |
# File 'lib/puma/events.rb', line 40 def formatter @formatter end |
#stderr ⇒ Object (readonly)
Returns the value of attribute stderr.
39 40 41 |
# File 'lib/puma/events.rb', line 39 def stderr @stderr end |
#stdout ⇒ Object (readonly)
Returns the value of attribute stdout.
39 40 41 |
# File 'lib/puma/events.rb', line 39 def stdout @stdout end |
Class Method Details
.null ⇒ Object
172 173 174 175 |
# File 'lib/puma/events.rb', line 172 def self.null n = NullIO.new Events.new n, n end |
Instance Method Details
#connection_error(error, req, text = "HTTP connection error") ⇒ Object
An HTTP connection error has occurred. error
a connection exception, req
the request, and text
additional info
95 96 97 |
# 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) ⇒ Object
75 76 77 |
# File 'lib/puma/events.rb', line 75 def debug(str) log("% #{str}") if @debug end |
#debug_error(error, req = nil, text = "") ⇒ Object
Log occurred error debug dump. error
an exception object, req
the request, and text
additional info
131 132 133 |
# 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) ⇒ Object
Write str
to @stderr
81 82 83 84 |
# File 'lib/puma/events.rb', line 81 def error(str) @error_logger.info(text: format("ERROR: #{str}")) exit 1 end |
#fire(hook, *args) ⇒ Object
Fire callbacks for the named hook
44 45 46 |
# File 'lib/puma/events.rb', line 44 def fire(hook, *args) @hooks[hook].each { |t| t.call(*args) } end |
#fire_on_booted! ⇒ Object
147 148 149 |
# File 'lib/puma/events.rb', line 147 def fire_on_booted! fire(:on_booted) end |
#fire_on_restart! ⇒ Object
151 152 153 |
# File 'lib/puma/events.rb', line 151 def fire_on_restart! fire(:on_restart) end |
#fire_on_stopped! ⇒ Object
155 156 157 |
# File 'lib/puma/events.rb', line 155 def fire_on_stopped! fire(:on_stopped) end |
#format(str) ⇒ Object
86 87 88 |
# File 'lib/puma/events.rb', line 86 def format(str) formatter.call(str) end |
#log(str) ⇒ Object
Write str
to @stdout
64 65 66 67 68 69 |
# File 'lib/puma/events.rb', line 64 def log(str) @stdout.puts format(str) if @stdout.respond_to? :puts @stdout.flush unless @stdout.sync rescue Errno::EPIPE end |
#on_booted(&block) ⇒ Object
135 136 137 |
# File 'lib/puma/events.rb', line 135 def on_booted(&block) register(:on_booted, &block) end |
#on_restart(&block) ⇒ Object
139 140 141 |
# File 'lib/puma/events.rb', line 139 def on_restart(&block) register(:on_restart, &block) end |
#on_stopped(&block) ⇒ Object
143 144 145 |
# File 'lib/puma/events.rb', line 143 def on_stopped(&block) register(:on_stopped, &block) end |
#parse_error(error, req) ⇒ Object
An HTTP parse error has occurred. error
a parsing exception, and req
the request.
103 104 105 |
# 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) ⇒ Object
Register a callback for a given hook
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/puma/events.rb', line 50 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) ⇒ Object
An SSL error has occurred.
111 112 113 114 115 116 |
# 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") ⇒ Object
An unknown error has occurred. error
an exception object, req
the request, and text
additional info
122 123 124 |
# 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) ⇒ Object
71 72 73 |
# File 'lib/puma/events.rb', line 71 def write(str) @stdout.write format(str) end |