繁体   English   中英

Rails,Capistrano,Nginx,Unicorn - 应用程序已经初始化(RuntimeError)

[英]Rails, Capistrano, Nginx, Unicorn - Application has been already initialized (RuntimeError)

任何人都可以了解这个错误究竟是指什么?

我在部署新版本的网站时遇到了麻烦。

I,  INFO -- : reloading config_file=[snip]/current/config/unicorn.rb
I,  INFO -- : Refreshing Gem list
E, ERROR -- : error reloading config_file=[snip]/current/config/unicorn.rb: Application has been already initialized. (RuntimeError)
E, ERROR -- : [snip]/shared/bundle/ruby/1.9.1/gems/railties-3.2.3/lib/rails/application.rb:135:in `initialize!'
E, ERROR -- : [snip]/shared/bundle/ruby/1.9.1/gems/railties-3.2.3/lib/rails/railtie/configurable.rb:30:in `method_missing'
E, ERROR -- : [snip]/releases/20120907085937/config/environment.rb:5:in `<top (required)>'
E, ERROR -- : [snip]/shared/bundle/ruby/1.9.1/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:251:in `require'
E, ERROR -- : [snip]/shared/bundle/ruby/1.9.1/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:251:in `block in require'
E, ERROR -- : [snip]/shared/bundle/ruby/1.9.1/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:236:in `load_dependency'
E, ERROR -- : [snip]/shared/bundle/ruby/1.9.1/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:251:in `require'
E, ERROR -- : config.ru:4:in `block in <main>'
E, ERROR -- : [snip]/shared/bundle/ruby/1.9.1/gems/rack-1.4.1/lib/rack/builder.rb:51:in `instance_eval'
E, ERROR -- : [snip]/shared/bundle/ruby/1.9.1/gems/rack-1.4.1/lib/rack/builder.rb:51:in `initialize'
E, ERROR -- : config.ru:1:in `new'
E, ERROR -- : config.ru:1:in `<main>'
E, ERROR -- : [snip]/shared/bundle/ruby/1.9.1/gems/unicorn-4.3.1/lib/unicorn.rb:44:in `eval'
E, ERROR -- : [snip]/shared/bundle/ruby/1.9.1/gems/unicorn-4.3.1/lib/unicorn.rb:44:in `block in builder'
E, ERROR -- : [snip]/shared/bundle/ruby/1.9.1/gems/unicorn-4.3.1/lib/unicorn/http_server.rb:696:in `call'
E, ERROR -- : [snip]/shared/bundle/ruby/1.9.1/gems/unicorn-4.3.1/lib/unicorn/http_server.rb:696:in `build_app!'
E, ERROR -- : [snip]/shared/bundle/ruby/1.9.1/gems/unicorn-4.3.1/lib/unicorn/http_server.rb:677:in `load_config!'
E, ERROR -- : [snip]/shared/bundle/ruby/1.9.1/gems/unicorn-4.3.1/lib/unicorn/http_server.rb:303:in `join'
E, ERROR -- : [snip]/shared/bundle/ruby/1.9.1/gems/unicorn-4.3.1/bin/unicorn:121:in `<top (required)>'
E, ERROR -- : [snip]/shared/bundle/ruby/1.9.1/bin/unicorn:23:in `load'
E, ERROR -- : [snip]/shared/bundle/ruby/1.9.1/bin/unicorn:23:in `<main>'
I,  INFO -- : reaped #<Process::Status: pid 3182 exit 0> worker=0
I,  INFO -- : reaped #<Process::Status: pid 3185 exit 0> worker=1
I,  INFO -- : reaped #<Process::Status: pid 3188 exit 0> worker=2
I,  INFO -- : reaped #<Process::Status: pid 3191 exit 0> worker=3
I,  INFO -- : worker=0 ready
I,  INFO -- : worker=3 ready
I,  INFO -- : worker=1 ready
I,  INFO -- : worker=2 ready

Unicorn.rb

root = "/home/[user]/apps/[site]/current"
working_directory root
pid "#{root}/tmp/pids/unicorn.pid"
stderr_path "#{root}/log/unicorn.log"
stdout_path "#{root}/log/unicorn.log"

listen "/tmp/unicorn.[site].sock", :backlog => 2048

worker_processes 4

preload_app true

timeout 30

before_fork do |server, worker|
    defined?(ActiveRecord::Base) and ActiveRecord::Base.connection.disconnect!
end

after_fork do |server, worker|
    defined?(ActiveRecord::Base) and ActiveRecord::Base.establish_connection
end

任何帮助表示赞赏 - 如果需要,我可以提取更多配置文件。

终于到了底。 在重新阅读Ryan Bates的Railscasts剧集零停机部署之后 ,我注意到我发出的是带有HUP而不是USR2的unicorn restart / reload命令。 在改变这一点,并从ginettev的答案中重新启用代码(我之前已经禁用该代码以尝试理解该问题,我现在可以按照我的意愿进行部署。

这个更改就像更改我的unicorn_init.sh文件一样简单:

restart|reload)
sig HUP && echo reloaded OK && exit 0
echo >&2 "Couldn't reload, starting '$CMD' instead"
run "$CMD"
;;

restart|reload)
sig USR2 && echo reloaded OK && exit 0
echo >&2 "Couldn't reload, starting '$CMD' instead"
run "$CMD"
;;

希望这有助于其他人!

看看对我有用的独角兽配置,在before_fork部分可以尝试添加以下代码

old_pid = '#{root}/tmp/pids/unicorn.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

此外,您无需检查配置中是否定义了ActiveRecord :: Base。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM