Created
August 17, 2011 20:34
-
-
Save chrisk/1152538 to your computer and use it in GitHub Desktop.
Example crontab generation in Capistrano
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This crontab is automatically generated on deploy | |
SHELL=/bin/bash | |
[email protected] | |
RAILS_ENV=<%= stage %> | |
RAKE_CMD="<%= "#{bundle_path} rake --rakefile #{current_path}/Rakefile --trace" %>" | |
LOG_FILE="<%= "#{current_path}/log/cron.log" %>" | |
# Note: times are in the user's local time | |
<% if stage == :production && primary_server -%> | |
* * * * * $RAKE_CMD task_for_one_host_in_production >> $LOG_FILE 2>&1 | |
<% end -%> | |
<% if primary_server -%> | |
* * * * * $RAKE_CMD task_for_one_host_in_all_envs >> $LOG_FILE 2>&1 | |
<% end -%> | |
* * * * * $RAKE_CMD task_for_all_hosts_in_all_envs >> $LOG_FILE 2>&1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace :deploy do | |
desc "Remove the remote user's crontab" | |
task :remove_crontab, :roles => :app do | |
run "crontab -r; true" # ignore non-zero status when there is no crontab | |
end | |
desc "Generate and install crontabs" | |
task :install_crontab, :roles => :app do | |
bundle_path = nil | |
run("which bundle", :once => true) { |_, _, output| bundle_path = output.strip } | |
# generate two crontabs: one for the primary server, and one for the rest | |
primary_server = true | |
primary_crontab = ERB .new(File.read("lib/tasks/crontab.erb"), nil, '-').result(binding) | |
primary_server = false | |
app_crontab = ERB.new(File.read("lib/tasks/crontab.erb"), nil, '-').result(binding) | |
tmp_crontab_path = "#{current_path}/lib/tasks/crontab" | |
put primary_crontab, tmp_crontab_path, :only => {:primary => true} | |
begin | |
put app_crontab, tmp_crontab_path, :except => {:primary => true} | |
rescue Capistrano::NoMatchingServersError | |
puts " * no additional app servers in this environment; skipping the non-primary crontab" | |
end | |
run "crontab #{tmp_crontab_path} && rm -f #{tmp_crontab_path}" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment