LoginSignup
1

More than 5 years have passed since last update.

Redis with rails. You need to reconnect to Redis after forking error.の解決方法

Last updated at Posted at 2018-03-09

2年間運用しているRailsアプリケーションにRspec高速化のためにspring-commands-rspecgemを追加したところ発生。

  • エラーメッセージ
Redis::InheritedError:
       Tried to use a connection from a child process without reconnecting. You need to reconnect to Redis after forking or set :inherit_socket to true.

エラーメッセージを見ると、Springがフォークしてできたプロセスが、Redisとのコネクションが取れていないということらしい。Springのドキュメントを見てみると、フォークした後タイミングのコールバックが用意されていたので利用。

config/spring.rb
Spring.after_fork do
  begin
    Redis.current = Redis.new(hogehoge)
    Redis.current.ping # 接続チェック
  rescue => err
    Rails.logger.info(err)
  end
end

上記エラーが発生した環境はそれぞれだと思いますが、参考になれば幸いです。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1