title: Rails+Capistrano: Sidekiq doesn't always start when new code is deployed
body: <p>I am sometimes having issues with Sidekiq when deploying new code with using Capistrano.</p>
  
  <p>This is my setup (<code>deploy/production.rb</code>):</p>
  
  <pre><code>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 =&gt; 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 =&gt; :web, :except =&gt; { :no_release =&gt; true } do
        begin
          from = source.next_revision(current_revision) # &lt;-- Fail here at first-time deploy because of current/REVISION absence
        rescue
          err_no = true
        end
        if err_no || capture("cd #{latest_release} &amp;&amp; #{source.local.log(from)} vendor/assets/ app/assets/ | wc -l").to_i &gt; 0
          run %Q{cd #{latest_release} &amp;&amp; #{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
  </code></pre>
  
  <p>What sometimes happens is that I deploy some code and one time Sidekiq is running and the other time it doesn't. I'd say the ratio is that 1/5 cases Sidekiq is not running (is not restarted) after deploying new code.</p>
  
  <p>How to ensure that Sidekiq will always be running (properly restarted) after every deployment?</p>
  
  <p>Thank you</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p><code>deploy.rb</code>:</p>
  
  <pre><code>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
  </code></pre>
  
  <p><strong>EDIT2:</strong>
  Also, I am not sure if this is related, but sometimes when I change something in the <code>/config</code> folder, I need to log in to the (Ubuntu) server, kill the unicorn process and manually start the server to see the change.</p>