Module: HerokuDjAutoScale

Defined in:
lib/heroku_dj_auto_scale.rb,
lib/heroku_dj_auto_scale/version.rb

Defined Under Namespace

Modules: Scaler

Constant Summary collapse

VERSION =
"0.0.9"

Instance Method Summary collapse

Instance Method Details

#completed(*args) ⇒ Object



54
55
56
57
# File 'lib/heroku_dj_auto_scale.rb', line 54

def completed(*args)
  # Nothing fancy, just shut everything down if we have no jobs
  Scaler.workers = 0 if Scaler.job_count.zero? 
end

#enqueue(*args) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/heroku_dj_auto_scale.rb', line 59

def enqueue(*args)
  [
    {
      :workers => 0, # This many workers
      :job_count => 0 # For this many jobs or more, until the next level
    },
    {
      :workers => 1, # This many workers
      :job_count => 1 # For this many jobs or more, until the next level
    },
    {
      :workers => 2,
      :job_count => 400
    },
    {
      :workers => 3,
      :job_count => 2500
    }
  ].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]
      # Set the number of workers unless they are already set to a level we want. Don't scale down here!
      if Scaler.workers <= scale_info[:workers]
        Scaler.workers = scale_info[:workers]
      end
      break # We've set or ensured that the worker count is high enough
    end
  end
end