This tutorial will show you how to add a scheduled job to Rails 5 and Heroku. It only takes a few minutes.
1. To run scheduled jobs on Heroku, they require you to enable their free add-on, Heroku Scheduler. Add it to your app via the command line:
heroku addons:create scheduler:standard
2. Inside your Rails 5 app, create a file inside libs/tasks called scheduler.rake.
desc "Short description of the task goes here" task hello_world: :environment do puts "Hello world!" end
3. Now, let’s tell Scheduler to run
hello_world. First, open it with this command:
heroku addons:open scheduler
4. Now we’ll create our first job.
- Under Schedule, set how often you’d like this task to run. (If you want to create a job that recurs weekly, you can refer to my post on running Heroku’s Scheduler weekly.)
- Under Run Command, type
rake hello_world(or the name of your task from Step 2).
And that’s it! You now have a scheduled job that recurs as often as you’d like.