Class: ScaleWorkers::AutoScale

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/scale_workers/auto_scale.rb

Defined Under Namespace

Modules: LOAD_LISTENER

Constant Summary collapse

LOAD_DECREMENT_FACTOR =

2x times

2

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

#say

Constructor Details

#initialize(queue = 'default') ⇒ AutoScale

Returns a new instance of AutoScale.



16
17
18
19
20
21
22
23
# File 'lib/scale_workers/auto_scale.rb', line 16

def initialize(queue = 'default')
  self.queue = queue
  self.config = ScaleWorkers.configuration
  self.current_workers_count = 0
  self.previous_workers_count = 0
  self.high_load = false
  self.interrupted = false
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



14
15
16
# File 'lib/scale_workers/auto_scale.rb', line 14

def config
  @config
end

#current_workers_countObject

Returns the value of attribute current_workers_count.



13
14
15
# File 'lib/scale_workers/auto_scale.rb', line 13

def current_workers_count
  @current_workers_count
end

#high_loadObject

Returns the value of attribute high_load.



14
15
16
# File 'lib/scale_workers/auto_scale.rb', line 14

def high_load
  @high_load
end

#interruptedObject

Returns the value of attribute interrupted.



14
15
16
# File 'lib/scale_workers/auto_scale.rb', line 14

def interrupted
  @interrupted
end

#previous_workers_countObject

Returns the value of attribute previous_workers_count.



13
14
15
# File 'lib/scale_workers/auto_scale.rb', line 13

def previous_workers_count
  @previous_workers_count
end

#queueObject

Returns the value of attribute queue.



13
14
15
# File 'lib/scale_workers/auto_scale.rb', line 13

def queue
  @queue
end

Instance Method Details

#exitObject



67
68
69
# File 'lib/scale_workers/auto_scale.rb', line 67

def exit
  stop self.current_workers_count
end

#increment_or_decrementObject



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/scale_workers/auto_scale.rb', line 71

def increment_or_decrement
  if jobs_count > 0
    load = LoadMonitor.can_increase_load?(config.max_cpu_load, config.max_memory_load)    
    self.current_workers_count += config.increment_step if(load  && self.previous_workers_count < config.max_workers)
    self.current_workers_count -= config.decrement_step if(!load && self.previous_workers_count > config.min_workers)
    self.current_workers_count  = 0 if self.current_workers_count < 0
  else
    self.current_workers_count = config.min_workers
  end
  scale_workers
  sleep(config.sleep_time)
end

#jobs_countObject



63
64
65
# File 'lib/scale_workers/auto_scale.rb', line 63

def jobs_count
  config.count_procedure.call(self.queue, config.max_failure)
end

#monitorObject



25
26
27
28
29
30
31
# File 'lib/scale_workers/auto_scale.rb', line 25

def monitor
  bind_interrupt_listener
  usage_listener = bind_high_usage_listener
  monitor_workers
ensure
  Thread.kill(usage_listener) if usage_listener
end

#monitor_workersObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/scale_workers/auto_scale.rb', line 33

def monitor_workers
  loop do
    say 'monitoring'
    begin
      if self.interrupted
        exit
        break
      elsif self.high_load
        say('high_load')
        high_load_decrement
        scale_workers
        self.high_load = false
      else
        increment_or_decrement
      end
    rescue Exception => e
      raise e
      self.interrupted = true
    end
  end
end

#start(count) ⇒ Object



59
60
61
# File 'lib/scale_workers/auto_scale.rb', line 59

def start(count)
  config.start_procedure.call(self.queue, count)
end

#stop(count) ⇒ Object



55
56
57
# File 'lib/scale_workers/auto_scale.rb', line 55

def stop(count)
  config.stop_procedure.call(self.queue, count)
end