Rails + Capistrano:部署新代码时,Sidekiq并不总是启动

时间:2016-04-25 15:29:44

标签: ruby-on-rails ubuntu amazon-ec2 capistrano sidekiq

在使用Capistrano部署新代码时,我有时会遇到Sidekiq的问题。

这是我的设置(deploy/production.rb):

set :rails_env, "production"
set :stage, :production

set :whenever_command, "bundle exec whenever"
set :whenever_environment, defer { stage }
require "whenever/capistrano"

set :user, 'deployer'
set :use_sudo, false

before "deploy", "deploy:setup"
after "deploy:restart", "deploy:cleanup"

server "IP", :web, :app, :db, primary: true
set :deploy_to, "/home/deployer/apps/myapp-production/"

set :ssh_options, { :forward_agent => true }
set :keep_releases, 3

namespace :deploy do
  %w[start stop restart].each do |command|
    desc "#{command} unicorn server"
    task command, roles: :app, except: {no_release: true} do
      run "/etc/init.d/unicorn_myapp-production #{command}"
    end
  end
  task :setup_config, roles: [:app] do
    sudo "ln -nfs #{current_path}/config/nginx_production.conf /etc/nginx/sites-enabled/default"
    sudo "ln -nfs #{current_path}/config/unicorn_init_production.sh /etc/init.d/unicorn_myapp-production"
    run "mkdir -p #{shared_path}/config"
    put File.read("config/database.yml"), "#{shared_path}/config/database.yml"
    puts "Now edit the config files in #{shared_path}."
  end
  after "deploy:setup", "deploy:setup_config"

  task :symlink_config, roles: [:app] do
    run "ln -nfs #{shared_path}/config/database.yml #{release_path}/config/database.yml"
  end
  after "deploy:finalize_update", "deploy:symlink_config"
  after "deploy:create_symlink", "deploy:restart"

  desc "Make sure local git is in sync with remote."
  task :check_revision, roles: [:web] do
    unless `git rev-parse HEAD` == `git rev-parse origin/master`
      puts "WARNING: HEAD is not the same as origin/master"
      puts "Run `git push` to sync changes."
      exit
    end
  end

  namespace :assets do
    task :precompile, :roles => :web, :except => { :no_release => true } do
      begin
        from = source.next_revision(current_revision) # <-- Fail here at first-time deploy because of current/REVISION absence
      rescue
        err_no = true
      end
      if err_no || capture("cd #{latest_release} && #{source.local.log(from)} vendor/assets/ app/assets/ | wc -l").to_i > 0
        run %Q{cd #{latest_release} && #{rake} RAILS_ENV=#{rails_env} #{asset_env} assets:precompile}
      else
        logger.info "Skipping asset pre-compilation because there were no asset changes"
      end
    end
  end

  before "deploy", "deploy:check_revision"
end

有时会发生的是我部署了一些代码,一次Sidekiq正在运行,而另一次则没有。我说这个比例是在部署新代码后,Sidekiq没有运行(没有重启)的1/5个案例。

如何确保每次部署后Sidekiq始终正在运行(正确重启)?

谢谢

修改

deploy.rb

require "capistrano/ext/multistage"
require "rvm/capistrano"
require 'bundler/capistrano'
require 'delayed/recipes' # added for running deplayed jobs
require 'capistrano/sidekiq'

set :application, 'myapp'
set :bundle_flags, "--quiet --no-cache"

# Default value for :scm is :git
set :scm, :git
default_run_options[:pty] = true
set :deploy_via, :remote_cache
set :repository, 'git@bitbucket.org:username/myapp.git'
set :branch, "master"

set :normalize_asset_timestamps, false

set :pty, true

EDIT2: 此外,我不确定这是否相关,但有时当我在/config文件夹中更改某些内容时,我需要登录到(Ubuntu)服务器,终止unicorn进程并手动启动服务器以查看变化

1 个答案:

答案 0 :(得分:0)

这是Capistrano 3的一个已知问题,将pty设置为false应该可以解决问题。

  

Capistrano 3上的pty为true时,有一个已知的错误会阻止sidekiq启动。

     

set:pty,false

https://github.com/seuros/capistrano-sidekiq#known-issues-with-capistrano-3

相关问题: https://github.com/seuros/capistrano-sidekiq/issues/23