Sha256: d619e2920a5aecac0bc6bc31010a1b2691f980560c3beac5b3b4497185ad8652

Contents?: true

Size: 1.18 KB

Versions: 3

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.to_s } || {}
        )
      end

      def sidekiq_workers
        @sidekiq_workers ||= formations.select { |formation| formation['command'].match(/sidekiq/) }
                                       .map { |formation| formation['type'] }
      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

3 entries across 3 versions & 1 rubygems

Version Path
sidekiq-heroku-scaler-0.3.5 lib/sidekiq-heroku-scaler/heroku/client.rb
sidekiq-heroku-scaler-0.3.4 lib/sidekiq-heroku-scaler/heroku/client.rb
sidekiq-heroku-scaler-0.3.3 lib/sidekiq-heroku-scaler/heroku/client.rb