[Awesome Ruby Gem] Use whenever gem to cron jobs in Ruby
whenever
Whenever is a Ruby gem that provides a clear syntax for writing and deploying cron jobs.
Installation
You can install it as a gem:
1 | gem install whenever |
or add it into a Gemfile (Bundler):
1 | # Gemfile |
Then, run bundle install
.
1 | bundle install |
Getting started
1 | cd /apps/my-great-project |
This will create an initial config/schedule.rb
file for you (as long as the config folder is already present in your project).
The whenever command
The whenever command will simply show you your schedule.rb file converted to cron syntax. It does not read or write your crontab file.
1 | cd /apps/my-great-project |
To write your crontab file for your jobs, execute this command:
1 | whenever --update-crontab |
Other commonly used options include:
1 | whenever --user app # set a user as which to install the crontab |
Note: If you run the whenever --update-crontab
without passing the --user
attribute, cron will be generated by the current user. This mean tasks that needs other user permission will fail.
You can list installed cron jobs using crontab -l
.
Run whenever --help
for a complete list of options for selecting the schedule to use, setting variables in the schedule, etc.
Example schedule.rb file
1 | every 3.hours do # 1.minute 1.day 1.week 1.month 1.year is also supported |
Define your own job types
Whenever ships with three pre-defined job types: command, runner, and rake. You can define your own with job_type.
For example:
1 | job_type :awesome, '/usr/local/bin/awesome :task :fun_level' |
Would run /usr/local/bin/awesome
party extreme every two hours. :task
is always replaced with the first argument, and any additional :whatevers are replaced with the options passed in or by variables that have been defined with set.
The default job types that ship with Whenever are defined like so:
1 | job_type :command, ":task :output" |
Pre-Rails 3 apps and apps that don’t use Bundler will redefine the rake and runner jobs respectively to function correctly.
If a :path is
not set it will default to the directory in which whenever was executed. environment_variable
will default to RAILS_ENV
. :environment
will default to production
. :output
will be replaced with your output redirection settings which you can read more about here: http://github.com/javan/whenever/wiki/Output-redirection-aka-logging-your-cron-jobs
All jobs are by default run with bash -l -c 'command...'
. Among other things, this allows your cron jobs to play nice with RVM by loading the entire environment instead of cron’s somewhat limited environment. Read more: http://blog.scoutapp.com/articles/2010/09/07/rvm-and-cron-in-production
You can change this by setting your own :job_template
.
1 | set :job_template, "bash -l -c ':job'" |
Or set the job_template to nil
to have your jobs execute normally.
1 | set :job_template, nil |
Parsing dates and times
Whenever uses the Chronic gem to parse the specified dates and times.
You can set your custom Chronic configuration if the defaults don’t fit you.
For example, to assume a 24 hour clock instead of the default 12 hour clock:
1 | set :chronic_options, hours24: true |
You can see a list of all available options here: https://github.com/mojombo/chronic/blob/master/lib/chronic/parser.rb
Customize email recipient with the MAILTO environment variable
Output from the jobs is sent to the email address configured in the MAILTO environment variable.
There are many ways to further configure the recipient.
Example: A global configuration, overriding the environment’s value:
1 | env 'MAILTO', '[email protected]' |
Example: A MAILTO configured for all the jobs in an interval block:
1 | every 3.hours, mailto: '[email protected]' do |
Example: A MAILTO configured for a single job:
1 | every 3.hours do |
Capistrano integration
Use the built-in Capistrano recipe for easy crontab updates with deploys. For Capistrano V3, see the next section.
In your config/deploy.rb
file:
1 | require "whenever/capistrano" |
Take a look at the recipe for options you can set. https://github.com/javan/whenever/blob/master/lib/whenever/capistrano/v2/recipes.rb For example, if you’re using bundler do this:
1 | set :whenever_command, "bundle exec whenever" |
If you are using different environments (such as staging
, production
), then you may want to do this:
1 | set :whenever_environment, defer { stage } |
The capistrano variable :stage
should be the one holding your environment name. This will make the correct :environment available in your schedule.rb
.
If both your environments are on the same server you’ll want to namespace them, or they’ll overwrite each other when you deploy:
1 | set :whenever_environment, defer { stage } |
If you use a schedule at an alternative path, you may configure it like so:
1 | set :whenever_load_file, defer { "#{release_path}/somewhere/else/schedule.rb" } |
Capistrano V3 Integration
In your `Capfile file:
1 | require "whenever/capistrano" |
Take a look at the load:defaults task (bottom of file) for options you can set. For example, to namespace the crontab entries by application and stage do this in your config/deploy.rb
file:
1 | set :whenever_identifier, ->{ "#{fetch(:application)}_#{fetch(:stage)}" } |
The Capistrano integration by default expects the :application variable to be set in order to scope jobs in the crontab.
Capistrano roles
The first thing to know about the new roles support is that it is entirely optional and backwards-compatible. If you don’t need different jobs running on different servers in your capistrano deployment, then you can safely stop reading now and everything should just work the same way it always has.
When you define a job in your schedule.rb file, by default it will be deployed to all servers in the whenever_roles list (which defaults to [:db]
).
However, if you want to restrict certain jobs to only run on subset of servers, you can add a roles: [...]
argument to their definitions. Make sure to add that role to the whenever_roles list in your deploy.rb.
When you run cap deploy, jobs with a :roles
list specified will only be added to the crontabs on servers with one or more of the roles in that list.
Jobs with no :roles argument will be deployed to all servers in the whenever_roles list. This is to maintain backward compatibility with previous releases of whenever.
So, for example, with the default whenever_roles of [:db]
, a job like this would be deployed to all servers with the :db
role:
1 | every :day, at: '12:20am' do |
If we set whenever_roles to [:db, :app] in deploy.rb, and have the following jobs in schedule.rb:
1 | every :day, at: '1:37pm', roles: [:app] do |
Here are the basic rules:
-
If a server’s role isn’t listed in
whenever_roles
, it will never have jobs added to its crontab. -
If a server’s role is listed in the
whenever_roles
, then it will have all jobs added to its crontab that either list that role in their :roles arg or that don’t have a:roles
arg. -
If a job has a
:roles
arg but that role isn’t in thewhenever_roles
list, that job will not be deployed to any server.
References
[1] javan/whenever: Cron jobs in Ruby - https://github.com/javan/whenever
[2] whenever | RubyGems.org | your community gem host - https://rubygems.org/gems/whenever/
[3] Home · javan/whenever Wiki - https://github.com/javan/whenever/wiki