繁体   English   中英

Redis与铁轨。 在分叉错误后,您需要重新连接到Redis

[英]Redis with rails. You need to reconnect to Redis after forking error

我正在使用Redis和我的Rails应用程序。 我也安装了sidekiq gem。 我的redis服务器在默认端口的同一台机器上运行。

我创建了一个初始化程序,可以使redis lient初始化。

配置/初始化/ redis.rb

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

我有另一个初始化程序,用于设置系统中当前活动的帐户数。

配置/初始化/ z_account_list.rb

$redis.set('accounts',Account.count);

在我的一个观点中,我正在使用这段代码。

<div class="more-text">and <%= "#{$redis.get('accounts')}" %> more...</div>

当我在redis中手动设置帐户的值而不使用初始化程序时,一切正常。 但是当我添加初始化程序时,我得到了

ActionView::Template::Error (Tried to use a connection from a child process without reconnecting. You need to reconnect to Redis after forking.):

我搜索了错误。 但大多数解决方案都用于resque,并与after_fork有关。 因为我是Rails和Redis的新手,因为我不使用Resque,所以我有点困惑。 请帮我解决这个问题。

在像pushion passenger这样的分叉环境中,每当工人分叉时我们都必须重新连接到redis。 我最大的困惑是放置重新连接语句的位置。 在许多博客中,建议将其放在config / environments.rb中。 但它对我不起作用。

我补充道

if defined?(PhusionPassenger)
  PhusionPassenger.on_event(:starting_worker_process) do |forked|
    if forked
      $redis.client.disconnect
      $redis = Redis.new(:host => 'localhost', :port => 6379)
      Rails.logger.info "Reconnecting to redis"
    else
      # We're in conservative spawning mode. We don't need to do anything.
    end
  end
end

config / initializers / redis.rb,一切都开始正常了。

暂无
暂无

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

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