Class: Puma::App::Status

Inherits:
Object
  • Object
show all
Defined in:
lib/puma/app/status.rb

Overview

Check out #call‘s source code to see what actions this web application can respond to.

Constant Summary collapse

OK_STATUS =
'{ "status": "ok" }'.freeze
READ_ONLY_COMMANDS =
%w[gc-stats stats].freeze

Instance Method Summary collapse

Constructor Details

#initialize(launcher, token: nil, data_only: false) ⇒ Status

Returns a new instance of Status.

Parameters:

  • launcher (::Puma::Launcher)
  • token (String, nil) (defaults to: nil)

    the token used for authentication

  • data_only (Boolean) (defaults to: false)

    if true, restrict to read-only data commands



16
17
18
19
20
# File 'lib/puma/app/status.rb', line 16

def initialize(launcher, token: nil, data_only: false)
  @launcher = launcher
  @auth_token = token
  @enabled_commands = READ_ONLY_COMMANDS if data_only
end

Instance Method Details

#call(env) ⇒ Object

most commands call methods in ::Puma::Launcher based on command in ‘env`



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/puma/app/status.rb', line 24

def call(env)
  unless authenticate(env)
    return rack_response(403, 'Invalid auth token', 'text/plain')
  end

  # resp_type is processed by following case statement, return
  # is a number (status) or a string used as the body of a 200 response
  command = env['PATH_INFO'][/\/([^\/]+)$/, 1]
  if @enabled_commands && !@enabled_commands.include?(command)
    return rack_response(404, "Command #{command.inspect} unavailable", 'text/plain')
  end

  resp_type =
    case command
    when 'stop'
      @launcher.stop ; 200

    when 'halt'
      @launcher.halt ; 200

    when 'restart'
      @launcher.restart ; 200

    when 'phased-restart'
      @launcher.phased_restart ? 200 : 404

    when 'refork'
      @launcher.refork ? 200 : 404

    when 'reload-worker-directory'
      @launcher.send(:reload_worker_directory) ? 200 : 404

    when 'gc'
      GC.start ; 200

    when 'gc-stats'
      Puma::JSONSerialization.generate GC.stat

    when 'stats'
      Puma::JSONSerialization.generate @launcher.stats

    when 'thread-backtraces'
      backtraces = []
      @launcher.thread_status do |name, backtrace|
        backtraces << { name: name, backtrace: backtrace }
      end
      Puma::JSONSerialization.generate backtraces

    else
      return rack_response(404, "Unsupported action", 'text/plain')
    end

  case resp_type
  when String
    rack_response 200, resp_type
  when 200
    rack_response 200, OK_STATUS
  when 404
    str = env['PATH_INFO'][/\/(\S+)/, 1].tr '-', '_'
    rack_response 404, "{ \"error\": \"#{str} not available\" }"
  end
end