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.
To use the Heroku Scheduler, install the add-on:
$ heroku addons:create scheduler:standard
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.
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.
Logs for scheduled jobs go into your logs as process
$ 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.
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.