2013-03-25 91 views
4

我想在我的rails應用程序中首次使用capistrano和獨角獸。我可以成功部署,除了麒麟不會與下面的錯誤從標準錯誤啓動:獨角獸PID在初始部署時陳舊

I, [2013-03-25T16:55:35.877323 #2378] INFO -- : reloading config_file=/var/www/good/current/config/unicorn.rb E, [2013-03-25T16:55:35.881496 #2378] ERROR -- : error reloading config_file=/var/www/good/current/config/unicorn.rb: Already running on PID:2378 (or pid=/var/www/good/current/tmp/pids/unicorn.pid is stale) (ArgumentError) E, [2013-03-25T16:55:35.881548 #2378] ERROR -- : /var/www/good/shared/bundle/ruby/1.9.1/gems/unicorn-4.6.2/lib/unicorn/http_server.rb:193:in `pid=' E, [2013-03-25T16:55:35.881579 #2378] ERROR -- : /var/www/good/shared/bundle/ruby/1.9.1/gems/unicorn-4.6.2/lib/unicorn/configurator.rb:111:in `block in commit!' E, [2013-03-25T16:55:35.881606 #2378] ERROR -- : /var/www/good/shared/bundle/ruby/1.9.1/gems/unicorn-4.6.2/lib/unicorn/configurator.rb:108:in `each' E, [2013-03-25T16:55:35.881645 #2378] ERROR -- : /var/www/good/shared/bundle/ruby/1.9.1/gems/unicorn-4.6.2/lib/unicorn/configurator.rb:108:in `commit!' E, [2013-03-25T16:55:35.881674 #2378] ERROR -- : /var/www/good/shared/bundle/ruby/1.9.1/gems/unicorn-4.6.2/lib/unicorn/http_server.rb:699:in `load_config!' E, [2013-03-25T16:55:35.881703 #2378] ERROR -- : /var/www/good/shared/bundle/ruby/1.9.1/gems/unicorn-4.6.2/lib/unicorn/http_server.rb:308:in `join' E, [2013-03-25T16:55:35.881731 #2378] ERROR -- : /var/www/good/shared/bundle/ruby/1.9.1/gems/unicorn-4.6.2/bin/unicorn:126:in `<top (required)>' E, [2013-03-25T16:55:35.881758 #2378] ERROR -- : /var/www/good/shared/bundle/ruby/1.9.1/bin/unicorn:23:in `load' E, [2013-03-25T16:55:35.881786 #2378] ERROR -- : /var/www/good/shared/bundle/ruby/1.9.1/bin/unicorn:23:in `<main>' I, [2013-03-25T16:55:36.233632 #2378] INFO -- : reaped 
#<Process::Status: pid 2392 exit 0> worker=1 I, [2013-03-25T16:55:36.234045 #2378] INFO -- : reaped 
#<Process::Status: pid 2397 exit 0> worker=3 I, [2013-03-25T16:55:36.234560 #2378] INFO -- : reaped 
#<Process::Status: pid 2394 exit 0> worker=2 I, [2013-03-25T16:55:36.336246 #8587] INFO -- : executing ["/var/www/good/shared/bundle/ruby/1.9.1/bin/unicorn", "-c", "/var/www/good/current/config/unicorn.rb", "-E", "production", "-D", {12=>#<Kgio::UNIXServer:fd 12>, 13=>#<Kgio::TCPServer:fd 13>}] (in /var/www/good/releases/20130325165445) I, [2013-03-25T16:55:36.336510 
#8587] INFO -- : forked child re-executing... I, [2013-03-25T16:55:38.475972 #2378] INFO -- : reaped 
#<Process::Status: pid 2389 exit 0> worker=0 I, [2013-03-25T16:55:38.476109 #2378] INFO -- : master complete 

關鍵是

錯誤重裝CONFIG_FILE = /無功/網絡/好/電流/配置/麒麟。 RB:對PID已經運行:2378

我已經包含的Gemfile,deploy.rb和unicorn.rb

我會很感激任何幫助或想法。我還使用RVM和Ubuntu

配置/ deploy.rb

require "rvm/capistrano" 
set :rvm_type, :system 
set :rvm_install_with_sudo, true 

require "bundler/capistrano" 
set :bundle_without, [:darwin, :development, :test] 

set :application, "Talking GOOD" 
set :repository, "[email protected]:FTW-Development/good.git" 
set :scm_user, "[email protected]" 
ssh_options[:forward_agent] = true 


#set :migrate_target, :current 
#set :ssh_options, { :forward_agent => true } 

#set :rails_env, "production" 
set :deploy_to, "/var/www/good" 
#set :normalize_asset_timestamps, false #what? 
set :user, "rails" 
set :group, "www" 
set :use_sudo, false 




set :keep_releases, 5 
after "deploy:restart", "deploy:cleanup" 

require 'capistrano-unicorn' 
#after 'deploy:restart', 'unicorn:reload' # app IS NOT preloaded 
after 'deploy:restart', 'unicorn:restart' # app preloaded 



#role :web, "your web-server here"       # Your HTTP server, Apache/etc 
#role :app, "your app-server here"       # This may be the same as your `Web` server 
#role :db, "your primary db-server here", :primary => true # This is where Rails migrations will run 
#role :db, "your slave db-server here" 



#set :stages, %(production, staging) 
#set :default_stage, "staging" 
#require 'capistrano/ext/multistage' 

#set(:unicorn_env) { rails_env } 
#set(:app_env)  { rails_env } 
#role(:web) { domain } 
#role(:app) { domain } 
#role(:db, :primary => true) { domain } 

#set(:deploy_to) { "/home/#{user}/#{application}/#{fetch :app_env}" } 
#set(:current_path) { File.join(deploy_to, current_dir) } 

server "good.ftwdev.com", :app, :web, :db, :primary => true 

配置/ unicorn.rb

app_path = "/var/www/good/current" 

# Set unicorn options 
worker_processes 1 
preload_app true 
timeout 180 
listen "127.0.0.1:9000" 

# Spawn unicorn master worker for user apps (group: apps) 
user 'rails.www' 

# Fill path to your app 
working_directory "/var/www/good/current" 

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

# 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" 

before_fork do |server, worker| 
    ActiveRecord::Base.connection.disconnect! 

    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 
end 

after_fork do |server, worker| 
    ActiveRecord::Base.establish_connection 
end 

的Gemfile

source 'https://rubygems.org' 

gem 'rails', '3.2.8' 

# Bundle edge Rails instead: 
# gem 'rails', :git => 'git://github.com/rails/rails.git' 

gem 'mysql2' 

gem 'devise', '~> 2.1.0' 
gem 'cancan' 

# Gems used only for assets and not required 
# in production environments by default. 
group :assets do 
    gem 'sass-rails', '~> 3.2.3' 
    gem 'coffee-rails', '~> 3.2.1' 
    gem 'bootstrap-sass', '~> 2.2.2.0' 

    # See https://github.com/sstephenson/execjs#readme for more supported runtimes 
    # gem 'therubyracer', :platforms => :ruby 

    gem 'uglifier', '>= 1.0.3' 
end 


gem 'jquery-rails' 
gem 'haml' 
gem 'hpricot' 
gem 'ruby_parser' 
gem 'simple_form' 
gem 'high_voltage' 
gem 'paperclip', '~> 3.0' 
gem 'twitter-bootstrap-rails' 


group :production do 
    gem 'rb-inotify', '~> 0.9' 
    gem 'execjs' 
    gem 'therubyracer' 
    gem 'unicorn', '~> 4.6' 
end 

group :development do 
    gem 'thin' 
    gem 'growl' 
    gem 'guard' 
    gem 'guard-bundler' 
    gem 'guard-livereload' 
    gem 'guard-rails' 
    gem 'guard-rspec' 
    gem 'rack-livereload' 
    gem 'html2haml' 
    gem 'capistrano' 
    gem 'capistrano-unicorn' 
    gem 'rvm-capistrano' 
    gem 'rb-fsevent', :require => false 
    gem 'rb-inotify', '~> 0.9', :require => false 
    gem 'rb-fchange', :require => false 
end 


#gem "rspec-rails", ">= 2.12.1", :group => [:development, :test] 
#gem "database_cleaner", ">= 0.9.1", :group => :test 
#gem "email_spec", ">= 1.4.0", :group => :test 
#gem "cucumber-rails", ">= 1.3.0", :group => :test, :require => false 
#gem "launchy", ">= 2.1.2", :group => :test 
#gem "capybara", ">= 2.0.2", :group => :test 
#gem "factory_girl_rails", ">= 4.1.0", :group => [:development, :test] 
#gem "bootstrap-sass", ">= 2.1.1.0" 
#gem "devise", ">= 2.2.0" 
#gem "cancan", ">= 1.6.8" 
#gem "rolify", ">= 3.2.0" 
#gem "simple_form", ">= 2.0.4" 
#gem "quiet_assets", ">= 1.0.1", :group => :development 
#gem "figaro", ">= 0.5.2" #used for configurations 
#gem "better_errors", ">= 0.2.0", :group => :development 
#gem "binding_of_caller", ">= 0.6.8", :group => :development 




# To use ActiveModel has_secure_password 
# gem 'bcrypt-ruby', '~> 3.0.0' 

# To use Jbuilder templates for JSON 
# gem 'jbuilder' 

# Use unicorn as the app server 
# gem 'unicorn' 

# Deploy with Capistrano 
# gem 'capistrano' 

# To use debugger 
# gem 'debugger' 
+0

嘗試殺死此進程(PID 2378),因爲它並不重要。 – 2013-03-25 17:49:16

+0

我相信這個過程是在那裏和那裏產生的。一旦cap停止運行,它就會消失......我試圖殺死所有的ruby進程(可能不是一個好主意),但這並沒有幫助。儘管我可能會導致這種情況,但我還是沒有任何想法。 – 2013-03-25 19:27:40

回答

6

你的問題是當你部署你在再次運行獨角獸之前不會殺死麒麟主控pid。我認爲問題出在你的卡皮斯特拉拉配方中...

隨着獨角獸我們通常會將pid保存在生產環境中的共享文件夾中,當我們啓動服務器時,還會保存日誌,上傳和其他獨立的sfuff的每個環境。

看到我deploy.rb之一,這兩個任務:

desc "Start unicorn" 
    task :start, :except => { :no_release => true } do 
    run "cd #{current_path} ; bundle exec unicorn_rails -c config/unicorn.rb -D" 
    run "ps aux | grep unicorn_rails | head -n 1 | awk '{print $2}' > #{deploy_to}/shared/tmp/pids/unicorn.pid" 
    end 

    desc "Stop unicorn" 
    task :stop, :except => { :no_release => true } do 
    run "kill -s QUIT `cat #{deploy_to}/shared/tmp/pids/unicorn.pid`" 
    end 

所以當我停止麒麟我總是殺法師PID是在共享文件夾,當我啓動它,我做一個副本這個pid到生產項目中的共享文件夾。 在這種情況下,我重起任務首先調用停止,然後啓動,但這不是零停機時間...

下面的鏈接有一個配方保持零停機時間,如果想給一個請嘗試: https://github.com/railscasts/373-zero-downtime-deployment/blob/master/blog-after/config/recipes/templates/unicorn.rb.erb

+1

您的零停機時間配方的鏈接已損壞。 – 2015-07-11 10:48:10

+0

我更新了Ryan Bates的零停機時間設置鏈接 – tbem 2015-07-11 13:54:03

0

陳舊的pid可能有三個原因: 1)沒有對unicorn.pid文件的寫入權限。檢查啓動獨角獸服務器時的錯誤日誌可以很容易地檢測到這一點。 2)在遠程機器的情況下:您還沒有將unicorn.pid添加到.gitignore,並且每次使用本地pid通過一個不自覺的提交強制覆蓋這些pid。 3)在config/unicorn.rb中存儲unicorn.pid的文件路徑與cap配方中config/deploy.rb中提供的路徑不同。

2

我有同樣的錯誤,原因是:

而彎腰的麒麟服務器寶石「capistrano3-麒麟」試圖殺死麒麟PROCESS_ID在TMP /的PID /麒麟。pid默認情況下,但是它在/shared/pids/unicorn.pid

通過更改的路徑unicorn.pidunicorn.rb解決了我的錯誤。