<commit_before>require 'heroku-api'

module HerokuResqueAutoScale
  module Scaler
    
    class << self
      @@heroku = Heroku::API.new(api_key: ENV['HEROKU_API_KEY'])
      
      def workers
        return nil unless authorised? 
        @@heroku.get_app(ENV['HEROKU_APP_NAME']).body['workers'].to_i
      end

      def workers=(qty)
        return unless authorised?
        if safe_mode? and down? qty
          return unless safer?
        end
        @@heroku.post_ps_scale(ENV['HEROKU_APP_NAME'], 'worker', qty.to_s)
      end

      def job_count
        Resque.info[:pending].to_i
      end

      def working_job_count
        Resque.info[:working].to_i
      end
      
      protected
      
      def down? qty
        qty < workers 
      end
      
      def safe_mode?
        ENV['SAFE_MODE'] and ENV['SAFE_MODE'] == 'true'
      end
      
      def safer?
        job_count + working_job_count == 0
      end
      
      private
      
      def authorised?
        HerokuResqueAutoScale::Config.thresholds.include? Rails.env.to_s
      end
        
    end
  end

  def after_perform_scale_down(*args)
    scale_down
  end

  def on_failure_scale_down(exception, *args)
    scale_down
  end

  def after_enqueue_scale_up(*args)
    HerokuResqueAutoScale::Config.thresholds.reverse_each do |scale_info|
      # Run backwards so it gets set to the highest value first
      # Otherwise if there were 70 jobs, it would get set to 1, then 2, then 3, etc

      # If we have a job count greater than or equal to the job limit for this scale info
      if Scaler.job_count >= scale_info[:job_count]<commit_msg>Corrected authorized check on thresolds instead of environments<commit_after>require 'heroku-api'

module HerokuResqueAutoScale
  module Scaler
    
    class << self
      @@heroku = Heroku::API.new(api_key: ENV['HEROKU_API_KEY'])
      
      def workers
        return nil unless authorized? 
        @@heroku.get_app(ENV['HEROKU_APP_NAME']).body['workers'].to_i
      end

      def workers=(qty)
        return unless authorized?
        if safe_mode? and down? qty
          return unless safer?
        end
        @@heroku.post_ps_scale(ENV['HEROKU_APP_NAME'], 'worker', qty.to_s)
      end

      def job_count
        Resque.info[:pending].to_i
      end

      def working_job_count
        Resque.info[:working].to_i
      end
      
      protected
      
      def down? qty
        qty < workers 
      end
      
      def safe_mode?
        ENV['SAFE_MODE'] and ENV['SAFE_MODE'] == 'true'
      end
      
      def safer?
        job_count + working_job_count == 0
      end
      
      private
      
      def authorized?
        HerokuResqueAutoScale::Config.environments.include? Rails.env.to_s
      end
        
    end
  end

  def after_perform_scale_down(*args)
    scale_down
  end

  def on_failure_scale_down(exception, *args)
    scale_down
  end

  def after_enqueue_scale_up(*args)
    HerokuResqueAutoScale::Config.thresholds.reverse_each do |scale_info|
      # Run backwards so it gets set to the highest value first
      # Otherwise if there were 70 jobs, it would get set to 1, then 2, then 3, etc

      # If we have a job count greater than or equal to the job limit for this scale info
      if Scaler.job_count >= scale_info[:job_count]
{'fraction_non_alphanumeric': 0.07496977025392987, 'fraction_numerical': 0.0036275695284159614, 'mean_word_length': 2.194015444015444, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4643216', 'n_tokens_mistral': 1071, 'n_tokens_neox': 1026, 'n_words': 278}