From 436255e4a58452b0a28448d4d9a16804854147fd Mon Sep 17 00:00:00 2001 From: Javan Makhmali Date: Mon, 18 Oct 2010 18:51:27 -0400 Subject: [PATCH] added a built-in capistrano recipe --- README.rdoc | 20 +++++++------------- lib/whenever/capistrano.rb | 31 +++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 13 deletions(-) create mode 100644 lib/whenever/capistrano.rb diff --git a/README.rdoc b/README.rdoc index c2bcb1c..665d05d 100644 --- a/README.rdoc +++ b/README.rdoc @@ -70,24 +70,18 @@ And you'll see your schedule.rb converted to cron sytax. Note: running `whenever == Capistrano integration -In your "config/deploy.rb" file do something like: +Use the built-in Capistrano recipe for easy crontab updates with deploys. - after "deploy:symlink", "deploy:update_crontab" +In your "config/deploy.rb" file: - namespace :deploy do - desc "Update the crontab file" - task :update_crontab, :roles => :db do - run "cd #{release_path} && whenever --update-crontab #{application}" - end - end + require "whenever/capistrano" -This will update your crontab file, leaving any existing entries unharmed. When using the --update-crontab option, Whenever will only update the entries in your crontab file related to the current schedule.rb file. You can replace the #{application} 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. +Take a look at the recipe for options you can set. http://github.com/javan/whenever/blob/master/lib/whenever/capistrano.rb +For example, if you're using bundler do this: -If you wish to simply overwrite your crontab file each time you deploy, use the --write-crontab option. This is ideal if you are only working with one app and every crontab entry is contained in a single schedule.rb file. + set :whenever_command, "bundle exec whenever" + require "whenever/capistrano" -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! - -If you want to override a variable (like your environment) at the time of deployment you can do so with the --set option: http://wiki.github.com/javan/whenever/setting-variables-on-the-fly == Credit diff --git a/lib/whenever/capistrano.rb b/lib/whenever/capistrano.rb new file mode 100644 index 0000000..d7d7c22 --- /dev/null +++ b/lib/whenever/capistrano.rb @@ -0,0 +1,31 @@ +Capistrano::Configuration.instance(:must_exist).load do + + _cset(:whenever_roles) { :db } + _cset(:whenever_command) { "whenever" } + _cset(:whenever_identifier) { application } + _cset(:whenever_update_flags) { "--update-crontab #{whenever_identifier}" } + _cset(:whenever_clear_flags) { "--clear-crontab #{whenever_identifier}" } + + # Disable cron jobs at the begining of a deploy. + after "deploy:update_code", "whenever:clear_crontab" + # Write the new cron jobs near the end. + after "deploy:symlink", "whenever:update_crontab" + # If anything goes wrong, undo. + after "deploy:rollback", "whenever:update_crontab" + + namespace :whenever do + desc "Update application's crontab entries using Whenever" + task :update_crontab, :roles => whenever_roles do + # Hack by Jamis to skip a task if the role has no servers defined. http://tinyurl.com/ckjgnz + next if find_servers_for_task(current_task).empty? + run "cd #{current_path} && #{whenever_command} #{whenever_update_flags}" + end + + desc "Clear application's crontab entries using Whenever" + task :clear_crontab, :roles => whenever_roles do + next if find_servers_for_task(current_task).empty? + run "cd #{release_path} && #{whenever_command} #{whenever_clear_flags}" + end + end + +end \ No newline at end of file