随着cron工作的部署而苦苦挣扎

时间:2014-10-14 06:49:23

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

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

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

这是我的配置: 的的Gemfile:

# 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文件中我不知道使用它的最佳做法是什么。

我很感激任何帮助...... 谢谢!

1 个答案:

答案 0 :(得分:0)

试试这个: 如果您之前未指定此内容,请将其添加到 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