Sha256: bbe2db2d2017d37aa60624bac3c93957571637b77e7eada7725287f95cc7da6d

Contents?: true

Size: 696 Bytes

Versions: 1

Compression:

Stored size: 696 Bytes

Contents

require 'delayed_job'

module JobsAutoscaling
  class Monitor
    IDLE = :idle
    BUSY = :busy

    def initialize(action: action)
      @action = action
    end

    def activate!
      Delayed::Worker.lifecycle.after(:work_queue_pop, &method(:work_queue_pop))
      change_state(IDLE)
    end

    protected

    def work_queue_pop(work_queue)
      unless work_queue.respond_to?(:all_workers_idle?)
        return
      end

      new_state = work_queue.all_workers_idle? ? IDLE : BUSY
      if new_state != @worker_state
        change_state(new_state)
      end
    end

    def change_state(new_state)
      @worker_state = new_state
      @action.public_send(new_state)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
inst-jobs-autoscaling-1.0.1 lib/jobs_autoscaling/monitor.rb