Class: Resque::Pool

Inherits:
Object
  • Object
show all
Extended by:
Logging
Includes:
Logging
Defined in:
lib/resque/pool.rb,
lib/resque/pool/cli.rb,
lib/resque/pool/killer.rb,
lib/resque/pool/logging.rb,
lib/resque/pool/version.rb,
lib/resque/pool/pooled_worker.rb,
lib/resque/pool/config_loaders/throttled.rb,
lib/resque/pool/config_loaders/file_or_hash_loader.rb

Defined Under Namespace

Modules: CLI, ConfigLoaders, Logging, PooledWorker Classes: Killer, QuitNowException

Constant Summary collapse

SIG_QUEUE_MAX_SIZE =
5
DEFAULT_WORKER_INTERVAL =
5
QUEUE_SIGS =
[ :QUIT, :INT, :TERM, :USR1, :USR2, :CONT, :HUP, :WINCH, Signal.signame(29).to_sym ]
CHUNK_SIZE =
(16 * 1024)
VERSION =
"0.8.0"

Constants included from Logging

Logging::PROCLINE_PREFIX

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logging

app, is_log?, log, log_worker, procline, reopen_logs!

Constructor Details

#initialize(config_loader = nil) ⇒ Pool

Returns a new instance of Pool.



25
26
27
28
29
# File 'lib/resque/pool.rb', line 25

def initialize(config_loader=nil)
  init_config(config_loader)
  @workers = Hash.new { |workers, queues| workers[queues] = {} }
  procline "(initialized)"
end

Class Attribute Details

.app_nameObject

Returns the value of attribute app_name.



90
91
92
# File 'lib/resque/pool.rb', line 90

def app_name
  @app_name
end

.config_loaderObject

Returns the value of attribute config_loader.



90
91
92
# File 'lib/resque/pool.rb', line 90

def config_loader
  @config_loader
end

.kill_other_poolsObject

Returns the value of attribute kill_other_pools.



260
261
262
# File 'lib/resque/pool.rb', line 260

def kill_other_pools
  @kill_other_pools
end

.spawn_delayObject

Returns the value of attribute spawn_delay.



90
91
92
# File 'lib/resque/pool.rb', line 90

def spawn_delay
  @spawn_delay
end

.term_behaviorObject

Returns the value of attribute term_behavior.



259
260
261
# File 'lib/resque/pool.rb', line 259

def term_behavior
  @term_behavior
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



21
22
23
# File 'lib/resque/pool.rb', line 21

def config
  @config
end

#config_loaderObject (readonly)

Returns the value of attribute config_loader.



22
23
24
# File 'lib/resque/pool.rb', line 22

def config_loader
  @config_loader
end

#workersObject (readonly)

Returns the value of attribute workers.



23
24
25
# File 'lib/resque/pool.rb', line 23

def workers
  @workers
end

Class Method Details

.create_configuredObject



124
125
126
# File 'lib/resque/pool.rb', line 124

def self.create_configured
  Resque::Pool.new(config_loader)
end

.handle_winch=(bool) ⇒ Object



99
100
101
# File 'lib/resque/pool.rb', line 99

def self.handle_winch=(bool)
  @handle_winch = bool
end

.handle_winch?Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/resque/pool.rb', line 96

def self.handle_winch?
  @handle_winch ||= false
end

.hook(name) ⇒ Object

