Automating Jobs with Rails Rake Tasks and Heroku Scheduler

  • rails, ruby, gems, tutorials
  • 0 Comments

I recently implemented Heroku Scheduler and Rails Rake tasks to automatically make API calls and update data in a side project I have called Power Pol. Rails and Heroku make it really easy to setup and deploy this functionality in minutes.

Install Heroku Scheduler

The first step is to install Heroku Scheduler by cd-ing into my app and typing this command in terminal: heroku addons:create scheduler:standard.

Create a Custom Rake Task

Next, I need to create the custom Rake tasks and I do this in lib/tasks/scheduler.rake. Originally created to manage software build processes, Rake enables me to define a set of tasks and their dependencies, and it automatically runs custom code when a given task is called. Each task consists of three things:

  • A description
  • A task name, represented by a Ruby symbol, that identifies the task
  • The code the task will execute in a Ruby do...end block
desc "This task calls the NYT API"
task :nyt_api_votes => :environment do
  politicians = Politician.all
  request = Unirest.get("http://api.nytimes.com/svc/politics/v3/us/legislative/congress/114/senate/members/current.json?api-key=#{Figaro.env.nyt_api_key}").body
    request["results"][0]["members"].each do |dw|
        politician = Politician.find_by(:bio_guide => dw["id"])
        if politician
        politician.update(:votes_with_party_pct => dw["votes_with_party_pct"], :missed_votes_pct => dw["missed_votes_pct"])
        end
    end
end

Every time I call the :nyt_api_votes task, the code in the block sends a request to the New York Times Congress API, updates the data for the politicians in my app, and saves it to the database.

Test Locally Before Deployment

I can test my Rake task locally before deploying it to Heroku, and this is as simple as firing up http://localhost:3000 and running heroku run rake nyt_api_votes from the command line. Heroku scheduler uses the same one-off dynos that Heroku run uses to execute jobs, so if my task works with with Heroku run, it will work with the scheduler.

Schedule Jobs in Heroku

Scheduling a job is as easy as going to https://scheduler.heroku.com/dashboard, clicking “Add a Job", and providing the name of my task rake nyt_api_votes. I also need to select a dyno size (for a simple app like mine 1X is fine), choose the frequency at which I want the task to run (daily, hourly, or every 10 minutes), and at what time I want the first task to execute.

With these simple steps, the politicians' data in my app will update automatically, eliminating the hassle of manually calling the API from Rails console.

Additional Info

The code block in the Rake task uses two gems I highly recommend: Unirest for making API calls and Figaro for handling sensitive information (like API keys and passwords) as environment variables.