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
# 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

#formatterObject

Returns the value of attribute formatter.



40
41
42
# File 'lib/puma/events.rb', line 40

def formatter
  @formatter
end

#stderrObject (readonly)

Returns the value of attribute stderr.



39
40
41
# File 'lib/puma/events.rb', line 39

def stderr
  @stderr
end

#stdoutObject (readonly)

Returns the value of attribute stdout.



39
40
41
# File 'lib/puma/events.rb', line 39

def stdout
  @stdout
end

Class Method Details

.nullObject



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

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

.stdioObject



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

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

.stringsObject

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



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

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



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

Version:

  • 5.0.0



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.

Parameters:



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