Class: Puma::Events

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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
38
39
40
# File 'lib/puma/events.rb', line 28

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

Instance Attribute Details

#formatterObject

Returns the value of attribute formatter.



43
44
45
# File 'lib/puma/events.rb', line 43

def formatter
  @formatter
end

#stderrObject (readonly)

Returns the value of attribute stderr.



42
43
44
# File 'lib/puma/events.rb', line 42

def stderr
  @stderr
end

#stdoutObject (readonly)

Returns the value of attribute stdout.



42
43
44
# File 'lib/puma/events.rb', line 42

def stdout
  @stdout
end

Class Method Details

.nullObject



173
174
175
176
# File 'lib/puma/events.rb', line 173

def self.null
  n = NullIO.new
  Events.new n, n
end

.stdioObject



169
170
171
# File 'lib/puma/events.rb', line 169

def self.stdio
  Events.new $stdout, $stderr
end

.stringsObject

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



165
166
167
# File 'lib/puma/events.rb', line 165

def self.strings
  Events.new StringIO.new, StringIO.new
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

Version:

  • 5.0.0



96
97
98
# File 'lib/puma/events.rb', line 96

def connection_error(error, req, text="HTTP connection error")
  @error_logger.info(error: error, req: req, text: text)
end

#debug(str) ⇒ Object



76
77
78
# File 'lib/puma/events.rb', line 76

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

Version:

  • 5.0.0



132
133
134
# File 'lib/puma/events.rb', line 132

def debug_error(error, req=nil, text="")
  @error_logger.debug(error: error, req: req, text: text)
end

#error(str) ⇒ Object

Write str to @stderr



82
83
84
85
# File 'lib/puma/events.rb', line 82

def error(str)
  @error_logger.info(text: format("ERROR: #{str}"))
  exit 1
end

#fire(hook, *args) ⇒ Object

Fire callbacks for the named hook



47
48
49
# File 'lib/puma/events.rb', line 47

def fire(hook, *args)
  @hooks[hook].each { |t| t.call(*args) }
end

#fire_on_booted!Object



148
149
150
# File 'lib/puma/events.rb', line 148

def fire_on_booted!
  fire(:on_booted)
end

#fire_on_restart!Object



152
153
154
# File 'lib/puma/events.rb', line 152

def fire_on_restart!
  fire(:on_restart)
end

#fire_on_stopped!Object



156
157
158
# File 'lib/puma/events.rb', line 156

def fire_on_stopped!
  fire(:on_stopped)
end

#format(str) ⇒ Object



87
88
89
# File 'lib/puma/events.rb', line 87

def format(str)
  formatter.call(str)
end

#log(str) ⇒ Object

Write str to @stdout



67
68
69
70
# File 'lib/puma/events.rb', line 67

def log(str)
  @stdout.puts format(str) if @stdout.respond_to? :puts
rescue Errno::EPIPE
end

#on_booted(&block) ⇒ Object



136
137
138
# File 'lib/puma/events.rb', line 136

def on_booted(&block)
  register(:on_booted, &block)
end

#on_restart(&block) ⇒ Object



140
141
142
# File 'lib/puma/events.rb', line 140

def on_restart(&block)
  register(:on_restart, &block)
end

#on_stopped(&block) ⇒ Object



144
145
146
# File 'lib/puma/events.rb', line 144

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.



104
105
106
# File 'lib/puma/events.rb', line 104

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



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/puma/events.rb', line 53

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.

Parameters:



112
113
114
115
116
117
# File 'lib/puma/events.rb', line 112

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



123
124
125
# File 'lib/puma/events.rb', line 123

def unknown_error(error, req=nil, text="Unknown error")
  @error_logger.info(error: error, req: req, text: text)
end

#write(str) ⇒ Object



72
73
74
# File 'lib/puma/events.rb', line 72

def write(str)
  @stdout.write format(str)
end