【问题标题】:Struggling with the deployment of whenever cron jobs努力部署每当 cron 作业
【发布时间】:2014-12-08 20:44:05
【问题描述】:

我有一个使用 Capistrano 3 部署的 Rails 4 应用程序。 最近我尝试使用 When Gem 添加一些 cron 作业。

问题基本上是在我部署时 - 没有一个 schedule.rb 作业不存在。另外,我没有看到日志路径中生成的任何日志文件-...path/to/app/shared/log/

这是我的配置: 宝石文件:

# Use whenever to schedule jobs using Cron
gem 'whenever', :require => false

Capfile:(根据任何文档添加适当的要求)

require 'whenever/capistrano'

deploy.rb:

### See great guid on this in http://www.talkingquickly.co.uk/2014/01/deploying-rails-apps-to-a-vps-with-capistrano-v3/ ###

# config valid only for Capistrano 3.1
lock '3.2.1'

# Set local variables:
application = 'eng_performance'
home = '/home/deploy'


set :application, application
set :repo_url, "git@gitlab.com:danklei/#{application}.git"
set :branch, 'SmartAdmin1_4_1'

# Set the server deploy path of the application
set :deploy_to, "/var/www/#{application}"

# Set linked-files in order to share secret files without deploying them to the DB.
set :linked_files, %w{config/database.yml config/jenkinsCredentials.yml}

# Set directories we want symlinking to share
set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system}

# Set what specs should run before deployment is allowed to continue (see lib/capistrano/tasks/run_tests)
set :tests, []

# Set which config files should be copied by deploy:setup_config
# see documentation in lib/capistrano/tasks/setup_config.cap
# for details of operations
set(:config_files, %w(
  database.example.yml
  jenkinsCredentials.example.yml
))

namespace :deploy do

  desc 'Restart application'
  task :restart do
    # on roles(:app), in: :sequence, wait: 5 do
    on roles(:web), in: :sequence, wait: 5 do
      # Restart Phusion Passenger
      info 'restarting Phusion Passenger service'
      execute :touch, release_path.join('tmp/restart.txt')

    end
  end

  desc "Update application's crontab entries using Whenever"
  task :update_crontab do
    setup_whenever_task do |host|
      roles = host.roles_array.join(",")
      [fetch(:whenever_update_flags),  "--roles=#{roles}"]
    end
  end

  after :publishing, :restart

  after :restart, :clear_cache do
    on roles(:web), in: :groups, limit: 3, wait: 10 do
      # Here we can do anything such as:
      # within release_path do
      #   execute :rake, 'cache:clear'
      # end
    end
  end

  after :clear_cache, :update_crontab

end

after :deploy, "app_setup:create_tests_log_symlink"

schedule.rb:

# Use this file to easily define all of your cron jobs.
#
# It's helpful, but not entirely necessary to understand cron before proceeding.
# http://en.wikipedia.org/wiki/Cron

set :output, {:error => "#{path}/log/cron_error_log.log", :standard => "#{path}/log/cron_log.log"}

every 1.minute do
    command "date"     <-- this is just a sanity job to make sure something started working

end

every 1.day, at: "8:42" do
    command "echo updating golden_results db relation with rake task: update_golden_results"
    rake "golden:update_golden_results[false,true]"
end

every :reboot, :roles => [:web] do
    rake "golden:refresh_golden_comparison"
end

every 1.day, at: "4am", :roles => [:web] do
    command "echo refreshing rails cache with rake task: golden:refresh_golden_comparison"
    rake "golden:refresh_golden_comparison"
end

every 1.week, at: "4am", :roles => [:web] do
    command "echo updating golden_results db relation with rake task: update_golden_results"
    rake "golden:update_golden_results[false,false]"
end

注意:我已将每当提供的 whenever.rake 文件添加到 /app/lib/capistrano/tasks/whenever.rake 并将 :update_crontab 任务复制到我的 deploy.rb 文件中,因为我不知道会是什么使用它的最佳实践。

如果有任何帮助,我将不胜感激... 谢谢!

【问题讨论】:

  • 您是否像这样在 deploy.rb 文件中添加了环境 set :whenever_environment, defer { stage #define either production or staging whichever you are using here }
  • @anusha,得到它并添加它。事实上,我现在可以看到日志文件已添加到我的/log 目录中。但我想我错过了什么。当我运行"cap production deploy" 时,every :reboot... 任务应该有效。但是什么也没发生.. 另外,你能推荐一下:update_crontab 任务的放置位置吗?谢谢!
  • 那么你可以在部署before :deploy, :update_crontab之前调用:update_crontab任务检查一次日志
  • 您建议使用哪个:update_crontab?我在whenever.rake 提供的whenever.rake 文件中定义的那个我完全添加到/app/lib/capistrano/tasks/whenever.rake 中。顺便说一句 - 我最终更改了 set :whenever_roles,-&gt;{ :web } 而不是环境,因为默认角色是 :db 单独。随意添加一个答案,如果一切正常,我将标记为正确的答案。
  • 对你有用吗

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


【解决方案1】:

试试这个: 如果您之前没有指定,请将其添加到您的 deploy.rb 文件

set :whenever_environment, defer { stage #define either production or staging whichever you are using here }

还有

deploy.rb

desc "Update application's crontab entries using Whenever"
  task :update_crontab do
   on roles(:web, :db, :app), in: :groups, limit: 3, wait: 10 do
    setup_whenever_task do |host|
      roles = host.roles_array.join(",")
      [fetch(:whenever_update_flags),  "--roles=#{roles}"]
    end
  end

   before :deploy, :update_crontab

更新:

当然我们可以,但是 sysntax 是不同的,就像这样

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

它在任何时候github链接也请检查一次https://github.com/javan/whenever

【讨论】:

  • 我误以为:reboot指的是应用程序(rails/passenger)服务重启,实际上它与实际机器重启有关。
猜你喜欢
  • 2016-11-18
  • 1970-01-01
  • 2021-07-31
  • 2019-02-20
  • 1970-01-01
  • 1970-01-01
  • 2013-10-20
  • 2016-03-01
  • 2015-03-22
相关资源
最近更新 更多