class AutoScale

Constants

LOAD_DECREMENT_FACTOR

Attributes

current_pid_switch[RW]
current_workers_count[RW]
high_load[RW]
interrupted[RW]
previous_pid_switch[RW]
previous_workers_count[RW]
queue[RW]
stopped[RW]

Public Instance Methods

exit() click to toggle source
# File lib/auto_scale.rb, line 53
def exit
  stop self.current_workers_count
end
increment_or_decrement() click to toggle source
# File lib/auto_scale.rb, line 57
def increment_or_decrement
  if jobs_count > 0
    load = LoadMonitor.can_increase_load?(self.max_cpu_load, self.max_memory_load)    
    self.current_workers_count += self.increment_step if(load  && self.previous_workers_count < self.max_workers)
    self.current_workers_count -= self.decrement_step if(!load && self.previous_workers_count > self.min_workers)
    self.current_workers_count  = 0 if self.current_workers_count < 0
  else
    self.current_workers_count = self.min_workers
  end
  scale_workers
  sleep(self.sleep_time)
end
jobs_count() click to toggle source
# File lib/auto_scale.rb, line 49
def jobs_count
  ScaleWorkers.configuration.count_procedure(self.queue, self.max_failure)
end
monitor() click to toggle source
# File lib/auto_scale.rb, line 14
def monitor
  bind_interrupt_listener
  usage_listener = bind_high_usage_listener
  monitor_workers
ensure
  Thread.kill(usage_listener) if usage_listener
end
monitor_workers() click to toggle source
# File lib/auto_scale.rb, line 22
def monitor_workers
  loop do
    begin
      if self.interrupted
        exit
        break
      elsif self.high_load
        high_load_decrement
        scale_workers
        self.high_load = false
      else
        increment_or_decrement
      end
    rescue Exception => e
      self.interrupted = true
    end
  end
end
start(count) click to toggle source
# File lib/auto_scale.rb, line 45
def start(count)
  ScaleWorkers.configuration.start_procedure(self.queue, count)
end
stop(count) click to toggle source
# File lib/auto_scale.rb, line 41
def stop(count)
  ScaleWorkers.configuration.stop_procedure(self.queue, count)
end

Private Instance Methods

bind_high_usage_listener() click to toggle source
# File lib/auto_scale.rb, line 81
def bind_high_usage_listener
  Thread.new do
    high_load_counter = 0
    loop do
      if LoadMonitor.cpu_load > self.max_cpu_load || LoadMonitor.memory_load > self.max_memory_load
        high_load_counter += 1
      else
        high_load_counter = 0
      end
      self.high_load = true if high_load_counter >= LOAD_LISTENER::THRESHOLD
      sleep(LOAD_LISTENER::SLEEP)
    end
  end
end
bind_interrupt_listener() click to toggle source
# File lib/auto_scale.rb, line 71
def bind_interrupt_listener
  Signal.trap('TERM') do
    self.interrupted = true
  end

  Signal.trap('INT') do
    self.interrupted = true
  end
end
high_load_decrement() click to toggle source
# File lib/auto_scale.rb, line 103
def high_load_decrement
  self.current_workers_count -= (LOAD_DECREMENT_FACTOR * self.decrement_step)
  self.current_workers_count = 0 if self.current_workers_count < 0
end
scale_workers() click to toggle source
# File lib/auto_scale.rb, line 96
def scale_workers
  return if self.current_workers_count == self.previous_workers_count
  stop(self.previous_workers_count)
  start(self.current_workers_count)
  self.previous_workers_count = self.current_workers_count
end