Resque Heroku Autoscaler Gem

March 12, 2011 Alexander Murmann

For my side project I needed some worker processes. My project is hosted on Heroku which makes it really easy to add workers. However, currently I am the only user of that project and don’t need a worker to be running all the time. Automatically scaling the workers up when there are pending Resque jobs and scaling them back down once all the work is done seemed like to be the obvious solution to this. Googling immediately brought up a good blog post by Daniel Huckstep. As good as that solution looks, I don’t like to have untested copy-and-paste code in my project. So I decided to test drive my own solution and put it into a convenient gem which of course can be found on Github as well.

Setup

In order for the scaling to work Resque Heroku Atoscaler (RHA) needs to know your Heroku app’s name, your Heroku user name and your Heroku password. Per default those are being read from the following environment variables:

  • HEROKU_APP
  • HEROKU_USER
  • HEROKU_PASS

If you prefer you also can configure RHA using a config block. For example you might put the following into config/initialiazers/resque_heroku_autoscaler_setup.config

require 'resque/plugins/resque_heroku_autoscaler'

Resque::Plugins::HerokuAutoscaler.config do |c|
  c.heroku_user = 'john doe'
  c.heroku_pass = ENV['HEROKU_PASSWORD']
  c.heroku_app  = "my_app_#{Rails.env}"
end

To use RHA in one of your jobs, just extend your job class with Resque::Plugins::HerokuAutoscaler.

For example:

require 'resque/plugins/resque_heroku_autoscaler'

class TestJob
  extend Resque::Plugins::HerokuAutoscaler

  @queue = :test

  def perform
   ...awesome stuff...
  end
end

When you add the job to your Resque queue, a new worker will be started if there isn’t already one. If all jobs in the queue are processed the worker will be stopped again, keeping your costs low.

Changing Scaling Behavior

Per default RHA will only start a single worker, no matter how many jobs are pending. You can change this behavior in the config block as well:

require 'resque/plugins/resque_heroku_autoscaler'

Resque::Plugins::HerokuAutoscaler.config do |c|
  c.new_worker_count do |pending|
    (pending/5).ceil.to_i
  end
end

When calculating the new number of required workers the block given to new_worker_count will be called. Thus the example will result in starting one additional worker for every 5 pending jobs.

Please let me know if you run into any problems with the gem or can think of any features that might be helpful.

About the Author

Biography

More Content by Alexander Murmann
Previous
Service-Oriented Design in Practice
Service-Oriented Design in Practice

Bryan Helmkamp talks about practical service-oriented design.… Read more

Next
Chef Solo – Tugging on the bootstraps
Chef Solo – Tugging on the bootstraps

There's certainly no wrong way to run chef, however, a reasonable set of defaults are usually helpful as a ...

How do you measure digital transformation?

Take the Benchmark