<commit_before>== Introduction

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.

<NAME> created a great Railscast about Whenever: http://railscasts.com/episodes/164-cron-in-ruby

== Installation

Regular (non-Rails) install:
  
  $ gem sources -a http://gems.github.com  #you only need to run this once
  $ sudo gem install javan-whenever

In a Rails (2.1 or greater) application:
  
in your "config/environment.rb" file:

  Rails::Initializer.run do |config|
    config.gem 'javan-whenever', :lib => false, :source => 'http://gems.github.com'
  end
  
To install this gem (and all other missing gem dependencies), run rake gems:install (use sudo if necessary).

In older versions of Rails:

  $ gem sources -a http://gems.github.com  #you only need to run this once
  $ gem install javan-whenever

in your "config/environment.rb" file:

  Rails::Initializer.run do |config|
    ...
  end

  require 'whenever'
  
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.

== Getting started

  $ cd /my/rails/app
  $ wheneverize .

This will create an initial "config/schedule.rb" file you.

== Example schedule.rb file
  
  every 3.hours do
    runner "MyModel.some_process"       
    rake "my:rake:task"                 
    command "/usr/bin/my_great_command"
  end

  every 1.day, :at => '4:30 am' do 
    runner "MyModel.task_to_run_at_four_thirty_in_the_morning"
  end

  every :hour do # Many shortcuts available: :hour, :day, :month, :year, :reboot
    runner "SomeModel.ladeeda"
  end

  every :sunday, :at => '12pm' do # Use any day of the week or :weekend, :weekday 
    runner "Task.do_something_great"
  end

More examples on the wiki: http://wiki.github.com/javan/whenever/instructions-and-examples
  
== Cron output

  $ cd /my/rails/app
  $ whenever

And you'll see your schedule.rb converted to cron sytax

== Capistrano integration

In your "config/deploy.rb" file do something like:

  after "deploy:symlink", "deploy:update_crontab"

  namespace :deploy do
    desc "Update the crontab file"
    task :update_crontab, :roles => :db do
      run "cd #{release_path} && whenever --update-crontab #{application}"
    end
  end

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.

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.

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!

== Credit

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

While building Whenever, I learned a lot by digging through the source code of Capistrano - http://github.com/jamis/capistrano

== Feedback

Email me: javan [at] javan (dot) us

== License

Copyright (c) 2009 <NAME><commit_msg>Added info and link about setting variables on the fly.<commit_after>== Introduction

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.

Ryan Bates created a great Railscast about Whenever: http://railscasts.com/episodes/164-cron-in-ruby

== Installation

Regular (non-Rails) install:
  
  $ gem sources -a http://gems.github.com  #you only need to run this once
  $ sudo gem install javan-whenever

In a Rails (2.1 or greater) application:
  
in your "config/environment.rb" file:

  Rails::Initializer.run do |config|
    config.gem 'javan-whenever', :lib => false, :source => 'http://gems.github.com'
  end
  
To install this gem (and all other missing gem dependencies), run rake gems:install (use sudo if necessary).

In older versions of Rails:

  $ gem sources -a http://gems.github.com  #you only need to run this once
  $ gem install javan-whenever

in your "config/environment.rb" file:

  Rails::Initializer.run do |config|
    ...
  end

  require 'whenever'
  
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.

== Getting started

  $ cd /my/rails/app
  $ wheneverize .

This will create an initial "config/schedule.rb" file you.

== Example schedule.rb file
  
  every 3.hours do
    runner "MyModel.some_process"       
    rake "my:rake:task"                 
    command "/usr/bin/my_great_command"
  end

  every 1.day, :at => '4:30 am' do 
    runner "MyModel.task_to_run_at_four_thirty_in_the_morning"
  end

  every :hour do # Many shortcuts available: :hour, :day, :month, :year, :reboot
    runner "SomeModel.ladeeda"
  end

  every :sunday, :at => '12pm' do # Use any day of the week or :weekend, :weekday 
    runner "Task.do_something_great"
  end

More examples on the wiki: http://wiki.github.com/javan/whenever/instructions-and-examples
  
== Cron output

  $ cd /my/rails/app
  $ whenever

And you'll see your schedule.rb converted to cron sytax

== Capistrano integration

In your "config/deploy.rb" file do something like:

  after "deploy:symlink", "deploy:update_crontab"

  namespace :deploy do
    desc "Update the crontab file"
    task :update_crontab, :roles => :db do
      run "cd #{release_path} && whenever --update-crontab #{application}"
    end
  end

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.

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.

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!

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

== Credit

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

While building Whenever, I learned a lot by digging through the source code of Capistrano - http://github.com/jamis/capistrano

== Feedback

Email me: javan [at] javan (dot) us

== License

Copyright (c) 2009 <NAME>
{'fraction_non_alphanumeric': 0.0845225027442371, 'fraction_numerical': 0.005366508110745213, 'mean_word_length': 3.7813411078717203, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 38, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7405954', 'n_tokens_mistral': 2518, 'n_tokens_neox': 2365, 'n_words': 1029}