Auto-scale workers on Heroku with the workless gem
Adam Duke | 19 April 2013
We all know that performing long running tasks during an http request is a bad idea. There are tools out there like delayed_job and resque to help us put jobs on a queue and process them later using a worker. On Heroku this means running a worker to process the jobs we're queueing, but running a worker continuously costs about $36/month regardless of the amount of processing your app actually requires.
Enter workless, an extension for Delayed Job to only run workers when needed on Heroku. Workless starts up a worker process when there are jobs to be processed, and shuts the worker back down when all the jobs have been completed. Now your Heroku bill will add up based on your actual worker processing needs.
Adding workless to your Rails app
Add workless to your Gemfile
Add an after_initialize block to your config/environments/production.rb to set up the scaler
Add your Heroku API key and app name to heroku config so workless can access the Heroku API on your behalf
Additional configuration options
Workless has a few different scalers to choose from.
There are also some experimental features for the cedar stack to allow scaling multiple workers based on the current workload.