2019-07-16 18:53:28 -04:00
|
|
|
# frozen_string_literal: true
|
2021-06-09 10:55:45 -04:00
|
|
|
require 'puma/json_serialization'
|
2019-07-16 18:53:28 -04:00
|
|
|
|
2011-12-05 12:01:19 -05:00
|
|
|
module Puma
|
|
|
|
module App
|
2019-08-05 20:30:43 -04:00
|
|
|
# Check out {#call}'s source code to see what actions this web application
|
|
|
|
# can respond to.
|
2011-12-05 12:01:19 -05:00
|
|
|
class Status
|
2012-04-29 15:18:21 -04:00
|
|
|
OK_STATUS = '{ "status": "ok" }'.freeze
|
2011-12-06 19:22:02 -05:00
|
|
|
|
2020-10-25 16:49:38 -04:00
|
|
|
# @param launcher [::Puma::Launcher]
|
|
|
|
# @param token [String, nil] the token used for authentication
|
|
|
|
#
|
|
|
|
def initialize(launcher, token = nil)
|
|
|
|
@launcher = launcher
|
2019-09-20 06:45:56 -04:00
|
|
|
@auth_token = token
|
2013-07-05 19:08:13 -04:00
|
|
|
end
|
|
|
|
|
2020-10-25 16:49:38 -04:00
|
|
|
# most commands call methods in `::Puma::Launcher` based on command in
|
|
|
|
# `env['PATH_INFO']`
|
2011-12-05 12:01:19 -05:00
|
|
|
def call(env)
|
2011-12-06 19:22:02 -05:00
|
|
|
unless authenticate(env)
|
2012-04-29 15:24:00 -04:00
|
|
|
return rack_response(403, 'Invalid auth token', 'text/plain')
|
2011-12-06 19:22:02 -05:00
|
|
|
end
|
|
|
|
|
2020-10-25 16:49:38 -04:00
|
|
|
# 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
|
2011-12-05 12:01:19 -05:00
|
|
|
|
2020-10-25 16:49:38 -04:00
|
|
|
when 'halt'
|
|
|
|
@launcher.halt ; 200
|
2011-12-05 12:01:19 -05:00
|
|
|
|
2020-10-25 16:49:38 -04:00
|
|
|
when 'restart'
|
|
|
|
@launcher.restart ; 200
|
2012-04-04 11:38:22 -04:00
|
|
|
|
2020-10-25 16:49:38 -04:00
|
|
|
when 'phased-restart'
|
|
|
|
@launcher.phased_restart ? 200 : 404
|
2014-02-25 08:52:20 -05:00
|
|
|
|
2020-10-25 16:49:38 -04:00
|
|
|
when 'reload-worker-directory'
|
|
|
|
@launcher.send(:reload_worker_directory) ? 200 : 404
|
2017-08-03 17:46:49 -04:00
|
|
|
|
2020-10-25 16:49:38 -04:00
|
|
|
when 'gc'
|
|
|
|
GC.start ; 200
|
2017-08-03 17:46:49 -04:00
|
|
|
|
2020-10-25 16:49:38 -04:00
|
|
|
when 'gc-stats'
|
2021-06-09 10:55:45 -04:00
|
|
|
Puma::JSONSerialization.generate GC.stat
|
2019-11-11 00:08:41 -05:00
|
|
|
|
2020-10-25 16:49:38 -04:00
|
|
|
when 'stats'
|
2021-06-09 10:55:45 -04:00
|
|
|
Puma::JSONSerialization.generate @launcher.stats
|
2019-11-11 00:08:41 -05:00
|
|
|
|
2020-10-25 16:49:38 -04:00
|
|
|
when 'thread-backtraces'
|
|
|
|
backtraces = []
|
|
|
|
@launcher.thread_status do |name, backtrace|
|
|
|
|
backtraces << { name: name, backtrace: backtrace }
|
|
|
|
end
|
2021-06-09 10:55:45 -04:00
|
|
|
Puma::JSONSerialization.generate backtraces
|
2020-05-01 18:44:58 -04:00
|
|
|
|
2020-10-25 16:49:38 -04:00
|
|
|
else
|
|
|
|
return rack_response(404, "Unsupported action", 'text/plain')
|
|
|
|
end
|
2020-05-01 18:44:58 -04:00
|
|
|
|
2020-10-25 16:49:38 -04:00
|
|
|
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\" }"
|
2011-12-05 12:01:19 -05:00
|
|
|
end
|
|
|
|
end
|
2019-09-20 06:45:56 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def authenticate(env)
|
|
|
|
return true unless @auth_token
|
|
|
|
env['QUERY_STRING'].to_s.split(/&;/).include?("token=#{@auth_token}")
|
|
|
|
end
|
|
|
|
|
|
|
|
def rack_response(status, body, content_type='application/json')
|
|
|
|
headers = {
|
|
|
|
'Content-Type' => content_type,
|
|
|
|
'Content-Length' => body.bytesize.to_s
|
|
|
|
}
|
|
|
|
|
|
|
|
[status, headers, [body]]
|
|
|
|
end
|
2011-12-05 12:01:19 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|