whenever/README.rdoc

173 lines
6.5 KiB
Plaintext
Raw Normal View History

2009-02-17 00:47:21 +00:00
== Introduction
2009-02-16 03:24:10 +00:00
2009-02-26 22:25:56 +00:00
Whenever is a Ruby gem that provides a clear syntax for defining cron jobs. It outputs valid cron syntax and can even write your crontab file for you. It is designed to work well with Rails applications and can be deployed with Capistrano. Whenever works fine independently as well.
2009-02-16 03:24:10 +00:00
2009-06-02 11:13:09 +00:00
Ryan Bates created a great Railscast about Whenever: http://railscasts.com/episodes/164-cron-in-ruby
2009-06-24 20:37:37 +00:00
Discussion: http://groups.google.com/group/whenever-gem
2009-02-16 03:24:10 +00:00
== Installation
2009-02-17 01:08:15 +00:00
Regular (non-Rails) install:
2009-02-26 22:25:56 +00:00
$ gem sources -a http://gems.github.com #you only need to run this once
2009-02-17 01:08:15 +00:00
$ sudo gem install javan-whenever
2009-02-17 00:47:21 +00:00
2009-02-17 01:08:15 +00:00
In a Rails (2.1 or greater) application:
2009-02-16 03:24:10 +00:00
in your "config/environment.rb" file:
Rails::Initializer.run do |config|
config.gem 'javan-whenever', :lib => false, :source => 'http://gems.github.com'
2009-02-16 03:24:10 +00:00
end
To install this gem (and all other missing gem dependencies), run rake gems:install (use sudo if necessary).
In older versions of Rails:
2009-02-26 22:25:56 +00:00
$ gem sources -a http://gems.github.com #you only need to run this once
2009-02-17 00:47:21 +00:00
$ gem install javan-whenever
2009-02-16 03:24:10 +00:00
in your "config/environment.rb" file:
Rails::Initializer.run do |config|
...
end
2009-02-17 01:08:15 +00:00
require 'whenever'
2009-02-16 03:24:10 +00:00
2009-02-26 22:25:56 +00:00
NOTE: Requiring the whenever gem inside your Rails application is technically optional. However, if you plan to use something like Capistrano to automatically deploy and write your crontab file, you'll need to have the gem installed on your servers, and requiring it in your app is one way to ensure this.
2009-02-17 01:08:15 +00:00
2009-02-16 03:24:10 +00:00
== Getting started
2009-02-17 00:47:21 +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.
== Example schedule.rb file
2009-02-26 22:25:56 +00:00
2009-02-18 18:29:20 +00:00
every 3.hours do
runner "MyModel.some_process"
rake "my:rake:task"
command "/usr/bin/my_great_command"
2009-02-16 03:24:10 +00:00
end
2009-02-18 18:29:20 +00:00
every 1.day, :at => '4:30 am' do
2009-02-26 22:25:56 +00:00
runner "MyModel.task_to_run_at_four_thirty_in_the_morning"
2009-02-16 03:24:10 +00:00
end
every :hour do # Many shortcuts available: :hour, :day, :month, :year, :reboot
2009-02-17 01:08:15 +00:00
runner "SomeModel.ladeeda"
2009-02-16 03:24:10 +00:00
end
every :sunday, :at => '12pm' do # Use any day of the week or :weekend, :weekday
2009-02-16 03:24:10 +00:00
runner "Task.do_something_great"
end
2009-02-18 18:29:20 +00:00
More examples on the wiki: http://wiki.github.com/javan/whenever/instructions-and-examples
== Output redirection
In your schedule.rb file you can specify the redirection options for your commands at a global or command level by setting the 'output' variable.
# adds ">> /path/to/file.log 2>&1" to all commands
set :output => '/path/to/file.log'
Or you can STDOUT and STDERR separately,
# adds ">> cron.log 2> error.log" to all commands
set :output => {:error => 'error.log', :standard => 'cron.log'}
# adds ">> cron.log" to all commands
set :output => {:standard => 'cron.log'}
2009-02-16 03:24:10 +00:00
# adds "2> error.log" to all commands
set :output => {:error => 'error.log'}
Additionally you can set these values at the command level,
every 3.hours do
runner "MyModel.some_process", :output => 'cron.log'
rake "my:rake:task", :output => {:error => 'error.log', :standard => 'cron.log'}
command "/usr/bin/cmd"
end
In all cases you can if you explicitly set the value of any output to 'nil' it will add a redirect to /dev/null
# adds ">> /dev/null 2>&1" to all commands
set :output => nil
set :output => {:error => nil, :standard => nil}
# adds ">> /dev/null" to all commands
set :output => {:standard => nil}
# adds "2> /dev/null" to all commands
set :output => {:error => nil}
2009-02-16 03:24:10 +00:00
== Cron output
2009-02-17 00:47:21 +00:00
$ cd /my/rails/app
$ whenever
2009-02-16 03:24:10 +00:00
And you'll see your schedule.rb converted to cron sytax
== Capistrano integration
In your "config/deploy.rb" file do something like:
2009-02-16 03:24:10 +00:00
after "deploy:symlink", "deploy:update_crontab"
2009-02-17 00:47:21 +00:00
namespace :deploy do
desc "Update the crontab file"
task :update_crontab, :roles => :db do
2009-03-16 23:10:41 +00:00
run "cd #{release_path} && whenever --update-crontab #{application}"
2009-02-17 00:47:21 +00:00
end
end
2009-03-16 23:10:41 +00:00
This will update your crontab file, leaving any existing entries unharmed. When using the <code>--update-crontab</code> option, Whenever will only update the entries in your crontab file related to the current schedule.rb file. You can replace the <code>#{application}</code> with any identifying string you'd like. You can have any number of apps deploy to the same crontab file peacefully given they each use a different identifier.
2009-03-16 23:10:41 +00:00
If you wish to simply overwrite your crontab file each time you deploy, use the <code>--write-crontab</code> option. This is ideal if you are only working with one app and every crontab entry is contained in a single schedule.rb file.
2009-03-16 23:10:41 +00:00
By mixing and matching the <code>--load-file</code> and <code>--user</code> options with your various :roles in Capistrano it is entirely possible to deploy different crontab schedules under different users to all your various servers. Get creative!
2009-02-17 19:38:37 +00:00
If you want to override a variable (like your environment) at the time of deployment you can do so with the <code>--set</code> option: http://wiki.github.com/javan/whenever/setting-variables-on-the-fly
2009-02-19 01:09:31 +00:00
== Credit
2009-02-26 22:25:56 +00:00
Whenever was created for use at Inkling (http://inklingmarkets.com) where I work. Their take on it: http://blog.inklingmarkets.com/2009/02/whenever-easy-way-to-do-cron-jobs-from.html
2009-02-19 01:09:31 +00:00
2009-02-19 01:10:14 +00:00
While building Whenever, I learned a lot by digging through the source code of Capistrano - http://github.com/jamis/capistrano
2009-02-19 01:09:31 +00:00
2009-06-24 20:37:37 +00:00
== Discussion / Feedback / Issues / Bugs
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
2009-06-24 20:37:37 +00:00
If you've found a genuine bug or issue, please use the Issues section on github: http://github.com/javan/whenever/issues
== License
Copyright (c) 2009 Javan Makhmali
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.