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


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

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

.stdioObject


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

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

.stringsObject

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


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

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


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


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


128
129
130
# File 'lib/puma/events.rb', line 128

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


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

def fire_on_booted!
  fire(:on_booted)
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


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

def on_booted(&block)
  register(:on_booted, &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


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, peeraddr, peercert) ⇒ Object

An SSL error has occurred. error an exception object, peeraddr peer address, and peercert any peer certificate (if present).


111
112
113
114
# File 'lib/puma/events.rb', line 111

def ssl_error(error, peeraddr, 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


120
121
122
# File 'lib/puma/events.rb', line 120

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