Class: Elrpc::WorkerPool

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

Overview

タスク処理用のスレッドプール

Instance Method Summary collapse

Constructor Details

#initialize(num, logger) ⇒ WorkerPool

Returns a new instance of WorkerPool.



702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
# File 'lib/elrpc.rb', line 702

def initialize(num, logger)
     @logger = logger
  @job_queue = Queue.new
  @worker_threads = []
  num.times {
	@worker_threads << Thread.start(@job_queue, @logger) { |queue, logger|
         logger.debug("Worker Start")
	  loop {
           begin
             job = queue.shift
             logger.debug "Worker Thread : Job #{job}"
             break if job.nil?
             job.call
           rescue => e
             logger.error "Worker Error >>"
             logger.error e
           end
	  }
         logger.debug("Worker Exit")
	}
  }
end

Instance Method Details

#invoke(job) ⇒ Object



725
726
727
728
729
730
731
# File 'lib/elrpc.rb', line 725

def invoke(job)
     if @worker_threads.size == 0 then
       @logger.debug "Worker : Ignore #{job}"
       return
     end
  @job_queue << job
end

#killObject



733
734
735
736
737
738
739
# File 'lib/elrpc.rb', line 733

def kill
  @worker_threads.size.times {
	invoke(nil)
  }
     @worker_threads.each {|t| t.join }
  @worker_threads.clear
end