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”:http://devcenter.heroku.com/articles/reading-invoice due to the introduction of “the new process model”:http://blog.nofail.de/2011/06/migrating-an-existing-app-to-heroku-celadon-cedar-stack/ 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).

h2. Full Stack Background Processes

If you want to run a “resque worker”:http://blog.redistogo.com/2010/07/26/resque-with-redis-to-go/ and a “clockwork process”:http://adam.heroku.com/past/2010/6/30/replace_cron_with_clockwork/ 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

h2. Sharing Addon Connections

As Heroku officially announced in their “latest newsletter”:http://newsletterer.heroku.com/2011/07, 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”:http://redistogo.com/. 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.

h2. 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”:https://github.com/phoet/freemium.

h2. Even simpler

If you are just looking for a simple solution of running a background process have a look at “crony, a bootstrap project”:https://github.com/thomasjachmann/crony using “rufus-scheduler”:http://rufus.rubyforge.org/rufus-scheduler/.

9 thoughts on “Heroku Cedar Background Jobs for free!

  1. Alvin Lai

    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


    $ 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

  2. Zbig

    unfortunately this is agains heroku tos :/
    http://policy.heroku.com/tos 4.4:
    “You may not develop multiple Applications to simulate or act as a single Application or otherwise access the Heroku Services in a manner intended to avoid incurring fees.”

  3. Pingback: ついカッとなってPocketの未読記事をランダムに読めるWebサービス「RandomPocket」をつくりました。 | CreativeStyle

  4. Pingback: Heroku, Node.js e Parse - Randômio e Aleatórico

Comments are closed.