Created
November 23, 2011 14:36
-
-
Save paganotoni/1388820 to your computer and use it in GitHub Desktop.
Heroku Autoscale + Min and Max Workers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Inspired by https://gist.github.com/1332883 | |
module HerokuAutoscale | |
module Scaler | |
class << self | |
@@heroku = Heroku::Client.new( ENV['HEROKU_USER'], ENV['HEROKU_PASS'] ) | |
def workers | |
@@heroku.info(ENV['HEROKU_APP'])[:workers].to_i | |
end | |
def workers=( qty ) | |
min_workers = ENV["MIN_WORKERS"].to_i | |
max_workers = ENV["MAX_WORKERS"].to_i | |
@@heroku.set_workers(ENV['HEROKU_APP'], qty) unless qty < min_workers || qty > max_workers | |
end | |
def increment_workers( increment = 1 ) | |
new_total = workers() + increment | |
@@heroku.set_workers(ENV["HEROKU_APP"], new_total ) unless new_total > ENV["MAX_WORKERS"].to_i | |
end | |
def decrement_workers( decrement = 1 ) | |
new_total = workers() - decrement | |
@@heroku.set_workers(ENV["HEROKU_APP"], new_total ) unless new_total < ENV["MIN_WORKERS"].to_i | |
end | |
def job_count | |
# Searching for jobs that where not locked | |
Delayed::Job.count | |
end | |
def jobs_in_progress | |
in_progress = 0 | |
Delayed::Job.all.each{|dj| in_progress += 1 if dj.locked_by != nil } | |
return in_progress | |
end | |
end | |
end | |
def after_perform_scale_down | |
Scaler.workers = 0 | |
end | |
def after_enqueue_scale_up | |
Scaler.workers = 1 | |
end | |
def scale_up | |
Scaler.increment_workers( 1 ) | |
end | |
def scale_down | |
Rails.logger.info( "[MF] Job Count for scale_down =>"+ Scaler.job_count.to_s ) | |
in_progress = Scaler.jobs_in_progress | |
Scaler.workers = ENV["MIN_WORKERS"].to_i + in_progress | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment