Schedule Tasks Using ‘Whenever’ Gem- Ruby on Rails

Hima Chitalia
Coffee and Codes
Published in
4 min readSep 20, 2017
Whenever Gem — Schedule rake tasks

We always try to give 110% of ours to any project we are working on. Making stuff and seeing the instant result online gives me immense satisfaction. So, for while working on the final project for the Flatiron, I tried my best to impress my instructors. During the review, my instructor pointed out one thing that I didn’t think of!

Every time, a user wants to see industry news, my React front end will send a fetch request to back-end RoR API and it will send another fetch call to Guardian API. Which was not the ideal solution. Technically, the instructor wanted me to save fetch results in a database and after some regular interval only send fetch request to Guardian API. Otherwise, every time when the user wants to see industry news, send JSON news objects from the database.

Huhhhh.. So, I was in a situation I never thought about. I realized one thing, somehow I needed to schedule a fetch all to Guardian API once a day. And save the result, I receive in return. Search to find the perfect solution, dropped me to ‘Whenever’ Gem that allows you to easily schedule tasks using a simple tool called a cron log.

According to Wikipedia, “ The software utility Cron is a time-based job scheduler in Unix-like computer operating systems. People who set up and maintain software environments use cron to schedule jobs (commands or shell scripts) to run periodically at fixed times, dates, or intervals. It typically automates system maintenance or administration — though its general-purpose nature makes it useful for things like downloading files from the Internet and downloading email at regular intervals.”

So, technically you can set up cron tasks independently of any web application but Whenever enables you to manipulate standard functionality of your machine using Ruby.

To get started with Whenever, you need to install the gem. Like any other gem, this is a pretty simple installation process:

  • Install Whenever Gem

Installing gem is pretty straightforward. Use command gem install whenever to install the gem.

  • Wheneverize

Once you have installed the gem, you will need schedule.rb file in your config folder which you will be using to manage your application’s automated tasks. For this, just go into the root directory of your application use the command wheneverize . to create a file for you.

  • Create Rake Task

Creating a rake task is the key step of this entire process. There are several ways to create a rake task, but the one I like is to just use rake generator:

rails g task my_namespace my_task

It will generate scaffold for our new rake task in lib/tasks/my_namespace.rake

namespace :my_namespace do
desc “TODO”
task :my_task => :environment do
end
end

Up here I just gave you a skeleton to fill up. You can write here whatever code you may want rake to execute. You can change names of the files suitable for your application. For example, in my app, I wanted rake to run a method on Article model. So, my file looks like this:

namespace :news do
desc “Rake task to get news article”
task :fetch => :environment do
puts “Updating news Articles…”
Article.getArticles()
puts “#{Time.now} — Success!”
end
end

I like to use few puts statements here. So, whenever I see a log, I will see exact details of rake tasks. We can make sure this rake task exists and we are able to use them. Write command rake -T | grep my_namespace It should give you a list of all of your rake tasks for my_namespace file.

  • Schedule Rake Task

Now we need to use Whenever to schedule our rake task. We will write our code now in the schedule.rb file that Whenever automatically created in our config folder with the command wheneverize .

Whenever uses a human-readable syntax every [x].[minute/hour/day/week/etc.], :at => [time] do to schedule tasks. The gem has pretty good support for specialized tasks. It even supports scheduling tasks on just weekdays or weekends.

In the following code, I have set up Whenever to run my news:fetch rake task everyday at 9:00 am. I have also set the environment to be my development environment. I also like Whenever to send log to the file whenever.log in log folder. So, I asked Whenever to also do that.

ENV[‘RAILS_ENV’] = “development”set :output, ‘log/whenever.log’
every 1.day, :at => ‘10:20 am’ do
rake “news:fetch”
end
  • Update the crontab

After setting up the rake task, you need to run whenever --update-crontab command in the console to tell Whenever to start running your rake tasks.

You should see a result something like this:

[write] crontab file updated

If you want to check that the gem correctly scheduled your cron task, you can use the command crontab -l to list all cron tasks.

That’s all for this. Now Whenever will do its job to periodically run rake task for you and you will have all the time in this world to concentrate on other important things.

If you are planning to deploy your app on Heroku or if you already have a deployed app on Heroku. This won’t work there. Heroku has its own scheduler to schedule rake tasks. You can find all information about it here.

Any suggestion or question, please feel free to write me at hima.chhag@gmail.com or in comment section below.

Happy Coding!

--

--