简体   繁体   English

Capistrano回滚打破只要Cronjob

[英]Capistrano Rollback breaking Whenever Cronjob

The issue seems to be that during a capistrano deploy the created cron job has RAILS_ENV=staging as expected since the deployment environment is staging . 问题似乎是在capistrano部署期间,由于部署环境为staging因此创建的cron作业具有RAILS_ENV=staging符合预期。 However, in a capistrano rollback the created cron job has RAILS_ENV=new_staging where new_staging is the capistrano stage being rolled back. 但是,在capistrano回滚中,创建的cron作业具有RAILS_ENV=new_staging ,其中new_staging是要回滚的capistrano阶段。

my schedule file 我的日程表文件

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

My deploy/new_staging.rb file 我的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

And my deploy.rb 还有我的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
  • Whenever Version (0.9.7) 每当版本(0.9.7)
  • Capistrano Version (2.12.0) Capistrano版本(2.12.0)

What is causing the rollback to use the capistrano stage instead of the rails_env when running Whenever gem? 是什么原因导致回滚在运行Everyever gem时使用capistrano阶段而不是rails_env? And how can I get it to properly use the rails_env? 以及如何正确使用rails_env?

I'm a little confused by your staging setup (what is the difference between staging and new_staging?). 我对您的暂存设置有些困惑(暂存和new_staging有什么区别?)。 That said, you should be able to do this by changing your whenever_environment to use rails_env instead of the stage value set by capistrano: 这就是说,你应该能够通过改变你做到这一点whenever_environment使用rails_env代替stage由Capistrano的设定值:

so 所以

set :whenever_environment, defer { stage }

Would become: 会成为:

set :whenever_environment, defer { rails_env }

whenever README 每当自述文件

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

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