[英]SSH and -bash: fork: Cannot allocate memory Ubuntu , rails , Passenger, redis , sidekiq
[英]Restarting Rails Redis Cache Store connection upon Passenger fork
我想使用redis緩存存儲(使用redis-store gem)。
它在本地工作正常,但是當進行生產時Passenger分叉Rails工作者的多個實例時,我們得到Redis錯誤,這表明有關Redis訪問的不同實例之間存在同步問題。
這種錯誤的一個例子是
Got '7' as initial reply byte. If you're running in a multi-threaded environment, make sure you pass the :thread_safe option when initializing the connection. If you're in a forking environment, such as Unicorn, you need to connect to Redis after forking.
redis (2.2.2) lib/redis/connection/ruby.rb:78:in `format_reply'
我做了一些閱讀並了解到每個Passenger worker實例必須創建自己的Redis連接。 這可以使用以下代碼實現
#config/initializers/redis_fork_init.rb
if defined?(PhusionPassenger)
PhusionPassenger.on_event(:starting_worker_process) do |forked|
if forked
$redis = Redis.new
end
end
end
假設Redis訪問是通過遍布代碼的$ redis完成的 - 這個解決方案很棒。
我的問題是 - 如何創建一個新的Redis連接,當我執行Rails.cache
操作(如讀,寫等)時將使用它?
我的config / environments / production.rb包括以下內容:
config.cache_store = :redis_store, { :host => 'localhost', :port => 6379, :thread_safe => true }
使用Rails 3.2.3,Redis 2.2.2,redis-store 1.1.1,Passenger 3.0
看看redis商店的重新連接方法: https : //github.com/jodosha/redis-store/blob/master/redis-activesupport/lib/active_support/cache/redis_store.rb
所以基本上你的fork塊應該是這樣的:
if defined?(PhusionPassenger)
PhusionPassenger.on_event(:starting_worker_process) do |forked|
if forked
Rails.cache.reconnect
end
end
end
添加到$redis = Redis.new
的響應,將$redis = Redis.new
更改為Rails.cache.reconnect
。
在這個github問題中找到的信息, https://github.com/jodosha/redis-store/issues/21#issuecomment-948569
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.