class RemoteService::WorkerPool

Attributes

queue[R]
threads[R]

Public Class Methods

new(worker_count, monitor_interval) click to toggle source
# 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

Public Instance Methods

exit() click to toggle source
# File lib/remote_service/worker_pool.rb, line 29
def exit
  threads.each do |thread|
    thread.exit
  end
  monitor_thread.exit
end
join() click to toggle source
# 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) click to toggle source
# File lib/remote_service/worker_pool.rb, line 12
def run(*args, &block)
  queue.push({ args: args, callable: block })
end
start() click to toggle source
# File lib/remote_service/worker_pool.rb, line 16
def start
  spawn_workers
  monitor_thread
  RemoteService.logger.info "WORKER POOL - WORKERS: #{threads.size}"
end

Private Instance Methods

monitor_thread() click to toggle source
# File lib/remote_service/worker_pool.rb, line 49
def monitor_thread
  @monitor_thread ||= begin
    RemoteService.logger.info "WORKER POOL - MONITOR INTERVAL: #{@monitor_interval}s"
    Thread.new do
      loop do
        RemoteService.logger.info "WORKER POOL - WAITING: #{queue.size}"
        sleep(@monitor_interval)
      end
    end
  end
end
spawn_workers() click to toggle source
# File lib/remote_service/worker_pool.rb, line 38
def spawn_workers
  @worker_count.times do
    @threads << Thread.new do
      loop do
        data = queue.pop
        data[:callable].call(*data[:args])
      end
    end
  end
end