Class: Workers::Pool

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/workers/pool.rb

Constant Summary collapse

DEFAULT_POOL_SIZE =
20

Instance Method Summary collapse

Methods included from Helpers

#concat_e, #log_debug, #log_error, #log_info, #log_warn

Constructor Details

#initialize(options = {}) ⇒ Pool

Returns a new instance of Pool.



7
8
9
10
11
12
13
14
15
# File 'lib/workers/pool.rb', line 7

def initialize(options = {})
  @size = options[:size] || Workers::Pool::DEFAULT_POOL_SIZE
  @logger = Workers::LogProxy.new(options[:logger])
  @worker_class = options[:worker_class] || Workers::Worker

  @input_queue = Queue.new
  @workers = []
  @size.times { @workers << @worker_class.new(:input_queue => @input_queue) }
end

Instance Method Details

#enqueue(command, data) ⇒ Object



17
18
19
20
21
# File 'lib/workers/pool.rb', line 17

def enqueue(command, data)
  @input_queue.push(Event.new(command, data))

  return nil
end

#join(max_wait = nil) ⇒ Object



35
36
37
# File 'lib/workers/pool.rb', line 35

def join(max_wait = nil)
  return @workers.map { |w| w.join(max_wait) }
end

#perform(&block) ⇒ Object



23
24
25
26
27
# File 'lib/workers/pool.rb', line 23

def perform(&block)
  enqueue(:perform, block)

  return nil
end

#shutdown(&block) ⇒ Object



29
30
31
32
33
# File 'lib/workers/pool.rb', line 29

def shutdown(&block)
  @size.times { enqueue(:shutdown, block) }

  return nil
end