Class: RemoteService::WorkerPool

Inherits:
Object
  • Object
show all
Defined in:
lib/remote_service/worker_pool.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(worker_count, monitor_interval) ⇒ WorkerPool

Returns a new instance of WorkerPool.



5
6
7
8
9
10
# File 'lib/remote_service/worker_pool.rb', line 5

def initialize(worker_count, monitor_interval)
  @worker_count = worker_count
  @monitor_interval = monitor_interval
  @threads = []
  @queue = ::Queue.new
end

Instance Attribute Details

#queueObject (readonly)

Returns the value of attribute queue.



3
4
5
# File 'lib/remote_service/worker_pool.rb', line 3

def queue
  @queue
end

#threadsObject (readonly)

Returns the value of attribute threads.



3
4
5
# File 'lib/remote_service/worker_pool.rb', line 3

def threads
  @threads
end

Instance Method Details

#exitObject



29
30
31
32
33
34
# File 'lib/remote_service/worker_pool.rb', line 29

def exit
  threads.each do |thread|
    thread.exit
  end
  monitor_thread.exit
end

#joinObject



22
23
24
25
26
27
# File 'lib/remote_service/worker_pool.rb', line 22

def join
  threads.each do |thread|
    thread.join
  end
  monitor_thread.join
end

#run(*args, &block) ⇒ Object



12
13
14
# File 'lib/remote_service/worker_pool.rb', line 12

def run(*args, &block)
  queue.push({ args: args, callable: block })
end

#startObject



16
17
18
19
20
# File 'lib/remote_service/worker_pool.rb', line 16

def start
  spawn_workers
  monitor_thread
  RemoteService.logger.info "WORKER POOL - WORKERS: #{threads.size}"
end