如何使用无论何时 gem 创建相同的 cron 作业以用于登台和生产?

发布于 2024-12-07 19:49:02 字数 1693 浏览 1 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

猫烠⑼条掵仅有一顆心 2024-12-14 19:49:02

您可以使用类似于以下内容的方式命名您的每当任务:

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

在上面的示例中,stage 是包含环境的变量。将其更改为您正在使用的任何内容。

https://github.com/javan/whenever 处的 Capistrano 集成 部分如果您需要的话,会更详细一些。

You can namespace your whenever tasks by using something similar to the following:

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

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.

眉黛浅 2024-12-14 19:49:02

对于 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" to Capfile and set set :whenever_identifier, ->{ "#{fetch(:application)}_#{fetch(:stage)}" } to config/deploy.rb

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文