If you ran a rails app on a single server, all you had to do to schedule jobs is to configure a cron to invoke your rake tasks. Things are not so straightforward on restricted environments like Heroku, which are designed to scale horizontally.
The basic approach in Heroku to schedule jobs is to use the Heroku Scheduler add on. You can add the addon to your app
heroku addons:add scheduler:standard --app=<YOUR_HEROKU_APP_NAME>
Heroku expects the jobs you want to schedule to be in a file called scheduler.rake. Once you have created your rake task (let’s say it is called update_class_attendance), you can open the scheduler.
heroku addons:add scheduler:standard --app=<YOUR_HEROKU_APP_NAME>
This will open up the job configuration page in your browser. You can add your job by entering your task under the task column. You can also select the type of dynos as well the schedule for the task
rake update_class_attendance
Be aware that using the scheduler could incur additional costs as Heroku would spin off a new dyno to execute the task. Heroku also says that the scheduler is also not very reliable and should not be used for mission critical jobs. For critical jobs, use a clock process.
Leave a Reply