如何使用无论何时 gem 创建相同的 cron 作业以用于登台和生产?
我在 schedule.rb
中有一项工作:
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 作业的明智方法是什么?
I have one job in schedule.rb
:
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
In my staging deployment (bash) script I have this line to write to cron:
ssh $SERVER "cd $DEPLOY_TO && whenever --set environment=staging -w"
And this line in the production deployment script:
ssh $SERVER "cd $DEPLOY_TO && whenever --set environment=production -w"
This works fine and creates the job when I deploy either environment. The problem is that whenever sees them both as one job so it gets overwritten by whichever environment was last deployed:
# 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
and...
# 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
What's a sensible way to add the same cron job for two separate environments on the same server?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用类似于以下内容的方式命名您的每当任务:
在上面的示例中,
stage
是包含环境的变量。将其更改为您正在使用的任何内容。https://github.com/javan/whenever 处的 Capistrano 集成 部分如果您需要的话,会更详细一些。
You can namespace your whenever tasks by using something similar to the following:
In the above example,
stage
is the variable that contains the environment. Change it to whatever you are using.The Capistrano integration section at https://github.com/javan/whenever goes into a bit more detail if you need it.
对于 capistrano-v3-integration,将
require "whenever/capistrano"
添加到Capfile
并设置set :whenever_identifier, ->{ " #{fetch(:application)}_#{fetch(:stage)}" }
到config/deploy.rb
For capistrano-v3-integration add
require "whenever/capistrano"
toCapfile
and setset :whenever_identifier, ->{ "#{fetch(:application)}_#{fetch(:stage)}" }
toconfig/deploy.rb