Cron jobs in Ruby
Go to file
2009-02-19 11:51:18 -08:00
bin Added more opts to whenever binary. Moved code from rake tasks into bin files themselves and removed rake files. Bumped version to 0.1.4 2009-02-17 11:22:18 -08:00
lib Fixed load path to avoid conflicts with Rails files (Thanks Ryan Koopmans). Bumped to 0.1.5 2009-02-19 11:48:07 -08:00
test Got rid of already initialized constant warning. Improved version code. Improved test load paths. Bumped to 0.1.2 2009-02-16 18:32:55 -08:00
.gitignore Initial commit. 2009-02-15 19:24:10 -08:00
CHANGELOG.rdoc Fixed load path to avoid conflicts with Rails files (Thanks Ryan Koopmans). Bumped to 0.1.5 2009-02-19 11:48:07 -08:00
Rakefile Minor fixes. Bump to 0.1.3 2009-02-16 21:46:22 -08:00
README.rdoc Fixed load path to avoid conflicts with Rails files (Thanks Ryan Koopmans). Bumped to 0.1.5 2009-02-19 11:48:07 -08:00
whenever.gemspec Somehow dropped the gemspec, adding it again. 2009-02-19 11:51:18 -08:00

== Introduction

Whenever is a ruby gem that provides a ruby 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.

== 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, :version => '>= 0.1.5' :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 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 "DB.Backup"
  end

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

  every :sunday 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:write_crontab"

  namespace :deploy do
    desc "write the crontab file"
    task :write_crontab, :roles => :app do
      run "cd #{release_path} && whenever --write-crontab"
    end
  end
  
By mixing and matching the "--load-file" and "--user" 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!
  
USING THE "--write-crontab" OPTION WILL COMPLETELY OVERWRITE ANY EXISTING CRONTAB ENTRIES!

== Credit

Whenever was created for use at Inkling (http://inklingmarkets.com) where I work.

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

== Feedback

Lighthouse: http://javan.lighthouseapp.com/projects/25781-whenever/overview

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

== 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.