2010-10-26 14:30:17 +00:00
### Introduction
2009-02-16 03:24:10 +00:00
2010-10-20 21:12:00 +00:00
Whenever is a Ruby gem that provides a clear syntax for writing and deploying cron jobs.
2009-06-24 20:37:37 +00:00
2010-10-26 14:30:17 +00:00
### Installation
2009-02-16 03:24:10 +00:00
2010-10-26 14:30:17 +00:00
$ gem install whenever
2009-02-17 01:08:15 +00:00
2010-10-20 21:12:00 +00:00
Or with Bundler in your Gemfile.
2010-10-26 14:30:17 +00:00
gem 'whenever', :require => false
2010-10-20 21:12:00 +00:00
2010-10-26 14:30:17 +00:00
### Getting started
2009-02-16 03:24:10 +00:00
2010-10-26 14:30:17 +00:00
$ cd /my/rails/app
$ wheneverize .
2009-02-16 03:24:10 +00:00
This will create an initial "config/schedule.rb" file you.
2010-10-26 14:30:17 +00:00
### Example schedule.rb file
2009-02-26 22:25:56 +00:00
2010-10-26 14:30:17 +00:00
every 3.hours do
runner "MyModel.some_process"
rake "my:rake:task"
command "/usr/bin/my_great_command"
end
2009-02-16 03:24:10 +00:00
2010-10-26 14:30:17 +00:00
every 1.day, :at => '4:30 am' do
runner "MyModel.task_to_run_at_four_thirty_in_the_morning"
end
2009-02-16 03:24:10 +00:00
2010-10-26 14:30:17 +00:00
every :hour do # Many shortcuts available: :hour, :day, :month, :year, :reboot
runner "SomeModel.ladeeda"
end
2009-02-16 03:24:10 +00:00
2010-10-26 14:30:17 +00:00
every :sunday, :at => '12pm' do # Use any day of the week or :weekend, :weekday
runner "Task.do_something_great"
end
2009-02-18 18:29:20 +00:00
2011-03-08 16:31:56 +00:00
every '0 0 27-31 * * ' do
command "echo 'you can use raw cron sytax too'"
end
2011-03-06 22:31:17 +00:00
2010-10-26 14:30:17 +00:00
More examples on the wiki: < http: / / wiki . github . com / javan / whenever / instructions-and-examples >
2009-09-18 14:50:16 +00:00
2010-10-26 14:30:17 +00:00
### Define your own job types
2010-06-28 20:22:16 +00:00
2010-10-20 21:12:00 +00:00
Whenever ships with three pre-defined job types: command, runner, and rake. You can define your own with `job_type` .
2010-06-28 20:22:16 +00:00
For example:
2010-10-26 14:30:17 +00:00
job_type :awesome, '/usr/local/bin/awesome :task :fun_level'
2010-06-28 20:22:16 +00:00
2010-10-26 14:30:17 +00:00
every 2.hours do
awesome "party", :fun_level => "extreme"
end
2010-06-28 20:22:16 +00:00
2010-10-20 21:12:00 +00:00
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` .
2010-06-28 20:22:16 +00:00
The default job types that ship with Whenever are defined like so:
2010-10-26 14:30:17 +00:00
job_type :command, ":task :output"
job_type :rake, "cd :path & & RAILS_ENV=:environment rake :task :output"
job_type :runner, "cd :path & & script/runner -e :environment ':task' :output"
2010-10-20 21:12:00 +00:00
If a script/rails file is detected (like in a Rails 3 app), runner will be defined to fit:
2010-10-26 14:30:17 +00:00
job_type :runner, "cd :path & & script/rails runner -e :environment ':task' :output"
2010-10-20 21:12:00 +00:00
2010-10-26 14:30:17 +00:00
If a `:path` is not set it will default to the directory in which `whenever` was executed. `: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 >
2010-10-20 21:12:00 +00:00
2010-10-26 14:30:17 +00:00
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 >
2010-10-20 21:12:00 +00:00
2010-10-26 14:30:17 +00:00
You can change this by setting your own `:job_template` .
2010-10-20 21:12:00 +00:00
2010-10-26 14:30:17 +00:00
set :job_template, "bash -l -c ':job'"
2010-10-20 21:12:00 +00:00
Or set the job_template to nil to have your jobs execute normally.
2010-06-28 20:22:16 +00:00
2010-10-26 14:30:17 +00:00
set :job_template, nil
2010-06-28 20:22:16 +00:00
2010-10-20 21:34:36 +00:00
And you'll see your schedule.rb converted to cron sytax. Note: running `whenever` with no options does not display your current crontab file, it simply shows you the output of converting your schedule.rb file.
2009-02-16 03:24:10 +00:00
2010-10-26 14:30:17 +00:00
### Capistrano integration
2009-02-16 03:24:10 +00:00
2010-10-18 22:51:27 +00:00
Use the built-in Capistrano recipe for easy crontab updates with deploys.
2009-02-16 03:24:10 +00:00
2010-10-18 22:51:27 +00:00
In your "config/deploy.rb" file:
2009-02-17 00:47:21 +00:00
2010-10-26 14:30:17 +00:00
require "whenever/capistrano"
2009-03-06 01:58:23 +00:00
2010-10-26 14:30:17 +00:00
Take a look at the recipe for options you can set. < http: / / github . com / javan / whenever / blob / master / lib / whenever / capistrano . rb >
2010-10-18 22:51:27 +00:00
For example, if you're using bundler do this:
2009-03-06 01:58:23 +00:00
2010-10-26 14:30:17 +00:00
set :whenever_command, "bundle exec whenever"
require "whenever/capistrano"
2009-03-06 01:58:23 +00:00
2010-10-26 14:30:17 +00:00
### The `whenever` command
2010-10-20 21:34:36 +00:00
2010-10-26 14:30:17 +00:00
$ cd /my/rails/app
$ whenever
2010-10-20 21:34:36 +00:00
This will simply show you your schedule.rb file converted to cron syntax. It does not read or write your crontab file. Run `whenever --help` for a complete list of options.
2009-03-06 01:58:23 +00:00
2010-10-26 14:30:17 +00:00
### Credit
2009-02-19 01:09:31 +00:00
2011-03-07 02:15:47 +00:00
Whenever was created for use at Inkling (< http: / / inklingmarkets . com > ). Their take on it: < http: / / blog . inklingmarkets . com / 2009 / 02 / whenever-easy-way-to-do-cron-jobs-from . html >
2009-02-17 19:38:37 +00:00
2010-10-26 14:30:17 +00:00
Thanks to all the contributors who have made it even better: < http: / / github . com / javan / whenever / contributors >
2009-02-19 01:09:31 +00:00
2010-10-26 14:30:17 +00:00
### Discussion / Feedback / Issues / Bugs
2009-06-24 20:37:37 +00:00
2010-10-26 14:30:17 +00:00
For general discussion and questions, please use the google group: < http: / / groups . google . com / group / whenever-gem >
2009-02-16 03:24:10 +00:00
2010-10-26 14:30:17 +00:00
If you've found a genuine bug or issue, please use the Issues section on github: < http: / / github . com / javan / whenever / issues >
2009-02-19 16:08:33 +00:00
2010-10-26 14:30:17 +00:00
Ryan Bates created a great Railscast about Whenever: < http: / / railscasts . com / episodes / 164-cron-in-ruby >
2010-10-20 21:12:00 +00:00
It's a little bit dated now, but remains a good introduction.
2010-10-26 14:30:17 +00:00
### License
2009-02-19 16:08:33 +00:00
2010-04-26 16:09:40 +00:00
Copyright (c) 2009+ Javan Makhmali
2009-02-19 16:08:33 +00:00
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.