Rails: Running a scheduler

Heroku offer's a pretty nifty scheduler add-on for running jobs on your app at scheduled time intervals, much like cron in a traditional server environment. If you ever need to create something similar in a non-Heroku environment, here's how using whenever.

Step 1

First create a simple Ruby on Rails application. If you're not familiar with creating a Rails app, check out this tutorial

rails _5.x.x_ new cron_job --api -c -m -O -T -d postgresql

Step 2 - Install Whenever

Install the whenever gem for cron jobs. Place this within your Gemfile.

gem 'whenever', :require => false

Then install the gems.

bundle install

Step 3 - Run Whenever

Run this command within your terminal. This will create a new file within config/schedule.rb

wheneverize

Step 4 - Configure Whenever

Add a scheduled to config/schedule.rb.

every :day, :at => '12:20pm' do
   rake 'orders:cleanup'
end

Step 5 - Finalize your schedule

It will set the properly generated config le to the cron config.

whenever -w