123456789_123456789_123456789_123456789_123456789_

Class: Puma::App::Status

Relationships & Source Files
Inherits: Object
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

Class Method Summary

Instance Method Summary

Constructor Details

.new(launcher, token = nil) ⇒ Status

Parameters:

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

    the token used for authentication

[ GitHub ]

  
# File 'lib/puma/app/status.rb', line 14

def initialize(launcher, token = nil)
  @launcher = launcher
  @auth_token = token
end

Instance Method Details

#authenticate(env) (private)

[ GitHub ]

  
# File 'lib/puma/app/status.rb', line 81

def authenticate(env)
  return true unless @auth_token
  env['QUERY_STRING'].to_s.split('&;').include? "token=#{@auth_token}"
end

#call(env)

most commands call methods in Launcher based on command in env['PATH_INFO']

[ GitHub ]

  
# File 'lib/puma/app/status.rb', line 21

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
  resp_type =
    case env['PATH_INFO'][/\/([^\/]+)$/, 1]
    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

#rack_response(status, body, content_type = 'application/json') (private)

[ GitHub ]

  
# File 'lib/puma/app/status.rb', line 86

def rack_response(status, body, content_type='application/json')
  headers = {
    'content-type' => content_type,
    'content-length' => body.bytesize.to_s
  }

  [status, headers, [body]]
end