Class: Loops::WorkerPool
- Inherits:
-
Object
- Object
- Loops::WorkerPool
- Defined in:
- lib/loops/worker_pool.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #check_workers ⇒ Object
-
#initialize(name, pm, engine, &blk) ⇒ WorkerPool
constructor
A new instance of WorkerPool.
- #logger ⇒ Object
- #shutdown? ⇒ Boolean
- #start_workers(number) ⇒ Object
- #stop_workers(force) ⇒ Object
- #wait_workers ⇒ Object
Constructor Details
#initialize(name, pm, engine, &blk) ⇒ WorkerPool
Returns a new instance of WorkerPool.
5 6 7 8 9 10 11 |
# File 'lib/loops/worker_pool.rb', line 5 def initialize(name, pm, engine, &blk) @name = name @pm = pm @worker_block = blk @engine = engine @workers = [] end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/loops/worker_pool.rb', line 3 def name @name end |
Instance Method Details
#check_workers ⇒ Object
28 29 30 31 32 33 34 35 |
# File 'lib/loops/worker_pool.rb', line 28 def check_workers logger.debug("Checking loop #{name} workers...") @workers.each do |worker| next if worker.running? || worker.shutdown? logger.info("Worker #{worker.name} is not running. Restart!") worker.run end end |
#logger ⇒ Object
13 14 15 |
# File 'lib/loops/worker_pool.rb', line 13 def logger @pm.logger end |
#shutdown? ⇒ Boolean
17 18 19 |
# File 'lib/loops/worker_pool.rb', line 17 def shutdown? @pm.shutdown? end |
#start_workers(number) ⇒ Object
21 22 23 24 25 26 |
# File 'lib/loops/worker_pool.rb', line 21 def start_workers(number) logger.info("Creating #{number} workers for #{name} loop...") number.times do @workers << Worker.new(name, @pm, @engine, &@worker_block) end end |
#stop_workers(force) ⇒ Object
47 48 49 50 51 52 53 |
# File 'lib/loops/worker_pool.rb', line 47 def stop_workers(force) logger.info("Stopping loop #{name} workers...") @workers.each do |worker| next unless worker.running? worker.stop(force) end end |
#wait_workers ⇒ Object
37 38 39 40 41 42 43 44 45 |
# File 'lib/loops/worker_pool.rb', line 37 def wait_workers running = 0 @workers.each do |worker| next unless worker.running? running += 1 logger.info("Worker #{name} is still running (#{worker.pid})") end return running end |