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.

Direct Known Subclasses

PidEvents

Constant Summary collapse

DEFAULT =
new(STDOUT, STDERR)

Constants included from Const

Const::CGI_VER, 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::CONTENT_TYPE, Const::DATE, Const::ERROR_400_RESPONSE, Const::ERROR_404_RESPONSE, Const::ERROR_500_RESPONSE, Const::ERROR_503_RESPONSE, Const::ETAG, Const::ETAG_FORMAT, Const::FAST_TRACK_KA_TIMEOUT, Const::FIRST_DATA_TIMEOUT, Const::GATEWAY_INTERFACE, Const::GET, Const::HALT_COMMAND, Const::HEAD, Const::HIJACK, Const::HIJACK_IO, Const::HIJACK_P, Const::HOST, Const::HTTP, Const::HTTPS, Const::HTTPS_KEY, Const::HTTP_10, Const::HTTP_10_200, Const::HTTP_11, Const::HTTP_11_200, Const::HTTP_CONNECTION, Const::HTTP_HOST, Const::HTTP_IF_MODIFIED_SINCE, Const::HTTP_IF_NONE_MATCH, Const::HTTP_VERSION, Const::HTTP_X_FORWARDED_FOR, Const::KEEP_ALIVE, Const::LAST_MODIFIED, Const::LINE_END, Const::LOCALHOST, 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_SOCKET, Const::PUMA_TMP_BASE, Const::PUMA_VERSION, Const::RACK_AFTER_REPLY, Const::RACK_INPUT, Const::RACK_URL_SCHEME, Const::REDIRECT, Const::REMOTE_ADDR, Const::REQUEST_METHOD, Const::REQUEST_PATH, Const::REQUEST_URI, Const::RESTART_COMMAND, Const::SCRIPT_NAME, Const::SERVER_NAME, Const::SERVER_PORT, Const::SERVER_PROTOCOL, Const::SERVER_SOFTWARE, Const::SLASH, Const::STATUS_FORMAT, Const::STOP_COMMAND, Const::TRANSFER_ENCODING, Const::TRANSFER_ENCODING_CHUNKED

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.



16
17
18
19
20
21
22
23
24
# File 'lib/puma/events.rb', line 16

def initialize(stdout, stderr)
  @stdout = stdout
  @stderr = stderr

  @stdout.sync = true
  @stderr.sync = true

  @on_booted = []
end

Instance Attribute Details

#stderrObject (readonly)

Returns the value of attribute stderr.



26
27
28
# File 'lib/puma/events.rb', line 26

def stderr
  @stderr
end

#stdoutObject (readonly)

Returns the value of attribute stdout.



26
27
28
# File 'lib/puma/events.rb', line 26

def stdout
  @stdout
end

Class Method Details

.stdioObject



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

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

.stringsObject

Returns an Events object which writes it’s status to 2 StringIO objects.



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

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

Instance Method Details

#error(str) ⇒ Object

Write str to @stderr



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

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

#fire_on_booted!Object



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

def fire_on_booted!
  @on_booted.each { |b| b.call }
end

#log(str) ⇒ Object

Write str to @stdout



30
31
32
# File 'lib/puma/events.rb', line 30

def log(str)
  @stdout.puts str
end

#on_booted(&b) ⇒ Object



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

def on_booted(&b)
  @on_booted << b
end

#parse_error(server, env, error) ⇒ Object

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



49
50
51
52
# File 'lib/puma/events.rb', line 49

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

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

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



58
59
60
61
62
63
64
65
# File 'lib/puma/events.rb', line 58

def unknown_error(server, error, kind="Unknown")
  if error.respond_to? :render
    error.render "#{Time.now}: #{kind} error", @stderr
  else
    @stderr.puts "#{Time.now}: #{kind} error: #{error.inspect}"
    @stderr.puts error.backtrace.join("\n")
  end
end

#write(str) ⇒ Object



34
35
36
# File 'lib/puma/events.rb', line 34

def write(str)
  @stdout.write str
end