lib/workless/scalers/heroku.rb in workless-1.2.3 vs lib/workless/scalers/heroku.rb in workless-2.2.0

- old
+ new

@@ -1,27 +1,55 @@ -require 'heroku-api' +# frozen_string_literal: true +require 'platform-api' + module Delayed module Workless module Scaler - class Heroku < Base - extend Delayed::Workless::Scaler::HerokuClient def self.up - client.put_workers(ENV['APP_NAME'], 1) if self.workers == 0 + return unless workers_needed > min_workers && workers < workers_needed + updates = { "quantity": workers_needed } + client.formation.update(ENV['APP_NAME'], 'worker', updates) end def self.down - client.put_workers(ENV['APP_NAME'], 0) unless self.jobs.count > 0 or self.workers == 0 + return if workers == workers_needed + updates = { "quantity": workers_needed } + client.formation.update(ENV['APP_NAME'], 'worker', updates) end def self.workers - client.get_ps(ENV['APP_NAME']).body.count { |p| p["process"] =~ /worker\.\d?/ } + client.formation.info(ENV['APP_NAME'], 'worker')['quantity'].to_i end - end + # Returns the number of workers needed based on the current number of pending jobs and the settings defined by: + # + # ENV['WORKLESS_WORKERS_RATIO'] + # ENV['WORKLESS_MAX_WORKERS'] + # ENV['WORKLESS_MIN_WORKERS'] + # + def self.workers_needed + [[(jobs.count.to_f / workers_ratio).ceil, max_workers].min, min_workers].max + end + def self.workers_ratio + if ENV['WORKLESS_WORKERS_RATIO'].present? && (ENV['WORKLESS_WORKERS_RATIO'].to_i != 0) + ENV['WORKLESS_WORKERS_RATIO'].to_i + else + 100 + end + end + + def self.max_workers + ENV['WORKLESS_MAX_WORKERS'].present? ? ENV['WORKLESS_MAX_WORKERS'].to_i : 1 + end + + def self.min_workers + ENV['WORKLESS_MIN_WORKERS'].present? ? ENV['WORKLESS_MIN_WORKERS'].to_i : 0 + end + end end end end