Whenever
This article will walk you through the process of creating a scheduled task/job with cron by using the whenever
Gem.
Cron together with whenever will help us automate recurring tasks.
Table of Contents
What is Whenever
The whenever gem simplifies cron jobs and gives a much more understandable syntax for writing them.
Cron
Cron is a command-line tool for scheduling jobs. It enables users to create tasks, also known as cron jobs, that run automatically at predefined intervals and are often in the form of commands or scripts.
How to install whenever
Reminder:
Make sure that your containers are up and running.
In your gemfile, add gem 'whenever'
.
gem 'whenever'
Then run bundle install.
root@0122:/usr/src/app# bundle install
Fetching gem metadata from https://rubygems.org/..........
Resolving dependencies...
...
Installing chronic 0.10.2
Fetching whenever 1.0.0
Installing whenever 1.0.0
Bundle complete! 28 Gemfile dependencies, 103 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.
Usage
Go to your project container the run bundle exec wheneverize .
to create an initial config/schedule.rb
file.
root@0122:/usr/src/app# bundle exec wheneverize .
[add] writing `./config/schedule.rb'
[done] wheneverized!
The commands in whenever
By using the whenever
command, it will display your schedule.rb
file in cron syntax. It neither reads nor writes to your crontab file.
root@0122:/usr/src/app# bundle exec whenever
## [message] Above is your schedule file converted to cron syntax; your crontab file was not updated.
## [message] Run `whenever --help' for more options.
Use the command whenever --update-crontab
to write your crontab file for your jobs.
root@0122:/usr/src/app# whenever --update-crontab
[write] crontab file updated
With command crontab -l
, you can have the list of cron jobs.
root@0122:/usr/src/app# crontab -l
# Begin Whenever generated tasks for: /usr/src/app/config/schedule.rb at: xxxx-xx-xx xx:xx:xx +0000
# End Whenever generated tasks for: /usr/src/app/config/schedule.rb at: xxxx-xx-xx xx:xx:xx +0000
Some samples are commented out in the schedule.rb
file for reference. One thing that must be added is a location where the cron tasks will be logged once they have completed. We can put ours in cron.log
, but we will customized it.
# config/schedule.rb
+ set :output, 'log/cron.log'
Cron job and interval
Whenever provides several job types for describing tasks. We’ll be using rake
, but have a look at the other choices below:
- runner
MyModel.sample_process
- rake
rake:task
- command
/usr/bin/sample_command
Let’s try making a sample task.
root@0122:/usr/src/app# rails g task sample welcome
create lib/tasks/sample.rake
Modify the sample task.
# lib/tasks/sample.rake
namespace :sample do
- desc "TODO"
- task welcome: :environment do
+ desc 'Print welcome'
+ task welcome: :development do
+ puts 'Welcome to cron'
end
end
Let’s do the sample task with 1 minute interval.
# config/schedule.rb
set :output, 'log/cron.log'
+ every 1.minute do
+ rake 'sample:welcome'
+ end
Following that, we will update our crontab to tell cron about our task.
Whenever default environment is production, if you want to set it to development, run the following:
root@0122:/usr/src/app# whenever --update-crontab --set environment='development'
[write] crontab file updated
That is all for whenever
Gem. Check out Whenever Documentation to know more.