gpt4 book ai didi

ruby-on-rails - 使用 Dokku + nginx + unicorn 的未初始化常量 ActiveRecord (NameError)

转载 作者:行者123 更新时间:2023-11-29 13:57:00 25 4
gpt4 key购买 nike

我使用 Dokku 在 digitalocean 上运行 Rails 应用程序+ nginx + PostgreSQL。部署的应用程序正在使用 WEBrick 作为默认服务器运行,我想迁移到 unicorn。

gem 文件

group :production do
gem 'pg'
gem 'unicorn'
end

我用过 Heroku unicorn 的配置

程序文件

web: bundle exec unicorn -p $PORT -c ./config/unicorn.rb

config/unicorn.rb

worker_processes Integer(ENV["WEB_CONCURRENCY"] || 3)
timeout 15
preload_app true

before_fork do |server, worker|
Signal.trap 'TERM' do
puts 'Unicorn master intercepting TERM and sending myself QUIT instead'
Process.kill 'QUIT', Process.pid
end

defined?(ActiveRecord::Base) and
ActiveRecord::Base.connection.disconnect!
end

after_fork do |server, worker|
Signal.trap 'TERM' do
puts 'Unicorn worker intercepting TERM and doing nothing. Wait for master to send QUIT'
end

defined?(ActiveRecord::Base) and
ActiveRecord::Base.establish_connection
end

日志是这样的

root@oktobTest:~# dokku logs oktob
I, [2015-04-25T18:30:29.195191 #13] INFO -- : Refreshing Gem list
config.ru:4:in `block in <main>': uninitialized constant ActiveRecord (NameError)
from /app/vendor/bundle/ruby/2.1.0/gems/rack-1.5.2/lib/rack/builder.rb:55:in `instance_eval'
from /app/vendor/bundle/ruby/2.1.0/gems/rack-1.5.2/lib/rack/builder.rb:55:in `initialize'
from config.ru:1:in `new'
from config.ru:1:in `<main>'
from /app/vendor/bundle/ruby/2.1.0/gems/unicorn-4.8.3/lib/unicorn.rb:48:in `eval'
from /app/vendor/bundle/ruby/2.1.0/gems/unicorn-4.8.3/lib/unicorn.rb:48:in `block in builder'
from /app/vendor/bundle/ruby/2.1.0/gems/unicorn-4.8.3/lib/unicorn/http_server.rb:764:in `call'
from /app/vendor/bundle/ruby/2.1.0/gems/unicorn-4.8.3/lib/unicorn/http_server.rb:764:in `build_app!'
from /app/vendor/bundle/ruby/2.1.0/gems/unicorn-4.8.3/lib/unicorn/http_server.rb:137:in `start'
from /app/vendor/bundle/ruby/2.1.0/gems/unicorn-4.8.3/bin/unicorn:126:in `<top (required)>'
from /app/vendor/bundle/ruby/2.1.0/bin/unicorn:23:in `load'
from /app/vendor/bundle/ruby/2.1.0/bin/unicorn:23:in `<main>'

当我运行 docker ps 命令时,我只看到 PostgreSQL 进程,没有 Rails 服务器的进程。

root@oktobTest:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fbdbb516b19c postgresql/oktob:latest "/usr/bin/start_pgsq 2 hours ago Up 2 hours 0.0.0.0:49153->5432/tcp elegant_ardinghelli

最佳答案

我在 dokku 上问了这个问题Github页面并得到了答案

因此将 active_record gem 添加到 config/unicorn.rb 将解决问题:

require 'rubygems'
require 'active_record'

worker_processes Integer(ENV["WEB_CONCURRENCY"] || 3)
timeout 15
preload_app true

before_fork do |server, worker|
Signal.trap 'TERM' do
puts 'Unicorn master intercepting TERM and sending myself QUIT instead'
Process.kill 'QUIT', Process.pid
end

defined?(ActiveRecord::Base) and
ActiveRecord::Base.connection.disconnect!
end

after_fork do |server, worker|
Signal.trap 'TERM' do
puts 'Unicorn worker intercepting TERM and doing nothing. Wait for master to send QUIT'
end

defined?(ActiveRecord::Base) and
ActiveRecord::Base.establish_connection
end

这是结果

使用 docker ps


容器 ID 图像命令创建状态端口名称
4a43ac670998 dokku/oktob:latest "/start web"12 分钟前 上升 12 分钟 grave_yonath
fbdbb516b19c postgresql/oktob:latest "/usr/bin/start_pgsq 6 天前 Up 6 天 0.0.0.0:49153->5432/tcp elegant_ardinghelli

使用 htop

enter image description here

关于ruby-on-rails - 使用 Dokku + nginx + unicorn 的未初始化常量 ActiveRecord (NameError),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29869186/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com