4

I want to deploy my rails through Capistrano with Puma and Nginx. I have configured deploy.rb for puma and added required gems in gem files.

I am able to run initial deploy command as 'cap production deploy:initial' and able to access my rails app as described below.

But when I want to deploy some new changes or restart puma it fails and gave this error.

Gemfile:

gem 'capistrano', '~> 3.4.0'
gem 'capistrano-rvm',     require: false
gem 'capistrano-rails',   require: false
gem 'capistrano-bundler', require: false
gem 'capistrano3-puma',   require: false
# gem 'capistrano-passenger',   require: false
gem 'capistrano-ext',     require: false
gem 'capistrano-faster-assets', '~> 1.0.2'

Capfile:

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

# Include default deployment tasks
require 'capistrano/deploy'
require 'capistrano/rails'
require 'capistrano/bundler'
require 'capistrano/rails/assets'
require 'capistrano/rails/migrations'
require 'capistrano/faster_assets'

require 'capistrano/rvm'
require 'capistrano/puma'
require 'capistrano/puma/workers'
require 'capistrano/puma/nginx'
Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r }

deploy.rb

# Puma Server Configuration
set :puma_threads,    [4, 16]
set :puma_workers,    1

# Don't change these unless you know what you're doing
set :pty,             true
set :use_sudo,        false
set :puma_bind,       "unix://#{shared_path}/tmp/sockets/puma.sock"
# set :puma_conf,       "#{shared_path}/puma.rb"
set :puma_state,      "#{shared_path}/tmp/pids/puma.state"
set :puma_pid,        "#{shared_path}/tmp/pids/puma.pid"
set :puma_access_log, "#{release_path}/log/puma.error.log"
set :puma_error_log,  "#{release_path}/log/puma.access.log"
set :puma_preload_app, true
set :puma_worker_timeout, nil
set :puma_init_active_record, true

namespace :puma do
  desc 'Create Directories for Puma Pids and Socket'
  task :make_dirs do
    on roles(:app) do
      execute "mkdir #{shared_path}/tmp/sockets -p"
      execute "mkdir #{shared_path}/tmp/pids -p"
    end
  end

  before :start, :make_dirs
end

namespace :deploy do
  desc "Make sure local git is in sync with remote."
  task :check_revision do
    on roles(:app) do
      unless `git rev-parse HEAD` == `git rev-parse origin/capistrano`
        puts "WARNING: HEAD is not the same as origin/capistrano"
        puts "Run `git push` to sync changes."
        exit
      end
    end
  end

  desc 'Initial Deploy'
  task :initial do
    on roles(:app) do
      before 'deploy:restart', 'puma:start'
      invoke 'deploy'
    end
  end

  desc 'Restart application'
  task :restart do
    on roles(:app), in: :sequence, wait: 5 do
      invoke 'puma:restart'
    end
  end

  before :starting,     :check_revision
  after  :finishing,    :compile_assets
  after  :finishing,    :cleanup
  after  :finishing,    :restart
end

I have also used the below command to generate template for Puma and Nginx as below.

rails g capistrano:nginx_puma:config

I have run below commands to deploy my rails to EC2 instance (with Ubuntu)

cap production deploy:check
cap production puma:config
cap production puma:nginx_config
cap production deploy:initial

Now, I want to deploy some changes with below code.

cap production deploy

But I am getting the error as below.

(Backtrace restricted to imported tasks)
cap aborted!
SSHKit::Runner::ExecuteError: Exception while executing as ubuntu@54.175.134.149: bundle exit status: 1
bundle stdout: No such file or directory - connect(2) for "/tmp/puma-status-1439451994589-14316"
bundle stderr: Nothing written

SSHKit::Command::Failed: bundle exit status: 1
bundle stdout: No such file or directory - connect(2) for "/tmp/puma-status-1439451994589-14316"
bundle stderr: Nothing written

Tasks: TOP => deploy:restart
(See full trace by running task with --trace)

Please help! Thanks

Lalit Kumar Maurya
  • 5,475
  • 2
  • 35
  • 29

1 Answers1

0

I found one workaround to fix this issue. Just add the snippet below to your deploy.rb file. It will override puma restart task.

Rake::Task["puma:restart"].clear_actions

namespace :puma do
  task :restart do
    on roles(:all) do
      execute "RACK_ENV=#{fetch(:rails_env)} #{fetch(:rvm_binary)} #{fetch(:rvm_ruby_version)} do pumactl -S #{shared_path}/tmp/pids/puma.state restart"
    end
  end
end
mpospelov
  • 1,510
  • 1
  • 15
  • 24