Class: Puma::Events

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

Constants included from Const

Const::CGI_VER, Const::CHUNKED, Const::CHUNK_SIZE, Const::CLOSE, Const::CLOSE_CHUNKED, Const::CODE_NAME, Const::COLON, Const::CONNECTION_CLOSE, Const::CONNECTION_KEEP_ALIVE, Const::CONTENT_LENGTH, Const::CONTENT_LENGTH2, Const::CONTENT_LENGTH_S, Const::CONTINUE, Const::ERROR_400_RESPONSE, Const::ERROR_404_RESPONSE, Const::ERROR_408_RESPONSE, Const::ERROR_500_RESPONSE, Const::ERROR_503_RESPONSE, Const::FAST_TRACK_KA_TIMEOUT, Const::FIRST_DATA_TIMEOUT, Const::GATEWAY_INTERFACE, Const::HALT_COMMAND, Const::HEAD, Const::HIJACK, Const::HIJACK_IO, Const::HIJACK_P, Const::HTTP, Const::HTTPS, Const::HTTPS_KEY, Const::HTTP_10_200, Const::HTTP_11, Const::HTTP_11_100, Const::HTTP_11_200, Const::HTTP_CONNECTION, Const::HTTP_EXPECT, Const::HTTP_HOST, Const::HTTP_VERSION, Const::HTTP_X_FORWARDED_FOR, Const::KEEP_ALIVE, Const::LINE_END, Const::LOCALHOST, Const::LOCALHOST_ADDR, Const::LOCALHOST_IP, Const::MAX_BODY, Const::MAX_HEADER, Const::NEWLINE, Const::PATH_INFO, Const::PERSISTENT_TIMEOUT, Const::PORT_443, Const::PORT_80, Const::PUMA_CONFIG, Const::PUMA_PEERCERT, Const::PUMA_SERVER_STRING, Const::PUMA_SOCKET, Const::PUMA_TMP_BASE, Const::PUMA_VERSION, Const::QUERY_STRING, Const::RACK_AFTER_REPLY, Const::RACK_INPUT, Const::RACK_URL_SCHEME, Const::REMOTE_ADDR, Const::REQUEST_METHOD, Const::REQUEST_PATH, Const::REQUEST_URI, Const::RESTART_COMMAND, Const::SERVER_NAME, Const::SERVER_PORT, Const::SERVER_PROTOCOL, Const::SERVER_SOFTWARE, Const::STOP_COMMAND, Const::TRANSFER_ENCODING, Const::TRANSFER_ENCODING2, Const::TRANSFER_ENCODING_CHUNKED, Const::WRITE_TIMEOUT

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

  @hooks = Hash.new { |h,k| h[k] = [] }
end

Instance Attribute Details

#formatterObject

Returns the value of attribute formatter.



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

def formatter
  @formatter
end

#stderrObject (readonly)

Returns the value of attribute stderr.



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

def stderr
  @stderr
end

#stdoutObject (readonly)

Returns the value of attribute stdout.



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

def stdout
  @stdout
end

Class Method Details

.nullObject



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

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

.stdioObject



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

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

.stringsObject

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



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

def self.strings
  Events.new StringIO.new, StringIO.new
end

Instance Method Details

#debug(str) ⇒ Object



74
75
76
# File 'lib/puma/events.rb', line 74

def debug(str)
  log("% #{str}") if @debug
end

#error(str) ⇒ Object

Write str to @stderr



80
81
82
83
# File 'lib/puma/events.rb', line 80

def error(str)
  @stderr.puts format("ERROR: #{str}")
  exit 1
end

#fire(hook, *args) ⇒ Object

Fire callbacks for the named hook



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

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

#fire_on_booted!Object



130
131
132
# File 'lib/puma/events.rb', line 130

def fire_on_booted!
  fire(:on_booted)
end

#format(str) ⇒ Object



85
86
87
# File 'lib/puma/events.rb', line 85

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

#log(str) ⇒ Object

Write str to @stdout



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

def log(str)
  @stdout.puts format(str)
end

#on_booted(&block) ⇒ Object



126
127
128
# File 'lib/puma/events.rb', line 126

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

#parse_error(server, env, error) ⇒ Object

An HTTP parse error has occurred. server is the Server object, env the request, and error a parsing exception.



93
94
95
96
# File 'lib/puma/events.rb', line 93

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}"
  @stderr.puts "#{Time.now}: ENV: #{env.inspect}\n---\n"
end

#register(hook, obj = nil, &blk) ⇒ Object

Register a callback for a given hook



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

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

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



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

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

#unknown_error(server, error, kind = "Unknown", env = nil) ⇒ Object

An unknown error has occurred. server is the Server object, error an exception object, kind some additional info, and env the request.



111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/puma/events.rb', line 111

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

#write(str) ⇒ Object



70
71
72
# File 'lib/puma/events.rb', line 70

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