Class: Infrastruct::Manager

Inherits:
Object
  • Object
show all
Defined in:
lib/infrastruct/manager.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(worker_factory, thread_pool:) ⇒ Manager



5
6
7
8
9
10
# File 'lib/infrastruct/manager.rb', line 5

def initialize(worker_factory, thread_pool:)
  @worker_factory = worker_factory
  @thread_pool = thread_pool
  @results = Array.new
  @mutex = Mutex.new
end

Instance Attribute Details

#queueObject (readonly)

Returns the value of attribute queue.



3
4
5
# File 'lib/infrastruct/manager.rb', line 3

def queue
  @queue
end

Instance Method Details

#enqueue(*args, &block) ⇒ Object



12
13
14
# File 'lib/infrastruct/manager.rb', line 12

def enqueue(*args, &block)
  @thread_pool.enqueue([args, block])
end

#runObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/infrastruct/manager.rb', line 16

def run
  @thread_pool.run do |params|
    begin
      worker = @worker_factory.new
      result = worker.perform(*params[0], &params[1])

      @mutex.synchronize do
        @results.push(result)
      end
    rescue
      @thread_pool.enqueue(params)
    end
  end

  worker = @worker_factory.new
  worker.collect(@results)
end