Sha256: 0c9949c9bf117fb81448c3fc7d150e4505e9a015ae2a2001e5ffa718d07abd7b

Contents?: true

Size: 1.85 KB

Versions: 2

Compression:

Stored size: 1.85 KB

Contents

# frozen_string_literal: true

require 'sidekiq-heroku-scaler/heroku/client'
require 'sidekiq-heroku-scaler/worker'

module SidekiqHerokuScaler
  class Manager
    def initialize(heroku_app_name:, heroku_token:, workers:, strategy:)
      @heroku_client = SidekiqHerokuScaler::Heroku::Client.new(heroku_app_name, heroku_token)
      @strategy = strategy
      @workers = workers
    end

    def perform
      autoscalable_workers.each do |worker_name|
        autoscale_one(worker_name)
      end
    end

    private

    attr_reader :heroku_client, :strategy, :workers

    def autoscalable_workers
      heroku_client.sidekiq_workers & workers
    end

    def autoscale_one(worker_name)
      formation = heroku_client.formation_for(worker_name)
      return if formation.blank?

      sidekiq_worker = SidekiqHerokuScaler::Worker.new(worker_name, formation)

      process_formation(sidekiq_worker)
    end

    def decrease_delta_for(sidekiq_worker, delta)
      return delta unless strategy.smart_decrease

      processes = sidekiq_worker.processes.reverse.take_while do |process|
        process['busy'].zero? && process['quiet'] == 'false'
      end

      processes.size < delta.abs ? -processes.size : delta
    end

    def process_formation(sidekiq_worker)
      if strategy.increase?(sidekiq_worker)
        update_formation(sidekiq_worker, strategy.inc_count)
      elsif strategy.decrease?(sidekiq_worker)
        stop_sidekiq_workers(sidekiq_worker, -strategy.dec_count)
      end
    end

    def stop_sidekiq_workers(sidekiq_worker, delta)
      delta = decrease_delta_for(sidekiq_worker, delta)

      update_formation(sidekiq_worker, delta)
    end

    def update_formation(sidekiq_worker, delta)
      heroku_client.update_formation(sidekiq_worker.formation_id,
                                     strategy.safe_quantity(sidekiq_worker.quantity + delta))
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
sidekiq-heroku-scaler-0.4.1 lib/sidekiq-heroku-scaler/manager.rb
sidekiq-heroku-scaler-0.4.0 lib/sidekiq-heroku-scaler/manager.rb