Rails Capistrano - 每当Cron为每个部署添加cron作业

时间:2016-07-15 08:09:27

标签: ruby-on-rails cron capistrano whenever whenever-capistrano

我正在使用:

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

每当部署发生时,它会在每次部署后向crontab文件添加条目。自5次部署以来,crontab -l中有40个条目,因为schedule.rb有8个cron条目。每个版本都有不同的条目。它是否应该默认覆盖条目?

每次在schedule.rb文件中提及时重新创建条目。

2 个答案:

答案 0 :(得分:1)

我发现每当在crontab文件中添加一个cron作业时,每个cron作业都由一个注释行分隔,该注释行包含capistrano版本目录的路径......如下所示:

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

(您可以使用crontab -e查看原始crontab文件以查看每次放入的内容)

当下一次部署发生时,每当查看是否有注释分隔的cron作业,但使用 new 版本号时。它没有找到,所以它将新的作业附加到crontab文件。

我解决此问题的方法是在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'

因此crontab文件中的注释分隔符包含“当前”路径,而不是“releases / nnnnnnnnnnn”路径。

我怀疑这不应该是必要的,但在尝试解决问题一段时间之后,这就是我最终的结果。

答案 1 :(得分:0)

检查您是否设置

def fn(input):
    # do some processing

def detect_object(input):
   ...
   p = mp.Process(target=fn, args=(input))
   p.start() # instead of calling fn a new instance of the parent C++ application is started

如果不这样做,它将选择默认标识符,这是config / schedule.rb文件的扩展路径。

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

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