2012-08-01 13:11:27 -04:00
|
|
|
require 'puma/cli'
|
2012-08-02 18:03:52 -04:00
|
|
|
require 'puma/binder'
|
|
|
|
|
2012-08-01 13:11:27 -04:00
|
|
|
module Puma
|
|
|
|
class ClusterCLI < CLI
|
|
|
|
def setup_options
|
2012-08-02 18:03:52 -04:00
|
|
|
super
|
2012-08-01 13:11:27 -04:00
|
|
|
|
2012-08-02 18:03:52 -04:00
|
|
|
@options[:workers] = 2
|
2012-08-01 13:11:27 -04:00
|
|
|
|
2012-08-02 18:03:52 -04:00
|
|
|
@parser.on "-w", "--workers COUNT",
|
|
|
|
"How many worker processes to create" do |arg|
|
|
|
|
@options[:workers] = arg.to_i
|
2012-08-01 13:11:27 -04:00
|
|
|
end
|
2012-08-02 18:03:52 -04:00
|
|
|
|
|
|
|
@parser.banner = "puma cluster <options> <rackup file>"
|
2012-08-01 13:11:27 -04:00
|
|
|
end
|
|
|
|
|
2012-08-03 22:53:14 -04:00
|
|
|
def parse_options
|
|
|
|
@argv.shift if @argv.first == "cluster"
|
|
|
|
super
|
2012-08-01 13:11:27 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def worker
|
2012-08-03 22:53:14 -04:00
|
|
|
$0 = "puma cluster worker"
|
2012-08-01 13:11:27 -04:00
|
|
|
Signal.trap "SIGINT", "IGNORE"
|
|
|
|
|
2012-08-02 18:03:52 -04:00
|
|
|
@suicide_pipe.close
|
|
|
|
|
|
|
|
Thread.new do
|
|
|
|
IO.select [@check_pipe]
|
|
|
|
log "! Detected parent died, dieing"
|
|
|
|
exit! 1
|
|
|
|
end
|
|
|
|
|
2012-08-01 13:11:27 -04:00
|
|
|
min_t = @options[:min_threads]
|
|
|
|
max_t = @options[:max_threads]
|
|
|
|
|
|
|
|
server = Puma::Server.new @config.app, @events
|
|
|
|
server.min_threads = min_t
|
|
|
|
server.max_threads = max_t
|
2012-08-02 18:03:52 -04:00
|
|
|
server.binder = @binder
|
2012-08-01 13:11:27 -04:00
|
|
|
|
|
|
|
Signal.trap "SIGTERM" do
|
|
|
|
server.stop
|
|
|
|
end
|
|
|
|
|
|
|
|
server.run.join
|
|
|
|
end
|
|
|
|
|
|
|
|
def stop_workers
|
2012-08-02 18:03:52 -04:00
|
|
|
log "- Gracefully shutting down workers..."
|
2012-08-01 13:11:27 -04:00
|
|
|
@workers.each { |x| x.term }
|
2012-08-02 18:03:52 -04:00
|
|
|
|
|
|
|
begin
|
|
|
|
Process.waitall
|
|
|
|
rescue Interrupt
|
|
|
|
log "! Cancelled waiting for workers"
|
|
|
|
else
|
|
|
|
log "- Goodbye!"
|
|
|
|
end
|
2012-08-01 13:11:27 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
class Worker
|
|
|
|
def initialize(pid)
|
|
|
|
@pid = pid
|
|
|
|
end
|
|
|
|
|
|
|
|
attr_reader :pid
|
|
|
|
|
|
|
|
def term
|
|
|
|
begin
|
|
|
|
Process.kill "TERM", @pid
|
|
|
|
rescue Errno::ESRCH
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def spawn_workers
|
|
|
|
diff = @options[:workers] - @workers.size
|
|
|
|
|
|
|
|
diff.times do
|
|
|
|
pid = fork { worker }
|
|
|
|
debug "Spawned worker: #{pid}"
|
|
|
|
@workers << Worker.new(pid)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_workers
|
|
|
|
while true
|
|
|
|
pid = Process.waitpid(-1, Process::WNOHANG)
|
|
|
|
break unless pid
|
|
|
|
|
|
|
|
@workers.delete_if { |w| w.pid == pid }
|
|
|
|
end
|
|
|
|
|
|
|
|
spawn_workers
|
|
|
|
end
|
|
|
|
|
|
|
|
def run
|
|
|
|
@workers = []
|
2012-08-02 18:03:52 -04:00
|
|
|
|
2012-08-01 13:11:27 -04:00
|
|
|
@events = PidEvents.new STDOUT, STDERR
|
|
|
|
|
|
|
|
@options[:logger] = @events
|
|
|
|
|
|
|
|
parse_options
|
|
|
|
|
|
|
|
set_rack_environment
|
|
|
|
|
2012-08-02 18:03:52 -04:00
|
|
|
write_pid
|
|
|
|
write_state
|
2012-08-01 13:11:27 -04:00
|
|
|
|
|
|
|
log "Puma #{Puma::Const::PUMA_VERSION} starting in cluster mode..."
|
|
|
|
log "* Process workers: #{@options[:workers]}"
|
|
|
|
log "* Min threads: #{@options[:min_threads]}, max threads: #{@options[:max_threads]}"
|
|
|
|
log "* Environment: #{ENV['RACK_ENV']}"
|
|
|
|
|
2012-08-02 18:03:52 -04:00
|
|
|
@binder.parse @options[:binds], self
|
|
|
|
|
2012-08-01 13:11:27 -04:00
|
|
|
read, write = IO.pipe
|
|
|
|
|
|
|
|
Signal.trap "SIGCHLD" do
|
|
|
|
write.write "!"
|
|
|
|
end
|
|
|
|
|
2012-08-03 22:53:14 -04:00
|
|
|
stop = false
|
|
|
|
|
|
|
|
begin
|
|
|
|
Signal.trap "SIGUSR2" do
|
|
|
|
@restart = true
|
|
|
|
stop = true
|
|
|
|
write.write "!"
|
|
|
|
end
|
|
|
|
rescue Exception
|
|
|
|
end
|
|
|
|
|
|
|
|
begin
|
|
|
|
Signal.trap "SIGTERM" do
|
|
|
|
stop = true
|
|
|
|
write.write "!"
|
|
|
|
end
|
|
|
|
rescue Exception
|
|
|
|
end
|
|
|
|
|
2012-08-02 18:03:52 -04:00
|
|
|
# Used by the workers to detect if the master process dies.
|
|
|
|
# If select says that @check_pipe is ready, it's because the
|
|
|
|
# master has exited and @suicide_pipe has been automatically
|
|
|
|
# closed.
|
|
|
|
#
|
|
|
|
@check_pipe, @suicide_pipe = IO.pipe
|
|
|
|
|
2012-08-01 13:11:27 -04:00
|
|
|
spawn_workers
|
|
|
|
|
2012-08-03 22:53:14 -04:00
|
|
|
log "* Use Ctrl-C to stop"
|
2012-08-02 18:03:52 -04:00
|
|
|
|
2012-08-01 13:11:27 -04:00
|
|
|
begin
|
2012-08-03 22:53:14 -04:00
|
|
|
while !stop
|
|
|
|
begin
|
|
|
|
IO.select([read], nil, nil, 5)
|
|
|
|
check_workers
|
|
|
|
rescue Interrupt
|
|
|
|
stop = true
|
|
|
|
end
|
2012-08-01 13:11:27 -04:00
|
|
|
end
|
2012-08-03 22:53:14 -04:00
|
|
|
|
2012-08-01 13:11:27 -04:00
|
|
|
stop_workers
|
2012-08-02 18:03:52 -04:00
|
|
|
ensure
|
|
|
|
delete_pidfile
|
2012-08-01 13:11:27 -04:00
|
|
|
end
|
2012-08-03 22:53:14 -04:00
|
|
|
|
|
|
|
if @restart
|
|
|
|
log "* Restarting..."
|
|
|
|
restart!
|
|
|
|
end
|
2012-08-01 13:11:27 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|