John
John

Reputation: 397

rails use capistrano to deploy and restart?

I try to use capistrano to deploy my app, all problem in setting capistrano were fixed, except...

I can't auto restart server after deploy, here is my code:

gemfile:

gem 'capistrano-rails', '~> 1.1.3'#, group: :development
gem 'capistrano', '~> 3.1'
gem 'capistrano-rbenv', '~> 2.0'
gem 'capistrano-bundler', '~> 1.1.2'
gem 'capistrano-passenger', '~> 0.1.1'
gem 'capistrano3-delayed-job', '~> 1.0'
gem 'capistrano3-nginx', '~> 2.0'

capfile:

require 'capistrano/setup'
require 'capistrano/deploy'

require 'capistrano/rbenv'
require 'capistrano/bundler'
require 'capistrano/rails/assets'
require 'capistrano/rails/migrations'
require 'capistrano/passenger'
require 'capistrano/delayed-job'
require 'capistrano/nginx'

Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r }

deploy.rb:

require "whenever/capistrano"
`ssh-add` # need this to make key-forwarding work

set :whenever_identifier, ->{ "#{fetch(:application)}_#{fetch(:stage)}" }

set :application, 'devops'
set :repo_url, 'mygit'

set :rbenv_type, :user 
set :rbenv_ruby, "2.2.2"
set :rbenv_path, "/home/john/.rbenv"
set :rbenv_prefix, "RBENV_ROOT=#{fetch(:rbenv_path)} RBENV_VERSION=#{fetch(:rbenv_ruby)} #{fetch(:rbenv_path)}/bin/rbenv exec"
set :rbenv_map_bins, %w(rake gem bundle ruby rails)
set :rbenv_roles, :all 

set :deploy_to, '/home/john/devops'
set :log_level, :debug

set :linked_dirs, fetch(:linked_dirs, []).push("bin", "log", "tmp/pids", "tmp/cache", "tmp/sockets", "vendor/bundle", "public/system") 

deploy.rb

namespace :deploy do

# I try following code:
#---
after :deploy, cap nginx:restart 
run "sudo /etc/init.d/nginx restart"
run "touch tmp/restart.txt"
after :deploy, cap production passenger:restart
after :deploy, cap production deploy:restart
#---

# invoke 'delayed_job:restart'

  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
end

P.S. when I type "touch tmp/restart.txt" on local(after cap production deploy), my page doesn't change with my modification, I always need to use "sudo /etc/init.d/nginx restart", how can I fix this problem?

I try this, but also no response(no error msg):

after 'deploy:publishing', 'deploy:restart'

namespace :deploy do

    desc "Restart application"

    after :publishing, 'deploy:restart'

  task :restart do
    on roles(:app), in: :sequence, wait: 1 do
      execute :touch, release_path.join("tmp/restart.txt")
    end
  end

end

Upvotes: 1

Views: 2219

Answers (1)

Vasfed
Vasfed

Reputation: 18474

For passenger to restart app you should touch/restart.txt on server, not local:

namespace :deploy do

  desc 'Restart application'
  task :restart do
    on roles(:app), in: :sequence, wait: 5 do
      # Restarts Phusion Passenger
      execute :touch, release_path.join('tmp/restart.txt')
    end
  end

end

There's no point in restarting nginx itself, unless you're upgrading passenger for example.

Also restart is not instant, requests are routed to new code only after it has started

Upvotes: 2

Related Questions