Using Heroku Scheduler to Automate Tasks for Rails 5.x Applications
- Tutorial Overview
- Part 1: Create a Heroku App with the Heroku Scheduler Add-on
- Part 2: Create a Scheduled Rake Task
- Reference of Terms
Some applications need tasks to occur at set intervals. Examples of this may include:
- A daily email update to active users
- An hourly update of forecast data for a weather-related app
- A recalculation of an application's top trending hashtag every ten minutes
A task is a set of instructions to perform a job. The task could be a read or write to the application's database, a request to an API, or any job that can be routinely executed by code.
Ruby on Rails applications have a task management tool called Rake. Rake tasks are written in a file with a .rake extension, called a Rake file. They can be run with the command
rake <task_name>, i.e.:
This tutorial describes how to run Rake tasks for Rails 5.x applications deployed through the Heroku platform. Heroku is a cloud-based web-hosting service used to manage web application deployments.
Heroku has a free add-on called Heroku Scheduler, which is used to handle basic task scheduling. Without a task scheduler, a Rake task can be manually run from the command line. With the Heroku Scheduler, a task can be run automatically at specified times.
Notes about Heroku Scheduler:
- Tasks can only be run daily, hourly, or every ten minutes with Scheduler. For cases where different time intervals are needed, use a custom clock process.
- In rare cases, jobs scheduled with Heroku Scheduler may be skipped or run twice. Check Heroku's documentation for alternatives and task monitoring options.
Part 1: Create a Heroku App with the Heroku Scheduler Add-on
Through Heroku's admin dashboard, Scheduler can be added to an app, and tasks can be scheduled. This tutorial shows how to create a new Heroku project and add Scheduler.
Web application admin (non-developer)
You must have a Heroku account to complete this tutorial.
Creating a Heroku App
- From the Heroku dashboard click the new button and select create new app.
- You'll be prompted to name your app. After creating the app, you can provide a custom domain if you choose. Otherwise, your app will be hosted at your-app-name.herokuapp.com.
- Make sure your region is selected from the dropdown, and click Create app.
Adding the Heroku Scheduler
- From the dashboard, select the Overview tab and click Configure Add-ons.
- Under Add-ons, search for Heroku Scheduler.
- Upon selecting Heroku Scheduler, agree to the Terms of Service.
- Scheduler will be added and in the Overview dashboard, Scheduler will be visible under Installed add-ons.
Part 2: Create a Scheduled Rake Task
In order for Heroku Scheduler to run custom Rake tasks, a Rails application must have one or more predefined tasks. This tutorial shows how to add Rake tasks to a Rails app, push the code a Heroku app, and schedule the tasks with Heroku Scheduler.
This Github repository contains a demo Rails application with a rake file containing two tasks, which will be referenced throughout the tutorial.
Ruby on Rails Developer
The following must be installed:
Before beginning this tutorial, you must have a Rails 5 application with a Github repository. Note that in order to deploy on Heroku, the app must have a PostgreSQL database (Rails apps are created with a sqlite3 database by default).
If you don't have an existing app, you can fork and clone this Github repository, which includes a demo app with a rake file. The demo app has one model
dinosaur and a route, '/dinosaurs', which renders JSON data containing information about all dinosaurs in the database. Check the app's model, routes, and controller to see how this is set up.
Creating a Rake Task
- Within the Rails application, create a new file:
- Within the rakefile, create one or more tasks.
desc "Restore dinosaurs" task :restore_dinos => :environment do puts "Restoring dinos..." Dinosaur.restore puts "done" end
A rake task is made up of the following parts:
desc <task_description>explains what the task will do.
task <:symbolized_task_name>names the task. (This one is called restore_dinos.)
=> :environmentloads the entire Rails app by default, giving access to models, etc.
- The task is written within the code block. This example prints a message before calling the
.restoremethod on the Dinosaur model. After finishing, it prints 'done'.
- Test the rake task locally by running the command
Pushing to Github and Deploying to Heroku
pushthe code to Github. Code is deployed to Heroku through Github, so the Github repository must have a deployment-ready branch (in this case the master branch) to push to Heroku.
If your app is already deployed to Heroku, simply run the command
git push heroku masterand skip to Testing and Scheduling the Rake Task.
Otherwise, deploy the app to Heroku by first running the following command, which connects your repository to the app created on Heroku. Be sure to use the name that you set when creating the app on the Heroku dashboard.
heroku git:remote -a your-app-name
Push the code from the Github repository's master branch to Heroku.
git push heroku master
Migrate the database on Heroku.
heroku run rake db:migrate
Seed the database, if applicable.
heroku rake db:seed
Heroku apps use Dynos to run processes for each deployed application. Dynos run web processes and perform jobs (such as a Rake task). When the app is deployed, ensure a Dyno is running the web processes.
heroku ps:scale web=1
Open the application in browser to confirm the deployment worked.
See Heroku documentation for more information about deployment.
Testing and Scheduling the Rake Task
Test that the task runs properly on Heroku without errors.
heroku run rake restore_dinos
The task can be scheduled to run using the app's Heroku dashboard. From the Overview dashboard, click Heroku Scheduler under Installed Add-ons.
Click Add New Job.
rakecommand for the task you want to run in the text field.
Select the frequency you want the task to run from the dropdown.
- For daily tasks, select the UTC time you want the task to run.
- For hourly tasks, select the minutes on the hour you want the task to run.
You'll be redirected to the scheduler dashboard, where you can edit or remove the task, or create a new task.
See Heroku documentation for more information about Heroku Scheduler.
Reference of Terms
Dynos: containers used to run web processes and perform jobs for apps deployed through Heroku
Git: a version control system used widely in software development
Github: a web-hosted Git version control system
Heroku: a cloud-based web-hosting service used to manage web application deployments
Heroku Scheduler: a free Heroku add-on used to handle basic task scheduling
PostgreSQL: an object-relational database system
Rake task: a Ruby sub-routine written in a Rake file
Ruby on Rails: a web application framework with a model-view-controller structure