Class: Puma::CLI
- Inherits:
-
Object
- Object
- Puma::CLI
- Defined in:
- lib/puma/cli.rb
Overview
Handles invoke a Puma::Server in a command line style.
Constant Summary collapse
- DefaultTCPHost =
"0.0.0.0"
- DefaultTCPPort =
9292
- IS_JRUBY =
defined?(JRUBY_VERSION)
Instance Method Summary collapse
-
#error(str) ⇒ Object
Write
str
to @stderr. - #generate_restart_data ⇒ Object
-
#initialize(argv, stdout = STDOUT, stderr = STDERR) ⇒ CLI
constructor
Create a new CLI object using
argv
as the command line arguments. -
#load_rackup ⇒ Object
Load the specified rackup file, pull an options from the rackup file, and set @app.
-
#log(str) ⇒ Object
Write
str
to @stdout. -
#parse_options ⇒ Object
:nodoc:.
- #restart! ⇒ Object
- #restart_on_stop! ⇒ Object
-
#run ⇒ Object
Parse the options, load the rackup, start the server and wait for it to finish.
-
#setup_options ⇒ Object
Build the OptionParser object to handle the available options.
- #stop ⇒ Object
-
#write_pid ⇒ Object
If configured, write the pid of the current process out to a file.
- #write_state ⇒ Object
Constructor Details
#initialize(argv, stdout = STDOUT, stderr = STDERR) ⇒ CLI
Create a new CLI object using argv
as the command line arguments.
stdout
and stderr
can be set to IO-like objects which this object will report status on.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/puma/cli.rb', line 24 def initialize(argv, stdout=STDOUT, stderr=STDERR) @argv = argv @stdout = stdout @stderr = stderr @events = Events.new @stdout, @stderr @server = nil @status = nil @restart = false @temp_status_path = nil generate_restart_data end |
Instance Method Details
#error(str) ⇒ Object
Write str
to @stderr
96 97 98 99 |
# File 'lib/puma/cli.rb', line 96 def error(str) @stderr.puts "ERROR: #{str}" exit 1 end |
#generate_restart_data ⇒ Object
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 |
# File 'lib/puma/cli.rb', line 51 def generate_restart_data # Use the same trick as unicorn, namely favor PWD because # it will contain an unresolved symlink, useful for when # the pwd is /data/releases/current. if dir = ENV['PWD'] s_env = File.stat(dir) s_pwd = File.stat(Dir.pwd) if s_env.ino == s_pwd.ino and s_env.dev == s_pwd.dev @restart_dir = dir end end @restart_dir ||= Dir.pwd if defined? Rubinius::OS_ARGV @restart_argv = Rubinius::OS_ARGV else require 'rubygems' # if $0 is a file in the current directory, then restart # it the same, otherwise add -S on there because it was # picked up in PATH. # if File.exists?($0) @restart_argv = [Gem.ruby, $0] + ARGV else @restart_argv = [Gem.ruby, "-S", $0] + ARGV end end end |
#load_rackup ⇒ Object
Load the specified rackup file, pull an options from the rackup file, and set @app.
170 171 172 173 174 175 176 177 178 179 |
# File 'lib/puma/cli.rb', line 170 def load_rackup @app, = Rack::Builder.parse_file @rackup @options.merge! .each do |key,val| if key.to_s[0,4] == "bind" @binds << val end end end |
#log(str) ⇒ Object
Write str
to @stdout
90 91 92 |
# File 'lib/puma/cli.rb', line 90 def log(str) @stdout.puts str end |
#parse_options ⇒ Object
:nodoc:
209 210 211 |
# File 'lib/puma/cli.rb', line 209 def @parser.parse! @argv end |
#restart! ⇒ Object
83 84 85 86 |
# File 'lib/puma/cli.rb', line 83 def restart! Dir.chdir @restart_dir Kernel.exec(*@restart_argv) end |
#restart_on_stop! ⇒ Object
42 43 44 45 46 47 48 49 |
# File 'lib/puma/cli.rb', line 42 def restart_on_stop! if @restart_argv @restart = true return true else return false end end |
#run ⇒ Object
Parse the options, load the rackup, start the server and wait for it to finish.
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 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 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 |
# File 'lib/puma/cli.rb', line 216 def run @rackup = @argv.shift || "config.ru" unless File.exists?(@rackup) raise "Missing rackup file '#{@rackup}'" end load_rackup write_pid write_state unless @options[:quiet] @app = Rack::CommonLogger.new(@app, STDOUT) end if @binds.empty? @options[:Host] ||= DefaultTCPHost @options[:Port] ||= DefaultTCPPort end min_t = @options[:min_threads] max_t = @options[:max_threads] server = Puma::Server.new @app, @events server.min_threads = min_t server.max_threads = max_t log "Puma #{Puma::Const::PUMA_VERSION} starting..." log "* Min threads: #{min_t}, max threads: #{max_t}" if @options[:Host] log "* Listening on tcp://#{@options[:Host]}:#{@options[:Port]}" server.add_tcp_listener @options[:Host], @options[:Port] end @binds.each do |str| uri = URI.parse str case uri.scheme when "tcp" log "* Listening on #{str}" server.add_tcp_listener uri.host, uri.port when "unix" log "* Listening on #{str}" path = "#{uri.host}#{uri.path}" server.add_unix_listener path else error "Invalid URI: #{str}" end end @server = server if str = @options[:status_address] require 'puma/app/status' uri = URI.parse str app = Puma::App::Status.new server, self status = Puma::Server.new app, @events status.min_threads = 0 status.max_threads = 1 case uri.scheme when "tcp" log "* Starting status server on #{str}" status.add_tcp_listener uri.host, uri.port when "unix" log "* Starting status server on #{str}" path = "#{uri.host}#{uri.path}" status.add_unix_listener path else error "Invalid status URI: #{str}" end status.run @status = status end log "Use Ctrl-C to stop" begin server.run.join rescue Interrupt log " - Gracefully stopping, waiting for requests to finish" server.stop(true) log " - Goodbye!" end File.unlink @temp_status_path if @temp_status_path if @restart log "* Restarting..." restart! end end |
#setup_options ⇒ Object
Build the OptionParser object to handle the available options.
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 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/puma/cli.rb', line 103 def @options = { :min_threads => 0, :max_threads => 16, :quiet => false } @binds = [] @parser = OptionParser.new do |o| o.on "-b", "--bind URI", "URI to bind to (tcp:// and unix:// only)" do |arg| @binds << arg end o.on "--pidfile PATH", "Use PATH as a pidfile" do |arg| @options[:pidfile] = arg end o.on "-q", "--quiet", "Quiet down the output" do @options[:quiet] = true end o.on '-t', '--threads INT', "min:max threads to use (default 0:16)" do |arg| min, max = arg.split(":") if max @options[:min_threads] = min.to_i @options[:max_threads] = max.to_i else @options[:min_threads] = 0 @options[:max_threads] = arg.to_i end end o.on "-S", "--state PATH", "Where to store the state details" do |arg| @options[:state] = arg end o.on "--status [URL]", "The bind url to use for the status server" do |arg| if arg and arg != "@" @options[:status_address] = arg elsif IS_JRUBY raise NotImplementedError, "No default url available on JRuby" else require 'tmpdir' t = (Time.now.to_f * 1000).to_i path = "#{Dir.tmpdir}/puma-status-#{t}-#{$$}" @temp_status_path = path @options[:status_address] = "unix://#{path}" end end end @parser. = "puma <options> <rackup file>" @parser.on_tail "-h", "--help", "Show help" do log @parser exit 1 end end |
#stop ⇒ Object
316 317 318 |
# File 'lib/puma/cli.rb', line 316 def stop @server.stop(true) if @server end |
#write_pid ⇒ Object
If configured, write the pid of the current process out to a file.
184 185 186 187 188 189 190 |
# File 'lib/puma/cli.rb', line 184 def write_pid if path = @options[:pidfile] File.open(path, "w") do |f| f.puts Process.pid end end end |
#write_state ⇒ Object
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/puma/cli.rb', line 192 def write_state require 'yaml' if path = @options[:state] state = { "pid" => Process.pid } if url = @options[:status_address] state["status_address"] = url end File.open(path, "w") do |f| f.write state.to_yaml end end end |