简体   繁体   English

Rails Capistrano - 每当 Cron 为每个 Deployment 附加 cron 作业时

[英]Rails Capistrano - Whenever Cron appends cron jobs for every Deployment

I am using:我正在使用:

Ruby     1.9.3
whenever 0.9.4
Rails    3.2
and capistrano/whenever extension.

Whenever the deployment happens, it adds entries to the crontab file after each deployment.每当部署发生时,它都会在每次部署后向 crontab 文件添加条目。 Since 5 deployments, there are 40 entries in crontab -l , as schedule.rb has 8 cron entries.由于 5 次部署, crontab -l有 40 个条目,因为 schedule.rb 有 8 个 cron 条目。 For each release there are different entries.每个版本都有不同的条目。 Should it not overwrite the entries by default?默认情况下它不应该覆盖条目吗?

It recreates entries everytime mentioned in schedule.rb file.每次在schedule.rb文件中提到时,它都会重新创建条目。

I found that whenever was adding a cron job to the crontab file, with each cron job delimited by a comment line that includes the path to the capistrano releases directory... something like this:我发现每当向 crontab 文件添加一个 cron 作业时,每个 cron 作业都由一个注释行分隔,其中包括 capistrano 发布目录的路径......像这样:

# Begin Whenever generated tasks for: /home/path/www/to/releases/2070527160502/config/schedule.rb

(You can look at the raw crontab file with crontab -e to see what whenever has put in there) (您可以使用 crontab -e 查看原始 crontab 文件,以查看其中的内容)

When the next deploy occurs, whenever looks to see if there are comment-delimited cron jobs, but using the new release number.当下一次部署发生时,无论何时查看是否有注释分隔的 cron 作业,但使用新的版本号。 It doesn't find that, so it appends new jobs to the crontab file.它没有找到,所以它将新作业附加到 crontab 文件中。

My workaround for this problem is to specify the update_crontab task in deploy.rb with the explicit path to schedule.rb like this:我对此问题的解决方法是在 deploy.rb 中指定 update_crontab 任务,并使用 schedule.rb 的显式路径,如下所示:

namespace :deploy do
  task :update_crontab do
    on roles(:all) do
      within current_path do
        execute :bundle, :exec, :whenever, "--update-crontab", "~/path/to/current/config/schedule.rb"
      end
    end
  end
end
after 'deploy:symlink:release', 'deploy:update_crontab'

So the comment delimiters in the crontab file contain the 'current' path and not the 'releases/nnnnnnnnnnn' path.因此 crontab 文件中的注释分隔符包含“当前”路径而不是“releases/nnnnnnnnnnn”路径。

I suspect that this should not be necessary, but after trying to solve the problem for some time, this what what I ended up with.我怀疑这应该没有必要,但是在尝试解决问题一段时间后,这就是我的结果。

Check if you are setting检查您是否正在设置

set :whenever_identifier, ->{ "#{fetch(:application)}_#{fetch(:stage)}" }

If you do not it will pick the default identifier, which is the expanded path of your config/schedule.rb file.如果不这样做,它将选择默认标识符,即 config/schedule.rb 文件的扩展路径。

https://github.com/javan/whenever/blob/6e69dd8a6b3e2a8f4b2911b4efa5aab65cdc9dcb/lib/whenever/command_line.rb#L51 https://github.com/javan/whenever/blob/6e69dd8a6b3e2a8f4b2911b4efa5aab65cdc9dcb/lib/whenever/command_line.rb#L51

File.expand_path(@options[:file])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM