Heroku Cedar Background Jobs for free!
I’m using Heroku mostly for playing around with latest technology and hosting free apps. Since Heroku changed their pricing model due to the introduction of the new process model some of my apps changed from free to paid, especially those that had some background jobs or nightly crons (I really did not get, why this happend).
Full Stack Background Processes
If you want to run a resque worker and a clockwork process within your web-app, this becomes a costly thing, even if those are just running some minor jobs in the night, because you need to pay for 2 additional dynos.
You could achieve this through defining multiple processes in your Procfile:
# Procfile web: bundle exec rails server thin -p $PORT worker: QUEUE=* bundle exec rake environment resque:work clock: bundle exec clockwork app/clock.rb
heroku scale worker=1 clock=1
Sharing Addon Connections
As Heroku officially announced in their latest newsletter, it’s possible to share connections between multiple apps. In my case, this would be a connection to a redis key-value store, that is provided by Redistogo. It’s as simple as copying the environment configuration for the addon over to the second app:
heroku config | grep DATABASE_URL --app sushi => DATABASE_URL => postgres://lswlm... heroku config:add DATABASE_URL=postgres://lswlm... --app sushi-analytics => Adding config vars: DATABASE_URL => postgres://lswlm...m/ldfoiusfsf => Restarting app... done, v74.
Going Freemium
So the solution for getting back to a free worker setup is combining 3 Heroku apps and a shared Redis connection through Redistogo:
heroku apps:create freemium-web --stack cedar --remote heroku git push heroku master heroku apps:create freemium-worker --stack cedar --remote worker git push worker master heroku apps:create freemium-clock --stack cedar --remote clock git push clock master heroku scale web=1 --app=freemium-web heroku scale web=0 worker=1 --app=freemium-worker heroku scale web=0 clock=1 --app=freemium-clock heroku addons:add redistogo:nano --app=freemium-web heroku config:add `heroku config -s --app=freemium-web|grep redis` --app=freemium-worker heroku config:add `heroku config -s --app=freemium-web|grep redis` --app=freemium-clock
I created an example project running a Rails 3 application with a mounted Resque web, a Resque worker and a clock process with Clockwork.
Even simpler
If you are just looking for a simple solution of running a background process have a look at crony, a bootstrap project using rufus-scheduler.













If Heroku asks you about billing like this:
$ heroku scale web=0 clock=1 –app=gravity-clock
Scaling clock processes… This action will cause your account to be billed at the end of the month
For more information, see http://docs.heroku.com/billing
Are you sure you want to do this? (y/n) n
Try setting all to 0 first:
$ heroku scale web=0 worker=0 clock=0 –app=gravity-clock
Scaling clock processes… done, now running 0
Scaling web processes… done, now running 0
Scaling worker processes… done, now running 0
then:
$ heroku scale web=0 worker=0 clock=1 –app=gravity-clock
Scaling clock processes… done, now running 1
Scaling web processes… done, now running 0
Scaling worker processes… done, now running 0
Enjoy! =)
Alvin Lai
awesome
Have you ever tried to do this with Heroku scaler? It would be kinda awesome, but I still haven’t got there… :/