class JobsAutoscaling::Monitor

Constants

BUSY
IDLE

Public Class Methods

new(action: ) click to toggle source
# File lib/jobs_autoscaling/monitor.rb, line 8
def initialize(action: )
  @actions = Array(action)
end

Public Instance Methods

activate!() click to toggle source
# File lib/jobs_autoscaling/monitor.rb, line 12
def activate!
  Delayed::Worker.lifecycle.after(:check_for_work, &method(:check_for_work))
  load_preexisting_jobs
  change_state(preexisting_jobs_running? ? BUSY : IDLE)
end

Protected Instance Methods

change_state(new_state) click to toggle source
# File lib/jobs_autoscaling/monitor.rb, line 44
def change_state(new_state)
  @worker_state = new_state
  @actions.each do |action|
    action.public_send(new_state)
  end
end
check_for_work(work_queue) click to toggle source
# File lib/jobs_autoscaling/monitor.rb, line 37
def check_for_work(work_queue)
  new_state = work_queue.all_workers_idle? && !preexisting_jobs_running? ? IDLE : BUSY
  if new_state != @worker_state
    change_state(new_state)
  end
end
load_preexisting_jobs() click to toggle source
# File lib/jobs_autoscaling/monitor.rb, line 20
def load_preexisting_jobs
  @process_mtime_map = {}
  Delayed::Job.processes_locked_locally.each do |pid|
    mtime = Delayed::Worker::ProcessHelper.mtime(pid)
    @process_mtime_map[pid] = mtime if mtime
  end
end
preexisting_jobs_running?() click to toggle source
# File lib/jobs_autoscaling/monitor.rb, line 28
def preexisting_jobs_running?
  @process_mtime_map.each do |pid, mtime|
    unless Delayed::Worker::ProcessHelper.process_is_still_running?(pid, mtime)
      @process_mtime_map.delete(pid)
    end
  end
  @process_mtime_map.any?
end