1
0
Fork 0
mirror of https://github.com/puma/puma.git synced 2022-11-09 13:48:40 -05:00

Add worker indexes, expose them via on_worker_boot. Fixes #440

This commit is contained in:
Evan Phoenix 2014-01-25 13:53:02 -08:00
parent 2532928ed9
commit ff0d3b553d

View file

@ -7,6 +7,7 @@ module Puma
@phase = 0 @phase = 0
@workers = [] @workers = []
@worker_index = 0
@phased_state = :idle @phased_state = :idle
@phased_restart = false @phased_restart = false
@ -29,14 +30,15 @@ module Puma
end end
class Worker class Worker
def initialize(pid, phase) def initialize(idx, pid, phase)
@index = idx
@pid = pid @pid = pid
@phase = phase @phase = phase
@stage = :started @stage = :started
@signal = "TERM" @signal = "TERM"
end end
attr_reader :pid, :phase, :signal attr_reader :index, :pid, :phase, :signal
def booted? def booted?
@stage == :booted @stage == :booted
@ -68,9 +70,11 @@ module Puma
master = Process.pid master = Process.pid
diff.times do diff.times do
pid = fork { worker(upgrade, master) } idx = next_worker_index
pid = fork { worker(idx, upgrade, master) }
@cli.debug "Spawned worker: #{pid}" @cli.debug "Spawned worker: #{pid}"
@workers << Worker.new(pid, @phase) @workers << Worker.new(idx, pid, @phase)
end end
if diff > 0 if diff > 0
@ -78,6 +82,12 @@ module Puma
end end
end end
def next_worker_index
i = @worker_index
@worker_index += 1
i
end
def all_workers_booted? def all_workers_booted?
@workers.count { |w| !w.booted? } == 0 @workers.count { |w| !w.booted? } == 0
end end
@ -118,8 +128,8 @@ module Puma
end end
end end
def worker(upgrade, master) def worker(index, upgrade, master)
$0 = "puma: cluster worker: #{master}" $0 = "puma: cluster worker #{index}: #{master}"
Signal.trap "SIGINT", "IGNORE" Signal.trap "SIGINT", "IGNORE"
@master_read.close @master_read.close
@ -143,7 +153,7 @@ module Puma
# Invoke any worker boot hooks so they can get # Invoke any worker boot hooks so they can get
# things in shape before booting the app. # things in shape before booting the app.
hooks = @options[:worker_boot] hooks = @options[:worker_boot]
hooks.each { |h| h.call } hooks.each { |h| h.call(index) }
server = start_server server = start_server
@ -297,7 +307,7 @@ module Puma
w = @workers.find { |x| x.pid == pid } w = @workers.find { |x| x.pid == pid }
if w if w
w.boot! w.boot!
log "- Worker #{pid} booted, phase: #{w.phase}" log "- Worker #{w.index} (pid: #{pid}) booted, phase: #{w.phase}"
else else
log "! Out-of-sync worker list, no #{pid} worker" log "! Out-of-sync worker list, no #{pid} worker"
end end