ruby-on-rails - rails +Capistrano : Sidekiq doesn't always start when new code is deployed

标签 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 在运行,而另一次却没有。我会说这个比例是 1/5 的案例在部署新代码后 Sidekiq 没有运行(没有重新启动)。

如何确保 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

编辑 2: 另外,我不确定这是否相关,但有时当我在 /config 文件夹中更改某些内容时,我需要登录到(Ubuntu)服务器,杀死 unicorn 进程并手动启动服务器以查看更改。

最佳答案

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

There is a known bug that prevents sidekiq from starting when pty is true on Capistrano 3.

set :pty, false

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

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

关于ruby-on-rails - rails +Capistrano : Sidekiq doesn't always start when new code is deployed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36844927/

相关文章:

ubuntu - libstdc++.a 丢失

image - 无法从 cpp 代码 : `Segmentation fault (core dumped)` 运行 image_transport

amazon-web-services - 无法使用 terraform 在目标组中添加多个 target_id

java - 如何在 Amazon Web Services EC2 中安装 Tomcat

amazon-ec2 - EC2 上的 Sublime 文本 SFTP

ruby-on-rails - 用于动态页面布局的 Rails 应用程序中的多个布局与 css 欺骗与局部布局

ruby-on-rails - Rails redirect_to - 我也想重新加载页面

javascript - 页面加载时向用户发送消息?

javascript - 无法在 jQuery 中获取 Rails 返回值

reactjs - 禁止您无权访问此资源。使用 apache2 在 digitalocean 上部署时