Capistrano Rollback打破每当Cronjob

时间:2017-07-06 16:07:12

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

问题似乎是在capistrano部署期间,由于部署环境为RAILS_ENV=staging,因此创建的cron作业具有预期的staging。但是,在capistrano回滚中,创建的cron作业具有RAILS_ENV=new_staging,其中new_staging是要回滚的capistrano阶段。

我的日程文件

set :job_template, nil
job_type :rake,    "cd :path && :environment_variable=:environment bundle exec rake :task :output"

every 15.minute, roles: [:db] do
  rake "jobs:publish", output: lambda { "2>&1 >> /path/to/capistrano_directory/shared/log/jobs.log" }
end

我的deploy / new_staging.rb文件

set :branch, "develop"
set :deploy_env, 'staging'
set :rails_env, 'staging'

server "user@server_ip", :web, :app, :db, :metrics
role :db, "user@server_ip", :primary => true       # This is where Rails migrations will run
ssh_options[:forward_agent] = false                                # use local SSH keys remotely

ssh_options[:paranoid] = false

set :use_sudo, false
set :deploy_to, "/path/to/capistrano_directory"
set :unicorn_conf, "#{deploy_to}/current/config/environments/#{deploy_env}/unicorn.rb"
set :unicorn_pid, "#{deploy_to}/shared/pids/unicorn.pid"

before 'deploy:finalize_update', 'set_current_release'
before "deploy:finalize_update", "deploy:update_crontab"
after 'deploy:update_code', 'deploy:symlink_db'

namespace :deploy do
  task :symlink_db, :roles => :app do
    run "ln -nfs #{deploy_to}/shared/config/database.yml #{release_path}/config/database.yml"
  end

  task :start, :roles => :app, :except => { :no_release => true } do
    run "cd #{deploy_to}/current && bundle exec unicorn -c #{unicorn_conf} -E #{deploy_env} -D"
  end

  task :stop do
    run "#{try_sudo} kill -QUIT `cat #{unicorn_pid}`"
  end

  task :restart, :roles => :app, :except => { :no_release => true } do
    run "#{try_sudo} kill -s USR2 `cat #{unicorn_pid}`"
  end
end

task :set_current_release, :roles => :app, :except => { :no_release => true } do
  set :current_release, latest_release
end

我的deploy.rb

require 'bundler/capistrano'
require 'capistrano/ext/multistage'
set :whenever_command, "bundle exec whenever"
set :whenever_environment, defer { stage }
set :whenever_roles, [:db, :metrics]
require "whenever/capistrano"

set :stages, %w(production staging new_staging)

set :application, "###"
set :repository,  "###"
set :deploy_via, :remote_cache
set :scm, :git

default_run_options[:shell] = '/bin/bash --login'

ssh_options[:forward_agent] = false
ssh_options[:paranoid] = false


namespace :deploy do
  desc "Update the crontab file"
  task :update_crontab do
    run "cd #{release_path} && RAILS_ENV=#{fetch(:rails_env)} bundle exec whenever --update-crontab #{application} --set environment=#{fetch(:rails_env)}"
  end
end
  • 每当版本(0.9.7)
  • Capistrano版本(2.12.0)

什么导致回滚在运行Whenever gem时使用capistrano阶段而不是rails_env?我怎样才能正确使用rails_env?

1 个答案:

答案 0 :(得分:1)

我对您的暂存设置感到有点困惑(staging和new_staging之间的区别是什么?)。也就是说,您应该可以通过更改whenever_environment使用rails_env而不是capistrano设置的stage值来执行此操作:

所以

set :whenever_environment, defer { stage }

会变成:

set :whenever_environment, defer { rails_env }

whenever README