gpt4 book ai didi

ruby - 连接池返回 Redis 的实例而不是 ConnectionPool

转载 作者:IT王子 更新时间:2023-10-29 06:12:08 24 4
gpt4 key购买 nike

我正在尝试让我的 Rails 应用程序使用 Resque 来管理工作人员。但是,我想继续使用 ConnectionPool gem。

我在初始化程序中有这个:

puts ENV["REDISTOGO_URL"]

uri = (not ENV["REDISTOGO_URL"].nil?) ? URI.parse(ENV["REDISTOGO_URL"]) : nil

# at this point, debugger confirms $redis is nil
$redis = ConnectionPool::Wrapper.new(:size => 5, :timeout => 3) {
if uri.nil?
Redis.connect
else
Redis.connect(:host => uri.host, :port => uri.port, :password => uri.password)
end
}

$redis # just put this in here for the debugger
# At this point, $redis is #<Redis:0x007fb1b0036bf0>
# when it should be an instance of ConnectionPool::Wrapper

有没有人知道为什么 $redis 不会作为实例 ConnectionPool::Wrapper 返回?

我查遍了所有的gems源码,都没有设置$redis的值。在 ConnectionPool 的源代码中,我没有发现任何它会返回 Redis 实例而不是自身的地方。

只有当我从 DelayedJob 切换到 Resque 时才会发生这种情况。所以,这似乎是问题所在。但是,我不知所措。

我正在使用 unicorn 。这是 config 中的那个文件。

worker_processes 2
timeout 30
preload_app true

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

# If you are using Redis but not Resque, change this
if defined?(Resque)
Resque.redis.quit
Rails.logger.info('Disconnected from Redis')
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

# If you are using Redis but not Resque, change this
if defined?(Resque)
# Yes, commented the Resque out for debugging, still get the same problem.
#Resque.redis = ENV['REDISTOGO_URL']
Rails.logger.info('Connected to Redis')
end
end

最后,Procfile:

web: bundle exec unicorn -p $PORT -c ./config/unicorn.rb
worker: env TERM_CHILD=1 QUEUE=* bundle exec rake resque:work

我在我的开发环境中使用 foreman

非常感谢任何帮助。

最佳答案

来自文档:

You can use ConnectionPool::Wrapper to wrap a single global connection.

我看起来像 ConnectionPool::Wrapper 是为了包装到 Redis 的单个连接,以便于将大型应用程序从直接使用 Redis 迁移到使用 ConnectionPools

如果你调用$redis.with,你会得到ConnectionPool定义的#with

要获得实际的连接池,只需更改您的

ConnectionPool::Wrapper.new(:size => 5, :timeout => 3) { #redis logic }

ConnectionPool.new(:size => 5, :timeout => 3) { #redis logic }

关于ruby - 连接池返回 Redis 的实例而不是 ConnectionPool,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14915351/

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