1

タイトルで書いたように、Unicorn マスター プロセスは最近 (デプロイして再起動した後) ワーカーを殺し、常に新しいワーカーを開始しているため、Rails アプリがまったく機能していません。その上、サーバーの CPU はその間 100% です。ただし、 cap unicorn:stop を作成すると、すべてが正しくシャットダウンされますが、 cap unicorn:start の後、最初からやり直します。

unicorn.rb:

# Should be 'production' by default, otherwise use other env 
rails_env = ENV['RAILS_ENV'] || 'production'

app_path = "/home/ruby/app/current"

listen 5000
# Spawn unicorn master worker for user ruby (group: www-data)
user 'ruby', 'ruby'
working_directory app_path

worker_processes 2
timeout 90
#preload_app true

# Log everything to one file
stderr_path "log/unicorn.log"
stdout_path "log/unicorn.log"

# Set master PID location
pid "#{app_path}/tmp/pids/unicorn.pid"

# To save some memory and improve performance
GC.respond_to?(:copy_on_write_friendly=) and GC.copy_on_write_friendly = true

# Force the bundler gemfile environment variable to   
# reference the Сapistrano "current" symlink
before_exec do |_|
   ENV["BUNDLE_GEMFILE"] = File.join(@app, 'Gemfile')
end

before_fork do |server, worker|
 # Replace with MongoDB or whatever
 if defined?(ActiveRecord::Base)
    ActiveRecord::Base.connection.disconnect!
    Rails.logger.info('Disconnected from ActiveRecord')
 end

 old_pid = "#{server.config[:pid]}.oldbin"
  if File.exists?(old_pid) && server.pid != old_pid
    begin
      Process.kill("QUIT", File.read(old_pid).to_i)
    rescue Errno::ENOENT, Errno::ESRCH
      # someone else did our job for us
    end
  end

  sleep 1
end

after_fork do |server, worker|
  # Replace with MongoDB or whatever
  if defined?(ActiveRecord::Base)
    ActiveRecord::Base.establish_connection
    Rails.logger.info('Connected to ActiveRecord')
  end

終わり

そして私の deploy.rb

require "bundler/capistrano"
require 'rvm/capistrano'
set :whenever_command, "bundle exec whenever"
require "whenever/capistrano"
set :default_shell, :bash
set :rvm_type, :system
set :rvm_ruby_string, '1.9.3-p194'
set(:rvm_path)          { "/usr/local/rvm" }

set :application, "investwallet"

# main details
role :web, "example.com"
role :app, "example.com"
role :db, "example.com"

# server details
default_run_options[:pty] = true
set :deploy_to, "/home/ruby/app"
set :deploy_via, :remote_cache
set :user, "ruby"
set :use_sudo, false
set :port, 22

# repo details
set :scm, :git
set :scm_username, "git"
#set :scm_passphrase, ""  # The deploy user's password
set :repository, "git@example.com:/opt/git/example.git"
set :branch, "master"
#set :git_enable_submodules, 1
set :keep_releases, 3

set(:releases_path)     { File.join(deploy_to, version_dir) }
set(:shared_path)       { File.join(deploy_to, shared_dir) }
set(:current_path)      { File.join(deploy_to, current_dir) }
set(:release_path)      { File.join(releases_path, release_name) }

default_environment["RAILS_ENV"] = 'production'

require 'capistrano-unicorn'

after 'deploy:update_code', 'deploy:symlink_db'
after 'deploy:update_code', 'deploy:symlink_letter_opener'
after "deploy:restart", "deploy:cleanup"
after 'deploy:restart', 'unicorn:reload' # app IS NOT preloaded
after 'deploy:restart', 'unicorn:restart'  # app preloaded

namespace :deploy do
  desc "Symlinks the database.yml"
  task :symlink_db, :roles => :app do
    run "ln -nfs #{deploy_to}/shared/config/database.yml #      {release_path}/config/database.yml"
 end

  desc "Symlinks the tmp letter opener"
  task :symlink_letter_opener, :roles => :app do
    run "ln -nfs #{shared_path}/letter_opener/ #{release_path}/tmp/letter_opener"
 end

終わり

namespace :unicorn do
  desc "Restart Unicorn"
  task :restart do
    unicorn.stop
    unicorn.start
  end

終わり

4

1 に答える 1