class Dynomatic::Master

Attributes

configuration[RW]
current_dyno_count[RW]

Public Class Methods

new(configuration) click to toggle source
# File lib/dynomatic/master.rb, line 5
def initialize(configuration)
  @configuration = configuration
end

Public Instance Methods

adjust_dynos!() click to toggle source

Main method

# File lib/dynomatic/master.rb, line 18
def adjust_dynos!
  job_count = configuration.adapter.job_count
  new_dyno_count = dyno_count_for_job_count(job_count)

  if current_dyno_count != new_dyno_count
    set_dyno_count!(new_dyno_count)
  end
end
install!() click to toggle source
# File lib/dynomatic/master.rb, line 9
def install!
  this = self

  ActiveJob::Base.before_perform do
    this.adjust_dynos!
  end
end

Private Instance Methods

dyno_count_for_job_count(job_count) click to toggle source
# File lib/dynomatic/master.rb, line 29
def dyno_count_for_job_count(job_count)
  rule = configuration.sorted_rules.reverse.find { |rule| rule[:at_least] <= job_count }

  rule[:dynos]
end
scaler() click to toggle source
# File lib/dynomatic/master.rb, line 47
def scaler
  @scaler ||=
    if configuration.worker_names.present?
      HobbyScaler.new(configuration.heroku_token, configuration.heroku_app, configuration.worker_names)
    else
      Scaler.new(configuration.heroku_token, configuration.heroku_app)
    end
end
set_dyno_count!(new_dyno_count) click to toggle source
# File lib/dynomatic/master.rb, line 35
def set_dyno_count!(new_dyno_count)
  old_dyno_count = current_dyno_count
  self.current_dyno_count = new_dyno_count

  scaler.scale_to(new_dyno_count)
rescue => error
  # If something went wrong, reset to old value
  self.current_dyno_count = old_dyno_count

  Rails.logger.error "Something went wrong while adjusting dyno count: #{error.message}."
end