Quantcast
Channel: Active questions tagged redis+ruby-on-rails - Stack Overflow
Viewing all articles
Browse latest Browse all 873

Reconnect with Redis after Puma fork

$
0
0

I am using a global variable in a rails application to store a redis client using the redis gem. In a config/initializers/redis.rb, I have

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

Then in application code, I use $redis to work the data in the Redis store.

I also use puma as the web server in production environment, and capistrano to deploy code. In the deploy process, capistrano restarts puma.

Every time I start or restart the puma web servers, I always get an "Internal Server Error" when I first use $redis to access data in the Redis store. I saw errors like Redis::InheritedError (Tried to use a connection from a child process without reconnecting. You need to reconnect to Redis after forking.)

Searching around with google and stackoverflow led me to think that I needed to reconnect to Redis after puma forks child processes. So, I added in my config/puma.rb:

on_worker_boot do  $redis.pingend

But I was still getting the "Internal Server Error" caused by Redis::InheritedError (Tried to use a connection from a child process without reconnecting. You need to reconnect to Redis after forking.).

I saw this post http://qiita.com/yaotti/items/18433802bf1720fc0c53. I then tried adding in config/puma.rb:

on_restart do  $redis.quitend

That did not work.

I tried in config/initializers/redis.rb to $redis.ping right after Redis.new. That did not work either.

I got this error if puma was started with no puma processes running, or restarted when an instance of puma process was running.

Refreshing the page would get me past this error. But I want to get rid of this even on the first attempt to use $redis. I was thinking that I did not use the redis gem or configure its reconnection correctly. Could someone tell me:

  1. Is that the right way to use redis gem in a rails application?
  2. How should the redis connection be reconnected in puma?

puma gem documentation says, "You should place code to close global log files, redis connections, etc in this block so that their file descriptors don't leak into the restarted process. Failure to do so will result in slowly running out of descriptors and eventually obscure crashes as the server is restart many times." It was talking about the on_restart block. But it did not say how that should be done.


Viewing all articles
Browse latest Browse all 873

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>