capistrano-plugins / capistrano-unicorn-nginx

Capistrano tasks for automatic and sensible unicorn + nginx configuration
MIT License
175 stars 81 forks source link

Unicorn is restarted at the wrong time #59

Closed vedant1811 closed 7 years ago

vedant1811 commented 9 years ago

My cap deploy runs fine but I get (111: Connection refused) in my nginx logs after my deploy is complete (hence 502 errors). From the output of cap deploy I can see that both nginx and unicorn have been restarted.

I have to run a cap unicorn:restart explicitly after cap deploy to fix this.

my Capfile:

# Load DSL and set up stages
require 'capistrano/setup'

# Include default deployment tasks
require 'capistrano/deploy'

# Load custom tasks from `lib/capistrano/tasks` if you have any defined
Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r }

require 'capistrano/delayed-job'
require 'capistrano/rvm'
require 'capistrano/rails'
require 'capistrano/unicorn_nginx'
require 'capistrano/secrets_yml'
require 'capistrano/cookbook/check_revision'

my deploy.rb:

# config valid only for current version of Capistrano
lock '3.4.0'

set :application, 'instano-api'
set :repo_url, 'git@bitbucket.org:vedanta/instano-api.git'

# Default branch is :master
ask :branch, `git rev-parse --abbrev-ref HEAD`.chomp
set :user, 'ubuntu'

# Default deploy_to directory is /var/www/my_app_name
set :deploy_to, "/home/#{fetch(:user)}/apps/#{fetch(:application)}"

# Default value for :scm is :git
# set :scm, :git

# Default value for :format is :pretty
# set :format, :pretty

# Default value for :log_level is :debug
# set :log_level, :debug

# Default value for :pty is false
set :pty, true

# Default value for :linked_files is []
set :linked_files, fetch(:linked_files, []).push('config/database.yml', 'config/secrets.yml')

# Default value for linked_dirs is []
set :linked_dirs, fetch(:linked_dirs, []).push('log', 'tmp/pids', 'tmp/cache', 'tmp/sockets', 'vendor/bundle', 'public/system')

# Default value for default_env is {}
# set :default_env, { path: "/opt/ruby/bin:$PATH" }

# Default value for keep_releases is 5
set :keep_releases, 5

# unicorn_nginx options
# ==================

set :unicorn_workers, 4

# Custom SSH Options
# ==================
# You may pass any option but keep in mind that net/ssh understands a
# limited set of options, consult the Net::SSH documentation.
# http://net-ssh.github.io/net-ssh/classes/Net/SSH.html#method-c-start
#
# Global options
# --------------
set :ssh_options, {
  keys: %w(/home/vedant/.ssh/xmpp-user.pem),
  forward_agent: true
}

set :rvm_ruby_version, '2.1.0'

set :delayed_job_command, "bin/delayed_job"
set :rails_env, "production" #added for delayed job

before :deploy, 'deploy:check_revision'

namespace :deploy do

  after :finished, :restart_rpush do
    on roles(:web) do
      within release_path do
        with rails_env: fetch(:rails_env) do
          execute :bundle, :exec, "rpush stop -e #{fetch(:rails_env)} ; true"
          execute :bundle, :exec, "rpush start -e #{fetch(:rails_env)}"
        end
      end
    end
  end

  after :restart, :clear_cache do
    on roles(:web), in: :groups, limit: 3, wait: 10 do
      # Here we can do anything such as:
      # within release_path do
      #   execute :rake, 'cache:clear'
      # end
    end
  end

  desc "build missing paperclip styles"
  task :build_missing_paperclip_styles do
    on roles(:app) do
      execute "cd #{current_path}; RAILS_ENV=production bundle exec rake paperclip:refresh:missing_styles"
    end
  end
end
denstepa commented 8 years ago

+1 Did you find the solution ?

cweilemann commented 8 years ago

+1 for me as well. I think it has to do with the sudo service unicorn_xyz_env restart call.

When I run sudo service unicorn_xyz_env restart with unicorn running, it simply kills unicorn, resulting in 502s. If I run the same command again, unicorn starts properly. Repeating again, simply kills unicorn, and so on...

I'm wondering if this is an issue with unicorn...?

pascalhuynh commented 8 years ago

+1 any solution ?

hector commented 8 years ago

It happens the same to me, it seems to be have something with the Unicorn init.d script. I think this is the same issue: https://github.com/capistrano-plugins/capistrano-unicorn-nginx/issues/62 Unicorn signals: https://unicorn.bogomips.org/SIGNALS.html

hector commented 8 years ago

I fixed the problem in my server. See the explanation in the pull-request https://github.com/capistrano-plugins/capistrano-unicorn-nginx/pull/93. Until it gets merged you can use my fork.

Preen commented 7 years ago

This is merged now.