Class: ProcessBalancer::Watcher

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/process_balancer/watcher.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util

#hostname, #identity, #logger, #redis, #start_thread, #watchdog

Constructor Details

#initialize(pool, job_config) ⇒ Watcher

Returns a new instance of Watcher.



12
13
14
15
16
17
18
19
# File 'lib/process_balancer/watcher.rb', line 12

def initialize(pool, job_config)
  @pool       = pool
  @job_config = job_config
  @running    = {}
  @stopping   = []
  @stats      = {}
  @lock       = Mutex.new
end

Instance Attribute Details

#job_configObject (readonly)

Returns the value of attribute job_config.



10
11
12
# File 'lib/process_balancer/watcher.rb', line 10

def job_config
  @job_config
end

#statsObject (readonly)

Returns the value of attribute stats.



10
11
12
# File 'lib/process_balancer/watcher.rb', line 10

def stats
  @stats
end

Instance Method Details

#update_worker_config(process_index, process_count, job_count) ⇒ Object

called when the worker index has changed



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/process_balancer/watcher.rb', line 22

def update_worker_config(process_index, process_count, job_count)
  keep_set = process_count.zero? ? [] : (0...job_count).select { |i| (i % process_count) == process_index }

  with_lock do
    check_workers

    running_set = @running.keys
    create_set  = keep_set - running_set
    stop_set    = running_set - keep_set

    create_set.each do |worker_id|
      start_worker(worker_id)
    end

    stop_set.each do |worker_id|
      stop_worker(worker_id)
    end

    update_stats
  end
end