Ilya Cherevkov
Ilya Cherevkov

Reputation: 1803

Capistrano not really restarting a server?

I use Capistrano 2.15 and Nginx running on DigitalOcean droplet.

Sometimes, especially, when I install new gems or run migrations I need to hard reset my droplet to take my changes into effect.

This does not apply, if I simply change code somewhere, when running

cap deploy:restart 

is fine.

Also, when I log in into my machine and run

service nginx restart 

it does not help to capture new migrations.

deploy.rb

require "rvm/capistrano"

set :rvm_ruby_string, 'default'
set :rvm_type, :user

require "bundler/capistrano"

server "xx.xx.xx.xx", :web, :app, :db, primary: true

set :application, "xxx"
set :user, "deployer"
set :deploy_to, "/home/#{user}/apps/#{application}"
set :deploy_via, :remote_cache
set :use_sudo, false

set :scm, "git"
set :repository, "[email protected]:xxx/#{application}.git"
set :branch, "master"

default_run_options[:pty] = true
ssh_options[:forward_agent] = true

after "deploy", "deploy:cleanup" # keep only the last 5 releases

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_#{application} #{command}"
    end
  end

  task :setup_config, roles: :app do
    sudo "ln -nfs #{current_path}/config/nginx.conf /etc/nginx/sites-enabled/#{application}"
    sudo "ln -nfs #{current_path}/config/unicorn_init.sh /etc/init.d/unicorn_#{application}"
    run "mkdir -p #{shared_path}/config"
    put File.read("config/database.example.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
  desc "reload the database with seed data"
  task :seed do
    run "cd #{current_path}; bundle exec rake db:seed RAILS_ENV=#{rails_env}"
  end
  after "deploy:finalize_update", "deploy:symlink_config"

  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
  before "deploy", "deploy:check_revision"

  desc "tail production log files" 
  task :tail_logs, :roles => :app do
    trap("INT") { puts 'Interupted'; exit 0; }
    run "tail -f #{shared_path}/log/unicorn.log" do |channel, stream, data|
      puts  # for an extra line break before the host name
      puts "#{channel[:host]}: #{data}" 
      break if stream == :err
    end
  end

end


namespace :assets do

  task :symlink, roles: :web do
    run ("rm -rf #{latest_release}/public/assets &&
          mkdir -p #{latest_release}/public &&
          mkdir -p #{shared_path}/assets &&
          ln -s #{shared_path}/assets #{latest_release}/public/assets")
  end
end

namespace :uploads do

    desc <<-EOD
      Creates the upload folders unless they exist
      and sets the proper upload permissions.
    EOD
    task :setup, :except => { :no_release => true } do
      dirs = [File.join(shared_path,'uploads' )]

      run "#{try_sudo} mkdir -p #{dirs.join(' ')} && #{try_sudo} chmod g+w #{dirs.join(' ')}"
    end 

    desc <<-EOD
      [internal] Creates the symlink to uploads shared folder
      for the most recently deployed version.
    EOD
    task :symlink, :except => { :no_release => true } do
      run "rm -rf #{release_path}/public/uploads"
      run "ln -nfs #{shared_path}/uploads #{release_path}/public/uploads"
    end

    desc <<-EOD
      [internal] Computes uploads directory paths
      and registers them in Capistrano environment.
    EOD
    task :register_dirs do
      set :uploads_dirs,    %w(public/uploads)
      set :shared_children, fetch(:shared_children) + fetch(:uploads_dirs)
    end

    after       "deploy:finalize_update", "uploads:symlink"
    on :start,  "uploads:register_dirs"

  end

Here is unicorn_init:

http://pastebin.com/cm6NCupK

Upvotes: 0

Views: 116

Answers (1)

matanco
matanco

Reputation: 2129

when you deploy you use cap:deploy?

you use unicorn or passenger?

please paste your code here deploy.rb unicorn.rb(if you use unicorn) and unicorn_init.sh(or your sh file you create symlink from)

last question you use before_fork after_fork?(for zero downtime deploy?)

Upvotes: 1

Related Questions