Sha256: 815a047ea2d9b1e2aa89d19f224baf742765ede72988c5da09a842a993173648

Contents?: true

Size: 1.18 KB

Versions: 5

Compression:

Stored size: 1.18 KB

Contents

# frozen_string_literal: true

require 'platform-api'
require 'sidekiq-heroku-scaler/heroku/formation'

module SidekiqHerokuScaler
  module Heroku
    class Client
      def initialize(heroku_app_name, heroku_token)
        @heroku_app_name = heroku_app_name
        @heroku_token = heroku_token
      end

      def formations
        @formations ||= formation.list(heroku_app_name)
      end

      def formation_for(worker_name)
        SidekiqHerokuScaler::Heroku::Formation.new(
          formations.detect { |formation| formation['type'] == worker_name } || {}
        )
      end

      def sidekiq_workers
        @sidekiq_workers ||= begin
          formations.select { |formation| formation['command'].match(/sidekiq/) }
                    .map { |formation| formation['type'] }
        end
      end

      def update_formation(formation_id, quantity)
        formation.update(heroku_app_name, formation_id, quantity: quantity)
      end

      private

      attr_reader :heroku_app_name, :heroku_token

      def client
        @client ||= PlatformAPI.connect_oauth(heroku_token)
      end

      def formation
        @formation ||= PlatformAPI::Formation.new(client)
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
sidekiq-heroku-scaler-0.2.0 lib/sidekiq-heroku-scaler/heroku/client.rb
sidekiq-heroku-scaler-0.1.5 lib/sidekiq-heroku-scaler/heroku/client.rb
sidekiq-heroku-scaler-0.1.4 lib/sidekiq-heroku-scaler/heroku/client.rb
sidekiq-heroku-scaler-0.1.3 lib/sidekiq-heroku-scaler/heroku/client.rb
sidekiq-heroku-scaler-0.1.0 lib/sidekiq-heroku-scaler/heroku/client.rb