Class: Puma::ControlCLI
- Inherits:
-
Object
- Object
- Puma::ControlCLI
- Defined in:
- lib/puma/control_cli.rb
Constant Summary collapse
- CMD_PATH_SIG_MAP =
values must be string or nil value of ‘nil` means command cannot be processed via signal
{ 'gc' => nil, 'gc-stats' => nil, 'halt' => 'SIGQUIT', 'info' => 'SIGINFO', 'phased-restart' => 'SIGUSR1', 'refork' => 'SIGURG', 'reload-worker-directory' => nil, 'reopen-log' => 'SIGHUP', 'restart' => 'SIGUSR2', 'start' => nil, 'stats' => nil, 'status' => '', 'stop' => 'SIGTERM', 'thread-backtraces' => nil, 'worker-count-down' => 'SIGTTOU', 'worker-count-up' => 'SIGTTIN' }.freeze
- NO_REQ_COMMANDS =
commands that cannot be used in a request
%w[info reopen-log worker-count-down worker-count-up].freeze
- PRINTABLE_COMMANDS =
%w[gc-stats stats thread-backtraces].freeze
Instance Method Summary collapse
-
#initialize(argv, stdout = STDOUT, stderr = STDERR, env: ENV) ⇒ ControlCLI
constructor
A new instance of ControlCLI.
- #message(msg) ⇒ Object
- #prepare_configuration ⇒ Object
- #run ⇒ Object
- #send_request ⇒ Object
- #send_signal ⇒ Object
Constructor Details
#initialize(argv, stdout = STDOUT, stderr = STDERR, env: ENV) ⇒ ControlCLI
Returns a new instance of ControlCLI.
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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/puma/control_cli.rb', line 40 def initialize(argv, stdout=STDOUT, stderr=STDERR, env: ENV) @state = nil @quiet = false @pidfile = nil @pid = nil @control_url = nil @control_auth_token = nil @config_file = nil @command = nil @environment = env['APP_ENV'] || env['RACK_ENV'] || env['RAILS_ENV'] @argv = argv.dup @stdout = stdout @stderr = stderr @cli_options = {} opts = OptionParser.new do |o| o. = "Usage: pumactl (-p PID | -P pidfile | -S status_file | -C url -T token | -F config.rb) (#{CMD_PATH_SIG_MAP.keys.join("|")})" o.on "-S", "--state PATH", "Where the state file to use is" do |arg| @state = arg end o.on "-Q", "--quiet", "Do not display messages" do |arg| @quiet = true end o.on "-P", "--pidfile PATH", "Pid file" do |arg| @pidfile = arg end o.on "-p", "--pid PID", "Pid" do |arg| @pid = arg.to_i end o.on "-C", "--control-url URL", "The bind url to use for the control server" do |arg| @control_url = arg end o.on "-T", "--control-token TOKEN", "The token to use as authentication for the control server" do |arg| @control_auth_token = arg end o.on "-F", "--config-file PATH", "Puma config script" do |arg| @config_file = arg end o.on "-e", "--environment ENVIRONMENT", "The environment to run the Rack app on (default development)" do |arg| @environment = arg end o.on_tail("-H", "--help", "Show this message") do @stdout.puts o exit end o.on_tail("-V", "--version", "Show version") do @stdout.puts Const::PUMA_VERSION exit end end opts.order!(argv) { |a| opts.terminate a } opts.parse! @command = argv.shift # check presence of command unless @command raise "Available commands: #{CMD_PATH_SIG_MAP.keys.join(", ")}" end unless CMD_PATH_SIG_MAP.key? @command raise "Invalid command: #{@command}" end unless @config_file == '-' environment = @environment || 'development' if @config_file.nil? @config_file = %W(config/puma/#{environment}.rb config/puma.rb).find do |f| File.exist?(f) end end if @config_file require_relative 'configuration' require_relative 'log_writer' config = Puma::Configuration.new({ config_files: [@config_file] }, {} , env) config.load @state ||= config.[:state] @control_url ||= config.[:control_url] @control_auth_token ||= config.[:control_auth_token] @pidfile ||= config.[:pidfile] end end rescue => e @stdout.puts e. exit 1 end |
Instance Method Details
#message(msg) ⇒ Object
143 144 145 |
# File 'lib/puma/control_cli.rb', line 143 def (msg) @stdout.puts msg unless @quiet end |
#prepare_configuration ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/puma/control_cli.rb', line 147 def prepare_configuration if @state unless File.exist? @state raise "State file not found: #{@state}" end require_relative 'state_file' sf = Puma::StateFile.new sf.load @state @control_url = sf.control_url @control_auth_token = sf.control_auth_token @pid = sf.pid elsif @pidfile # get pid from pid_file @pid = File.read(@pidfile, mode: 'rb:UTF-8').to_i end end |
#run ⇒ Object
277 278 279 280 281 282 283 284 285 286 287 288 289 290 |
# File 'lib/puma/control_cli.rb', line 277 def run return start if @command == 'start' prepare_configuration if Puma.windows? || @control_url && !NO_REQ_COMMANDS.include?(@command) send_request else send_signal end rescue => e e. exit 1 end |
#send_request ⇒ Object
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
# File 'lib/puma/control_cli.rb', line 167 def send_request uri = URI.parse @control_url host = uri.host # create server object by scheme server = case uri.scheme when 'ssl' require 'openssl' host = host[1..-2] if host&.start_with? '[' OpenSSL::SSL::SSLSocket.new( TCPSocket.new(host, uri.port), OpenSSL::SSL::SSLContext.new) .tap { |ssl| ssl.sync_close = true } # default is false .tap(&:connect) when 'tcp' host = host[1..-2] if host&.start_with? '[' TCPSocket.new host, uri.port when 'unix' # check for abstract UNIXSocket UNIXSocket.new(@control_url.start_with?('unix://@') ? "\0#{host}#{uri.path}" : "#{host}#{uri.path}") else raise "Invalid scheme: #{uri.scheme}" end if @command == 'status' 'Puma is started' else url = "/#{@command}" if @control_auth_token url = url + "?token=#{@control_auth_token}" end server.syswrite "GET #{url} HTTP/1.0\r\n\r\n" unless data = server.read raise 'Server closed connection before responding' end response = data.split("\r\n") if response.empty? raise "Server sent empty response" end @http, @code, @message = response.first.split(' ',3) if @code == '403' raise 'Unauthorized access to server (wrong auth token)' elsif @code == '404' raise "Command error: #{response.last}" elsif @code != '200' raise "Bad response from server: #{@code}" end "Command #{@command} sent success" response.last if PRINTABLE_COMMANDS.include?(@command) end ensure if server if uri.scheme == 'ssl' server.sysclose else server.close unless server.closed? end end end |
#send_signal ⇒ Object
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 |
# File 'lib/puma/control_cli.rb', line 238 def send_signal unless @pid raise 'Neither pid nor control url available' end begin sig = CMD_PATH_SIG_MAP[@command] if sig.nil? @stdout.puts "'#{@command}' not available via pid only" @stdout.flush unless @stdout.sync return elsif sig.start_with? 'SIG' if Signal.list.key? sig.sub(/\ASIG/, '') Process.kill sig, @pid else raise "Signal '#{sig}' not available'" end elsif @command == 'status' begin Process.kill 0, @pid @stdout.puts 'Puma is started' @stdout.flush unless @stdout.sync rescue Errno::ESRCH raise 'Puma is not running' end return end rescue SystemCallError if @command == 'restart' start else raise "No pid '#{@pid}' found" end end "Command #{@command} sent success" end |