def run
@status = :run
output_header "cluster"
log "* Process workers: #{@options[:workers]}"
if preload?
log "* Preloading application"
load_and_bind
else
log "* Phased restart available"
unless @cli.config.app_configured?
error "No application configured, nothing to run"
exit 1
end
@cli.binder.parse @options[:binds], self
end
read, @wakeup = Puma::Util.pipe
Signal.trap "SIGCHLD" do
wakeup!
end
master_pid = Process.pid
Signal.trap "SIGTERM" do
if Process.pid != master_pid
log "Early termination of worker"
exit! 0
else
stop
end
end
@check_pipe, @suicide_pipe = Puma::Util.pipe
if daemon?
log "* Daemonizing..."
Process.daemon(true)
else
log "Use Ctrl-C to stop"
end
redirect_io
start_control
@cli.write_state
@master_read, @worker_write = read, @wakeup
spawn_workers
Signal.trap "SIGINT" do
stop
end
@cli.events.fire_on_booted!
begin
while @status == :run
begin
res = IO.select([read], nil, nil, 5)
if res
req = read.read_nonblock(1)
if req == "b"
pid = read.gets.to_i
w = @workers.find { |x| x.pid == pid }
if w
w.boot!
log "- Worker #{pid} booted, phase: #{w.phase}"
else
log "! Out-of-sync worker list, no #{pid} worker"
end
end
end
if @phased_restart
start_phased_restart
@phased_restart = false
end
check_workers
rescue Interrupt
@status = :stop
end
end
stop_workers unless @status == :halt
ensure
@check_pipe.close
@suicide_pipe.close
read.close
@wakeup.close
end
end