Class: Daemonizer::WorkerPool

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

Constant Summary collapse

MONITOR_VALUE =
[:vm_size, :private_dirty_rss, :rss]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, pm, &blk) ⇒ WorkerPool

Returns a new instance of WorkerPool.



8
9
10
11
12
13
14
# File 'lib/daemonizer/worker_pool.rb', line 8

def initialize(name, pm, &blk)
  @name = name
  @pm = pm
  @worker_block = blk
  @workers = []
  @stats = ::SimpleStatistics::DataSet.new
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/daemonizer/worker_pool.rb', line 5

def name
  @name
end

#statsObject (readonly)

Returns the value of attribute stats.



6
7
8
# File 'lib/daemonizer/worker_pool.rb', line 6

def stats
  @stats
end

Instance Method Details

#check_workersObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/daemonizer/worker_pool.rb', line 38

def check_workers
  Daemonizer.logger.debug "Checking loop #{name} workers..."

  Daemonizer::Stats::MemoryStats.new(self).find_workers.each do |p|
    worker_name = p.name
    MONITOR_VALUE.each do |value|
      @stats.tick(value)
      @stats[worker_name][value].add_probe(p.send(value))
    end
  end
  
  @workers.each do |worker|
    unless worker.running? || worker.shutdown?
      Daemonizer.logger.warn "Worker #{worker.name} is not running. Restart!"
      @stats.add_data(worker.process_name)
      MONITOR_VALUE.each do |v|
        @stats[worker.process_name].reset(v)
      end
      worker.run
    end
  end
end

#find_worker_by_name(name) ⇒ Object



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

def find_worker_by_name(name)
  @workers.detect do |w|
    w.process_name.to_s == name.to_s
  end
end

#shutdown?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/daemonizer/worker_pool.rb', line 22

def shutdown?
  @pm.shutdown?
end

#start_workers(number) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/daemonizer/worker_pool.rb', line 26

def start_workers(number)
  Daemonizer.logger.debug "Creating #{number} workers for #{name} pool..."
  number.times do |i|
    worker = Worker.new(name, @pm, i+1, &@worker_block)
    @workers << worker
    @stats.add_data(worker.process_name)
    Daemonizer.logger.info "Gathering data for #{worker.name}"    
  end
rescue Exception => e
  Daemonizer.logger.info "Result - #{e.inspect}"
end

#stop_workers(force) ⇒ Object



72
73
74
75
76
77
78
# File 'lib/daemonizer/worker_pool.rb', line 72

def stop_workers(force)
  Daemonizer.logger.debug "Stopping #{name} pool workers..."
  @workers.each do |worker|
    next unless worker.running?
    worker.stop(force)
  end
end

#wait_workersObject



62
63
64
65
66
67
68
69
70
# File 'lib/daemonizer/worker_pool.rb', line 62

def wait_workers
  running = 0
  @workers.each do |worker|
    next unless worker.running?
    running += 1
    Daemonizer.logger.debug "Worker #{name} is still running (#{worker.pid})"
  end
  return running
end