Class: Puma::Cluster

Inherits:
Runner show all
Defined in:
lib/puma/cluster.rb

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, #start_control, #start_server

Constructor Details

#initialize(cli) ⇒ Cluster

Returns a new instance of Cluster.



5
6
7
8
9
10
11
12
13
# File 'lib/puma/cluster.rb', line 5

def initialize(cli)
  super cli

  @phase = 0
  @workers = []

  @phased_state = :idle
  @phased_restart = false
end

Instance Method Details

#all_workers_booted?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/puma/cluster.rb', line 74

def all_workers_booted?
  @workers.count { |w| !w.booted? } == 0
end

#check_workersObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/puma/cluster.rb', line 78

def check_workers
  while @workers.any?
    pid = Process.waitpid(-1, Process::WNOHANG)
    break unless pid

    @workers.delete_if { |w| w.pid == pid }
  end

  spawn_workers

  if @phased_state == :idle && all_workers_booted?
    # If we're running at proper capacity, check to see if
    # we need to phase any workers out (which will restart
    # in the right phase).
    #
    w = @workers.find { |x| x.phase != @phase }

    if w
      @phased_state = :waiting
      log "- Stopping #{w.pid} for phased upgrade..."
      w.term
    end
  end
end

#haltObject



182
183
184
185
# File 'lib/puma/cluster.rb', line 182

def halt
  @status = :halt
  wakeup!
end

#phased_restartObject



161
162
163
164
165
166
167
168
# File 'lib/puma/cluster.rb', line 161

def phased_restart
  return false if @options[:preload_app]

  @phased_restart = true
  wakeup!

  true
end

#preload?Boolean

Returns:

  • (Boolean)


191
192
193
# File 'lib/puma/cluster.rb', line 191

def preload?
  @options[:preload_app]
end

#restartObject



156
157
158
159
# File 'lib/puma/cluster.rb', line 156

def restart
  @restart = true
  stop
end

#runObject



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
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
# File 'lib/puma/cluster.rb', line 195

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
    # The worker installs their own SIGTERM when booted.
    # Until then, this is run by the worker and the worker
    # should just exit if they get it.
    if Process.pid != master_pid
      log "Early termination of worker"
      exit! 0
    else
      stop
    end
  end

  # 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 = 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

#spawn_workersObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/puma/cluster.rb', line 56

def spawn_workers
  diff = @options[:workers] - @workers.size

  upgrade = (@phased_state == :waiting)

  master = Process.pid

  diff.times do
    pid = fork { worker(upgrade, master) }
    @cli.debug "Spawned worker: #{pid}"
    @workers << Worker.new(pid, @phase)
  end

  if diff > 0
    @phased_state = :idle
  end
end

#start_phased_restartObject



26
27
28
29
# File 'lib/puma/cluster.rb', line 26

def start_phased_restart
  @phase += 1
  log "- Starting phased worker restart, phase: #{@phase}"
end

#statsObject



187
188
189
# File 'lib/puma/cluster.rb', line 187

def stats
  %Q!{ "workers": #{@workers.size}, "phase": #{@phase} }!
end

#stopObject



170
171
172
173
# File 'lib/puma/cluster.rb', line 170

def stop
  @status = :stop
  wakeup!
end

#stop_blockedObject



175
176
177
178
179
180
# File 'lib/puma/cluster.rb', line 175

def stop_blocked
  @status = :stop if @status == :run
  wakeup!
  @control.stop(true) if @control
  Process.waitall
end

#stop_workersObject



15
16
17
18
19
20
21
22
23
24
# File 'lib/puma/cluster.rb', line 15

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



103
104
105
106
107
108
# File 'lib/puma/cluster.rb', line 103

def wakeup!
  begin
    @wakeup.write "!" unless @wakeup.closed?
  rescue SystemCallError, IOError
  end
end

#worker(upgrade, master) ⇒ Object



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
# File 'lib/puma/cluster.rb', line 110

def worker(upgrade, master)
  $0 = "puma: cluster worker: #{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

  # Be sure to change the directory again before loading
  # the app. This way we can pick up new code.
  if upgrade
    if dir = @options[:worker_directory]
      log "+ Changing to #{dir}"
      Dir.chdir dir
    end
  end

  # Invoke any worker boot hooks so they can get
  # things in shape before booting the app.
  hooks = @options[:worker_boot]
  hooks.each { |h| h.call }

  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

  server.run.join

ensure
  @worker_write.close
end