Sha256: cfba6c9c1f05d92bc3fc4de6e40f7f68af16f3e1514f502d09d0846789fa47f6

Contents?: true

Size: 1.36 KB

Versions: 6

Compression:

Stored size: 1.36 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(&method(:autoscale_one))
    end

    private

    attr_reader :heroku_client, :strategy, :workers

    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 process_formation(sidekiq_worker)
      if strategy.increase?(sidekiq_worker)
        heroku_client.update_formation(sidekiq_worker.formation_id,
                                       strategy.safe_quantity(sidekiq_worker.quantity + strategy.inc_count))
      elsif strategy.decrease?(sidekiq_worker)
        heroku_client.update_formation(sidekiq_worker.formation_id,
                                       strategy.safe_quantity(sidekiq_worker.quantity - strategy.dec_count))
      end
    end

    def autoscalable_workers
      heroku_client.sidekiq_workers & workers
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
sidekiq-heroku-scaler-0.3.5 lib/sidekiq-heroku-scaler/manager.rb
sidekiq-heroku-scaler-0.3.4 lib/sidekiq-heroku-scaler/manager.rb
sidekiq-heroku-scaler-0.3.3 lib/sidekiq-heroku-scaler/manager.rb
sidekiq-heroku-scaler-0.2.0 lib/sidekiq-heroku-scaler/manager.rb
sidekiq-heroku-scaler-0.1.5 lib/sidekiq-heroku-scaler/manager.rb
sidekiq-heroku-scaler-0.1.4 lib/sidekiq-heroku-scaler/manager.rb