2011-10-02 14 views
1

我在schedule.rb一個任務:我該如何創建相同的cron作業,以便隨時隨地創建作品?

set :output, File.expand_path('../log/whenever.log', __FILE__) 
set :job_template, "bash -l -c 'source ~/.bashrc ; :job'" 

every 1.day, :at => '12:01 am' do 
    runner "MyModel.do_something" 
end 

在我的分期部署(bash)的腳本我有這行寫的cron:

ssh $SERVER "cd $DEPLOY_TO && whenever --set environment=staging -w" 

而且這條線在生產部署腳本:

ssh $SERVER "cd $DEPLOY_TO && whenever --set environment=production -w" 

這可以正常工作,並在部署任一環境時創建作業。問題是,每當看到他們既作爲一個工作,所以它得到由哪個環境覆蓋最後部署:

# Begin Whenever generated tasks for: /Users/simon/apps/myapp/staging/config/schedule.rb 
1 0 * * * bash -l -c 'source ~/.bashrc ; cd /Users/simon/apps/myapp/staging && script/rails runner -e staging 'MyModel.do_something' >> /Users/simon/apps/myapp/staging/log/whenever.log 2>&1' 

# End Whenever generated tasks for: /Users/simon/apps/myapp/staging/config/schedule.rb 

和...

# Begin Whenever generated tasks for: /Users/simon/apps/myapp/production/config/schedule.rb 
1 0 * * * bash -l -c 'source ~/.bashrc ; cd /Users/simon/apps/myapp/production && script/rails runner -e production 'MyModel.do_something' >> /Users/simon/apps/myapp/production/log/whenever.log 2>&1' 

# End Whenever generated tasks for: /Users/simon/apps/myapp/production/config/schedule.rb 

什麼是明智的方式添加相同cron作業爲同一臺服務器上的兩個單獨的環境?

回答

4

您可以命名空間,那麼無論何時使用類似下面的內容任務:

# Whenever 
set :whenever_environment, defer { stage } 
set :whenever_identifier, defer { "#{application}-#{stage}" } 
require "whenever/capistrano" 

在上面的例子中,stage是包含環境變量。將其更改爲您正在使用的任何內容。

卡皮斯特拉諾整合部分在https://github.com/javan/whenever進一步細節,如果你需要它。

0

對於Capistrano的-V3集成添加require "whenever/capistrano"Capfile並設置set :whenever_identifier, ->{ "#{fetch(:application)}_#{fetch(:stage)}" }config/deploy.rb