gpt4 book ai didi

ruby-on-rails - Rails 应用程序 : Reconnect juggernaut redis after forking?

转载 作者:可可西里 更新时间:2023-11-01 11:45:25 25 4
gpt4 key购买 nike

我正在开发一个使用 Juggernaut 的 Rails 应用程序定期向客户端推送数据。我使用 Controller Action 来开始推送;但由于推送通常是一个漫长的过程(10 分钟或更长时间),我正在使用 spawn 来 fork 任务。例如:

def start_pushing
spawn_block(:argv => "juggernaut-pushing") do
for x in y
Juggernaut.publish(stuff in here)
sleep(30) # delay before publishing next item
end
end
end

问题是当我点击 start_pushing 操作时,我在日志文件中发现了这个错误:

spawn> Exception in child[27898] - Redis::InheritedError: Tried to use a connection from a child process without reconnecting. You need to reconnect to Redis after forking.

所以我在 spawn_block 中添加了以下内容,希望它能解决问题:

    require 'redis'
$redis.client.disconnect
$redis = Redis.new(:host => 'localhost', :port => 6379)

它似乎没有修复它,尽管在我添加它以重置 $redis 之前,该操作一直间歇性地工作。我在想,也许重置 $redis 没有做任何事情; Juggernaut 仍在访问旧连接。这看起来有可能吗?我如何确保 Juggernaut 使用新的 Redis 连接?

如果对我所描述的内容有任何疑问,请告诉我。感谢您的帮助,因为我现在被困住了。

最佳答案

问题在于使用全局变量(以 $ 开头的变量)并使用 Passenger。所有全局变量在所有乘客进程中共享。 Redis 有某种检查以确保 redis 调用的进程 ID 与连接的进程 ID 相同。因此,如果 redis 是使用 passenger worker 1 连接的,但随后 worker 2 使用它,则会出现此错误。

有关详细信息,请参阅 Redis::Client 类的方法 connect 和 ensure_connected。

this issue 中解释了解决方案.基本上,他们建议您喜欢 memcache 作为 explained here .

简而言之,在您的 config/environement.rb 中,添加以下内容:

if defined?(PhusionPassenger)
PhusionPassenger.on_event(:starting_worker_process) do |forked|
if forked
# We're in smart spawning mode.
$redis.reconnect
else
# We're in conservative spawning mode. We don't need to do anything.
end
end
end

关于ruby-on-rails - Rails 应用程序 : Reconnect juggernaut redis after forking?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11262067/

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