Config: hooks {{{



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/resque/pool.rb', line 33

def self.hook(name) # :nodoc:
  class_eval <<-CODE
    def self.#{name}(&block)
      @#{name} ||= []
      block ? (@#{name} << block) : @#{name}
    end

    def self.#{name}=(block)
      @#{name} = [block]
    end

    def call_#{name}!(*args)
      self.class.#{name}.each do |hook|
        hook.call(*args)
      end
    end
  CODE
end

.kill_other_pools!Object



103
104
105
106
# File 'lib/resque/pool.rb', line 103

def self.kill_other_pools!
  require 'resque/pool/killer'
  Resque::Pool::Killer.run
end

.runObject



117
118
119
120
121
122
# File 'lib/resque/pool.rb', line 117

def self.run
  if GC.respond_to?(:copy_on_write_friendly=)
    GC.copy_on_write_friendly = true
  end
  create_configured.start.join
end

.single_process_groupObject



111
112
113
114
115
# File 'lib/resque/pool.rb', line 111

def self.single_process_group
  %w[yes y true t 1 okay sure please].include?(
    ENV["RESQUE_SINGLE_PGRP"].to_s.downcase
  )
end

.single_process_group=(bool) ⇒ Object



108
109
110
# File 'lib/resque/pool.rb', line 108

def self.single_process_group=(bool)
  ENV["RESQUE_SINGLE_PGRP"] = !!bool ? "YES" : "NO"
end

Instance Method Details

#after_preforkObject

:call-seq:

after_prefork do |worker| ... end   => add a hook
after_prefork << hook               => add a hook

after_prefork will run in workers before any jobs. Use these hooks e.g. to reload database connections to ensure that they’re not shared among workers.

:yields: worker



62
# File 'lib/resque/pool.rb', line 62

hook :after_prefork

#after_spawnObject

:call-seq:

after_spawn do |worker, pid, workers| ... end  => add a hook
after_spawn << hook                            => add a hook

The after_spawn hooks will run in the master after spawning a new worker.

:yields: worker, pid, workers



73
# File 'lib/resque/pool.rb', line 73

hook :after_spawn

#all_known_queuesObject



396
397
398
# File 'lib/resque/pool.rb', line 396

def all_known_queues
  config.keys | workers.keys
end

#all_pidsObject



374
375
376
# File 'lib/resque/pool.rb', line 374

def all_pids
  workers.map {|q,workers| workers.keys }.flatten
end

#awaken_masterObject



180
181
182
183
184
185
186
187
# File 'lib/resque/pool.rb', line 180

def awaken_master
  begin
    self_pipe.last.write_nonblock('.') # wakeup master process from select
  rescue Errno::EAGAIN, Errno::EINTR
    # pipe is full, master should wake up anyways
    retry
  end
end

#create_worker(queues) ⇒ Object



449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
# File 'lib/resque/pool.rb', line 449

def create_worker(queues)
  queues = queues.to_s.split(',')
  worker = ::Resque::Worker.new(*queues)
  worker.pool_master_pid = Process.pid
  worker.term_timeout = (ENV['RESQUE_TERM_TIMEOUT'] || 4.0).to_f
  worker.term_child = ENV['TERM_CHILD']
  if worker.respond_to?(:run_at_exit_hooks=)
    # resque doesn't support this until 1.24, but we support 1.22
    worker.run_at_exit_hooks = ENV['RUN_AT_EXIT_HOOKS'] || false
  end
  if ENV['LOGGING'] || ENV['VERBOSE']
    worker.verbose = ENV['LOGGING'] || ENV['VERBOSE']
  end
  if ENV['VVERBOSE']
    worker.very_verbose = ENV['VVERBOSE']
  end
  worker
end

#delete_worker(pid) ⇒ Object

TODO: close any file descriptors connected to worker, if any



366
367
368
369
370
371
372
# File 'lib/resque/pool.rb', line 366

def delete_worker(pid)
  worker = nil
  workers.detect do |queues, pid_to_worker|
    worker = pid_to_worker.delete(pid)
  end
  worker
end

#environmentObject



150
151
152
153
154
155
156
157
158
# File 'lib/resque/pool.rb', line 150

def environment
  if defined? RAILS_ENV
    RAILS_ENV
  elsif defined?(Rails) && Rails.respond_to?(:env)
    Rails.env
  else
    ENV['RACK_ENV'] || ENV['RAILS_ENV'] || ENV['RESQUE_ENV']
  end
end

#graceful_worker_shutdown!(signal) ⇒ Object



274
275
276
277
278
279
280
281
282
# File 'lib/resque/pool.rb', line 274

def graceful_worker_shutdown!(signal)
  log "#{signal}: immediate shutdown (graceful worker shutdown)"
  if term_child
    signal_all_workers(:TERM)
  else
    signal_all_workers(:QUIT)
  end
  :break
end

#graceful_worker_shutdown_and_wait!(signal) ⇒ Object



263
264
265
266
267
268
269
270
271
272
# File 'lib/resque/pool.rb', line 263

def graceful_worker_shutdown_and_wait!(signal)
  log "#{signal}: graceful shutdown, waiting for children"
  if term_child
    signal_all_workers(:TERM)
  else
    signal_all_workers(:QUIT)
  end
  reap_all_workers(0) # will hang until all workers are shutdown
  :break
end

#handle_sig_queue!Object



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
# File 'lib/resque/pool.rb', line 210

def handle_sig_queue!
  case signal = sig_queue.shift
  when :USR1, :USR2, :CONT
    log "#{signal}: sending to all workers"
    signal_all_workers(signal)
  when :HUP
    log "HUP: reset configuration and reload logfiles"
    reset_config
    Logging.reopen_logs!
    log "HUP: gracefully shutdown old children (which have old logfiles open)"
    if term_child
      signal_all_workers(:TERM)
    else
      signal_all_workers(:QUIT)
    end
    log "HUP: new children will inherit new logfiles"
    maintain_worker_count
  when :WINCH
    if self.class.handle_winch?
      log "WINCH: gracefully stopping all workers"
      @config = {}
      maintain_worker_count
    end
  when :QUIT
    if term_child
      shutdown_everything_now!(signal)
    else
      graceful_worker_shutdown_and_wait!(signal)
    end
  when :INT
    graceful_worker_shutdown!(signal)
  when :TERM
    case self.class.term_behavior
    when "graceful_worker_shutdown_and_wait"
      graceful_worker_shutdown_and_wait!(signal)
    when "graceful_worker_shutdown"
      graceful_worker_shutdown!(signal)
    else
      shutdown_everything_now!(signal)
    end
  when Signal.signame(29).to_sym
    maintain_worker_count
    Thread.start(self, Process.pid, workers) do |instance, pid, workers|
      call_siginfo!(instance, pid, workers)
    end.join
  end
end

#init_config(loader) ⇒ Object

}}} Config: store loader and load config {{{



131
132
133
134
135
136
137
138
139
# File 'lib/resque/pool.rb', line 131

def init_config(loader)
  case loader
  when String, Hash, nil
    @config_loader = ConfigLoaders::FileOrHashLoader.new(loader)
  else
    @config_loader = loader
  end
  load_config
end

#init_self_pipe!Object



169
170
171
172
173
# File 'lib/resque/pool.rb', line 169

def init_self_pipe!
  self_pipe.each { |io| io.close rescue nil }
  self_pipe.replace(IO.pipe)
  self_pipe.each { |io| io.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC) }
end

#init_sig_handlers!Object



175
176
177
178
# File 'lib/resque/pool.rb', line 175

def init_sig_handlers!
  QUEUE_SIGS.each { |sig| trap_deferred(sig) }
  trap(:CHLD)     { |_| awaken_master }
end

#joinObject



317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'lib/resque/pool.rb', line 317

def join
  loop do
    reap_all_workers
    break if handle_sig_queue! == :break
    if sig_queue.empty?
      master_sleep
      load_config
      maintain_worker_count
    end
    procline("managing #{all_pids.inspect}")
  end
  procline("(shutting down)")
  #stop # gracefully shutdown all workers on our way out
  log "manager finished"
  #unlink_pid_safe(pid) if pid
end

#load_configObject



141
142
143
# File 'lib/resque/pool.rb', line 141

def load_config
  @config = config_loader.call(environment)
end

#maintain_worker_countObject

}}} ???: maintain_worker_count, all_known_queues {{{



388
389
390
391
392
393
394
# File 'lib/resque/pool.rb', line 388

def maintain_worker_count
  all_known_queues.each do |queues|
    delta = worker_delta_for(queues)
    spawn_missing_workers_for(queues) if delta > 0
    quit_excess_workers_for(queues)   if delta < 0
  end
end

#master_sleepObject



334
335
336
337
338
339
340
341
# File 'lib/resque/pool.rb', line 334

def master_sleep
  begin
    ready = IO.select([self_pipe.first], nil, nil, 1) or return
    ready.first && ready.first.first or return
    loop { self_pipe.first.read_nonblock(CHUNK_SIZE) }
  rescue Errno::EAGAIN, Errno::EINTR
  end
end

#pids_for(queues) ⇒ Object



422
423
424
# File 'lib/resque/pool.rb', line 422

def pids_for(queues)
  workers[queues].keys
end

#quit_excess_workers_for(queues) ⇒ Object



411
412
413
414
415
416
# File 'lib/resque/pool.rb', line 411

def quit_excess_workers_for(queues)
  delta = -worker_delta_for(queues)
  pids_for(queues)[0...delta].each do |pid|
    Process.kill("QUIT", pid)
  end
end

#reap_all_workers(waitpid_flags = Process::WNOHANG) ⇒ Object

}}} worker process management {{{



346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# File 'lib/resque/pool.rb', line 346

def reap_all_workers(waitpid_flags=Process::WNOHANG)
  @waiting_for_reaper = waitpid_flags == 0
  begin
    loop do
      # -1, wait for any child process
      wpid, status = Process.waitpid2(-1, waitpid_flags)
      break unless wpid

      if worker = delete_worker(wpid)
        log "Reaped resque worker[#{status.pid}] (status: #{status.exitstatus}) queues: #{worker.queues.join(",")}"
      else
        # this died before it could be killed, so it's not going to have any extra info
        log "Tried to reap worker [#{status.pid}], but it had already died. (status: #{status.exitstatus})"
      end
    end
  rescue Errno::ECHILD, QuitNowException
  end
end

#report_worker_pool_pidsObject



309
310
311
312
313
314
315
# File 'lib/resque/pool.rb', line 309

def report_worker_pool_pids
  if workers.empty?
    log "Pool is empty"
  else
    log "Pool contains worker PIDs: #{all_pids.inspect}"
  end
end

#reset_configObject



145
146
147
148
# File 'lib/resque/pool.rb', line 145

def reset_config
  config_loader.reset! if config_loader.respond_to?(:reset!)
  load_config
end

#reset_sig_handlers!Object



206
207
208
# File 'lib/resque/pool.rb', line 206

def reset_sig_handlers!
  QUEUE_SIGS.each {|sig| trap(sig, "DEFAULT") }
end

#self_pipeObject

Sig handlers and self pipe management {{{



164
# File 'lib/resque/pool.rb', line 164

def self_pipe; @self_pipe ||= [] end

#shutdown_everything_now!(signal) ⇒ Object



284
285
286
287
288
289
290
291
292
# File 'lib/resque/pool.rb', line 284

def shutdown_everything_now!(signal)
  log "#{signal}: immediate shutdown (and immediate worker shutdown)"
  if term_child
    signal_all_workers(:QUIT)
  else
    signal_all_workers(:TERM)
  end
  :break
end

#sig_queueObject



165
# File 'lib/resque/pool.rb', line 165

def sig_queue; @sig_queue ||= [] end

#siginfoObject

:call-seq:

siginfo do |pool_instance, pid, workers| ... end  => add a hook
siginfo << hook                                   => add a hook

The siginfo hooks will run in the master after receiving a SIGINFO 29 signal. A given block will receive the instance of the running manager pool, the manager pid and the workers hash.

:yields: pid, workers



85
# File 'lib/resque/pool.rb', line 85

hook :siginfo

#signal_all_workers(signal) ⇒ Object



378
379
380
381
382
383
# File 'lib/resque/pool.rb', line 378

def signal_all_workers(signal)
  log "Sending #{signal} to all workers"
  all_pids.each do |pid|
    Process.kill signal, pid
  end
end

#spawn_missing_workers_for(queues) ⇒ Object

}}} methods that operate on a single grouping of queues {{{ perhaps this means a class is waiting to be extracted



404
405
406
407
408
409
# File 'lib/resque/pool.rb', line 404

def spawn_missing_workers_for(queues)
  worker_delta_for(queues).times do |nr|
    spawn_worker!(queues)
    sleep Resque::Pool.spawn_delay if Resque::Pool.spawn_delay
  end
end

#spawn_worker!(queues) ⇒ Object



426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
# File 'lib/resque/pool.rb', line 426

def spawn_worker!(queues)
  worker = create_worker(queues)
  pid = fork do
    Process.setpgrp unless Resque::Pool.single_process_group
    worker.worker_parent_pid = Process.pid
    log_worker "Starting worker #{worker}"
    call_after_prefork!(worker)
    reset_sig_handlers!
    #self_pipe.each {|io| io.close }
    # will block until a shutdown signal is received
    if worker.method(:work).parameters.size > 2 # Backwards compat
      worker.work(ENV["INTERVAL"],
                  max_interval:     ENV['MAX_INTERVAL'],
                  min_interval:     ENV['MIN_INTERVAL'],
                  backoff_interval: ENV['BACKOFF_INTERVAL'])
    else
      worker.work(ENV['INTERVAL'] || DEFAULT_WORKER_INTERVAL)
    end
  end
  workers[queues][pid] = worker
  call_after_spawn!(worker, pid, workers)
end

#startObject

}}} start, join, and master sleep {{{



297
298
299
300
301
302
303
304
305
306
307
# File 'lib/resque/pool.rb', line 297

def start
  procline("(starting)")
  init_self_pipe!
  init_sig_handlers!
  maintain_worker_count
  procline("(started)")
  log "started manager"
  report_worker_pool_pids
  self.class.kill_other_pools! if self.class.kill_other_pools
  self
end

#term_childObject



166
# File 'lib/resque/pool.rb', line 166

def term_child; @term_child ||= ENV['TERM_CHILD'] end

#trap_deferred(signal) ⇒ Object

defer a signal for later processing in #join (master process)



191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/resque/pool.rb', line 191

def trap_deferred(signal)
  trap(signal) do |sig_nr|
    if @waiting_for_reaper && [:INT, :TERM].include?(signal)
      log "Recieved #{signal}: short circuiting QUIT waitpid"
      raise QuitNowException
    end
    if sig_queue.size < SIG_QUEUE_MAX_SIZE
      sig_queue << signal
      awaken_master
    else
      log "ignoring SIG#{signal}, queue=#{sig_queue.inspect}"
    end
  end
end

#worker_delta_for(queues) ⇒ Object



418
419
420
# File 'lib/resque/pool.rb', line 418

def worker_delta_for(queues)
  config.fetch(queues, 0) - workers.fetch(queues, []).size
end