我在rails应用程序中使用全局变量来使用redis gem存储redis客户端.在一个config/initializers/redis.rb,我有
$redis = Redis.new(host: "localhost", port: 6379)
Run Code Online (Sandbox Code Playgroud)
然后在应用程序代码中,我$redis用来处理Redis存储中的数据.
我还使用puma作为生产环境中的Web服务器,并使用capistrano来部署代码.在部署过程中,capistrano重启puma.
每次我启动或重新启动美洲狮网络服务器时,我第一次使用$redisRedis商店中的数据时总是会出现"内部服务器错误" .我看到了错误Redis::InheritedError (Tried to use a connection from a child process without reconnecting. You need to reconnect to Redis after forking.)
搜索谷歌和stackoverflow让我认为我需要重新连接到美洲狮分叉子进程后重新连接到Redis.所以,我补充说config/puma.rb:
on_worker_boot do
$redis.ping
end
Run Code Online (Sandbox Code Playgroud)
但我仍然得到了"内部服务器错误" Redis::InheritedError (Tried to use a connection from a child process without reconnecting. You need to reconnect to Redis after forking.).
我看过这篇文章http://qiita.com/yaotti/items/18433802bf1720fc0c53.然后我尝试添加config/puma.rb:
on_restart do
$redis.quit
end …Run Code Online (Sandbox Code Playgroud)