Cron jobs in Rails : Whenever gem or Scheduler in Heroku

To use autotriggered background processes in Ruby On Rails, we normally user gem like ‘Whenever‘. Its very easy to use.

Using single command like `whenever -i` will update your cron tab. To see your current Cron status you can simply use command like `whenever -l` or `crontab -l`.

Cron In Heroku

Looks like heroku does not support linux cron. Instead you need to use new add-on called Scheduler i which you need to manually set the jobs you want to run and frequency and timing.

Scheduler is an add-on for running jobs on your app at scheduled time intervals, much like cron in a traditional server environment.

Scheduler Dashboard

Installing the add-on

To use the Heroku Scheduler, install the add-on:

$ heroku addons:create scheduler:standard

Dyno-hour costs

Scheduler runs one-off dynos that will count towards your usage for the month. Dyno-hours from Scheduler tasks are counted just like those from heroku runor from scaled dynos. They will appear with a “scheduler” dyno type in your Heroku invoice.

Scheduling jobs

To schedule a frequency and time for a job, open the Scheduler dashboard by finding the app in My Apps, clicking “General Info”, then selecting “Scheduler” from the Add-ons drop down. The dashboard can also be opened from the command:

$ heroku addons:open scheduler

On the Scheduler Dashboard, click “Add Job…”, enter a task, select a frequency,dyno size, and next run time.

Inspecting output

Logs for scheduled jobs go into your logs as process scheduler.X:

$ heroku logs --ps scheduler.1
2011-02-04T14:10:16-08:00 heroku[scheduler.1]: State changed from created to starting
2011-02-04T14:10:16-08:00 app[scheduler.1]: Starting process with command `bin/clean_sessions`
2011-02-04T14:10:19-08:00 app[scheduler.1]: Deleting stale sessions...
2011-02-04T14:10:27-08:00 app[scheduler.1]: done.
2011-02-04T14:10:28-08:00 heroku[scheduler.1]: State changed from up to complete

The scheduled dyno is also visible with the heroku ps command:

$ heroku ps
=== scheduler: `bin/clean_sessions`
scheduler.1: complete for 5m

=== web: `bundle exec thin start -p $PORT -e production`
web.1: idle for 3h

How much would I be charged?

Well, every time you run scheduled task, or task is auto run, Heroku starts a One-Off Dyno. Similar like whenever you run `heroku run` command. You will be charged of how much time you kept the dyno open. So if you dont need it just close the heroku terminal.

Any time spent executing a one-off dyno will contribute to usage and will be charged just like any other dyno.

Cost

Dyno Type Price/dyno-month
free $0
hobby $7
standard-1x $25
standard-2x $50
performance-m $250
performance-l $500

Databases and add-ons are prorated to the second based on their applicable monthly fee.

When executing a one-off dyno with heroku run, a dyno will be provisioned for your command, and the time spent executing the command will accrue usage.

Sources

https://devcenter.heroku.com/articles/usage-and-billing#cost

https://devcenter.heroku.com/articles/scheduler

http://kvz.io/blog/2007/07/29/schedule-tasks-on-linux-using-crontab/

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s