Class: Puma::Cluster
Defined Under Namespace
Classes: Worker
Instance Method Summary
collapse
Methods inherited from Runner
#app, #before_restart, #daemon?, #development?, #error, #load_and_bind, #log, #output_header, #redirect_io, #ruby_engine, #start_control, #start_server
Constructor Details
#initialize(cli) ⇒ Cluster
Returns a new instance of Cluster.
5
6
7
8
9
10
11
12
13
14
|
# File 'lib/puma/cluster.rb', line 5
def initialize(cli)
super cli
@phase = 0
@workers = []
@next_check = nil
@phased_state = :idle
@phased_restart = false
end
|
Instance Method Details
#all_workers_booted? ⇒ Boolean
113
114
115
|
# File 'lib/puma/cluster.rb', line 113
def all_workers_booted?
@workers.count { |w| !w.booted? } == 0
end
|
#check_workers ⇒ Object
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
|
# File 'lib/puma/cluster.rb', line 117
def check_workers
return if @next_check && @next_check >= Time.now
@next_check = Time.now + 5
any = false
@workers.each do |w|
if w.ping_timeout?(@options[:worker_timeout])
log "! Terminating timed out worker: #{w.pid}"
w.kill
any = true
end
end
sleep 1 if any
while @workers.any?
pid = Process.waitpid(-1, Process::WNOHANG)
break unless pid
@workers.delete_if { |w| w.pid == pid }
end
spawn_workers
if all_workers_booted?
w = @workers.find { |x| x.phase != @phase }
if w
if @phased_state == :idle
@phased_state = :waiting
log "- Stopping #{w.pid} for phased upgrade..."
end
w.term
log "- #{w.signal} sent to #{w.pid}..."
end
end
end
|
#halt ⇒ Object
249
250
251
252
|
# File 'lib/puma/cluster.rb', line 249
def halt
@status = :halt
wakeup!
end
|
#next_worker_index ⇒ Object
106
107
108
109
110
111
|
# File 'lib/puma/cluster.rb', line 106
def next_worker_index
all_positions = 0...@options[:workers]
occupied_positions = @workers.map { |w| w.index }
available_positions = all_positions.to_a - occupied_positions
available_positions.first
end
|
#phased_restart ⇒ Object
228
229
230
231
232
233
234
235
|
# File 'lib/puma/cluster.rb', line 228
def phased_restart
return false if @options[:preload_app]
@phased_restart = true
wakeup!
true
end
|
#preload? ⇒ Boolean
258
259
260
|
# File 'lib/puma/cluster.rb', line 258
def preload?
@options[:preload_app]
end
|
#restart ⇒ Object
223
224
225
226
|
# File 'lib/puma/cluster.rb', line 223
def restart
@restart = true
stop
end
|
#run ⇒ Object
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
|
# File 'lib/puma/cluster.rb', line 262
def run
@status = :run
"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
Signal.trap "TTIN" do
@options[:workers] += 1
wakeup!
end
Signal.trap "TTOU" do
@options[:workers] -= 1 if @options[:workers] >= 2
@workers.last.term
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)
next if !req || req == "!"
pid = read.gets.to_i
if w = @workers.find { |x| x.pid == pid }
case req
when "b"
w.boot!
log "- Worker #{w.index} (pid: #{pid}) booted, phase: #{w.phase}"
when "p"
w.ping!
end
else
log "! Out-of-sync worker list, no #{pid} worker"
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
|
#spawn_workers ⇒ Object
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
# File 'lib/puma/cluster.rb', line 87
def spawn_workers
diff = @options[:workers] - @workers.size
master = Process.pid
diff.times do
idx = next_worker_index
pid = fork { worker(idx, master) }
@cli.debug "Spawned worker: #{pid}"
@workers << Worker.new(idx, pid, @phase)
@options[:after_worker_boot].each { |h| h.call }
end
if diff > 0
@phased_state = :idle
end
end
|
#start_phased_restart ⇒ Object
27
28
29
30
31
32
33
34
35
36
37
|
# File 'lib/puma/cluster.rb', line 27
def start_phased_restart
@phase += 1
log "- Starting phased worker restart, phase: #{@phase}"
if dir = @options[:worker_directory]
log "+ Changing to #{dir}"
Dir.chdir dir
end
end
|
#stats ⇒ Object
254
255
256
|
# File 'lib/puma/cluster.rb', line 254
def stats
%Q!{ "workers": #{@workers.size}, "phase": #{@phase}, "booted_workers": #{@workers.count{|w| w.booted?}} }!
end
|
#stop ⇒ Object
237
238
239
240
|
# File 'lib/puma/cluster.rb', line 237
def stop
@status = :stop
wakeup!
end
|
#stop_blocked ⇒ Object
242
243
244
245
246
247
|
# File 'lib/puma/cluster.rb', line 242
def stop_blocked
@status = :stop if @status == :run
wakeup!
@control.stop(true) if @control
Process.waitall
end
|
#stop_workers ⇒ Object
16
17
18
19
20
21
22
23
24
25
|
# File 'lib/puma/cluster.rb', line 16
def stop_workers
log "- Gracefully shutting down workers..."
@workers.each { |x| x.term }
begin
Process.waitall
rescue Interrupt
log "! Cancelled waiting for workers"
end
end
|
#wakeup! ⇒ Object
164
165
166
167
168
169
|
# File 'lib/puma/cluster.rb', line 164
def wakeup!
begin
@wakeup.write "!" unless @wakeup.closed?
rescue SystemCallError, IOError
end
end
|
#worker(index, master) ⇒ Object
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
|
# File 'lib/puma/cluster.rb', line 171
def worker(index, master)
$0 = "puma: cluster worker #{index}: #{master}"
Signal.trap "SIGINT", "IGNORE"
@master_read.close
@suicide_pipe.close
Thread.new do
IO.select [@check_pipe]
log "! Detected parent died, dying"
exit! 1
end
if !ENV['BUNDLE_GEMFILE'] and File.exist?("Gemfile")
log "+ Gemfile in context: #{File.expand_path("Gemfile")}"
end
hooks = @options[:before_worker_boot]
hooks.each { |h| h.call(index) }
server = start_server
Signal.trap "SIGTERM" do
server.stop
end
begin
@worker_write << "b#{Process.pid}\n"
rescue SystemCallError, IOError
STDERR.puts "Master seems to have exitted, exitting."
return
end
Thread.new(@worker_write) do |io|
payload = "p#{Process.pid}\n"
while true
sleep 5
io << payload
end
end
server.run.join
ensure
@worker_write.close
end
|