在Rails中拯救Redis异常

时间:2013-05-24 15:50:49

标签: ruby-on-rails-3 redis master-slave sidekiq redis-cache

我目前有一个Rails 3.2.13应用使用redis作为cache_storeSidekiq

我也在使用redis master-slave。我遇到了一个问题,在故障转移期间,一个从服务器正在成为主服务器,然后Rails连接到该节点,一旦它尝试写入节点,即使节点是master,也会引发此异常:

Redis::CommandError: READONLY You can't write against a read only slave.
vendor/bundle/ruby/1.9.1/gems/redis-3.0.4/lib/redis/client.rb:85:in `call'
vendor/bundle/ruby/1.9.1/gems/redis-3.0.4/lib/redis.rb:1181:in `block in srem'
vendor/bundle/ruby/1.9.1/gems/redis-3.0.4/lib/redis.rb:36:in `block in synchronize'
/usr/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
vendor/bundle/ruby/1.9.1/gems/redis-3.0.4/lib/redis.rb:36:in `synchronize'
vendor/bundle/ruby/1.9.1/gems/redis-3.0.4/lib/redis.rb:1180:in `srem'
vendor/bundle/ruby/1.9.1/gems/redis-namespace-1.3.0/lib/redis/namespace.rb:306:in `method_missing'
vendor/bundle/ruby/1.9.1/gems/sidekiq-2.11.2/lib/sidekiq/manager.rb:123:in `block in clear_worker_set'
vendor/bundle/ruby/1.9.1/gems/connection_pool-1.0.0/lib/connection_pool.rb:48:in `with'
vendor/bundle/ruby/1.9.1/gems/sidekiq-2.11.2/lib/sidekiq.rb:67:in `redis'
vendor/bundle/ruby/1.9.1/gems/sidekiq-2.11.2/lib/sidekiq/manager.rb:118:in `clear_worker_set'
vendor/bundle/ruby/1.9.1/gems/sidekiq-2.11.2/lib/sidekiq/manager.rb:45:in `block in stop'
vendor/bundle/ruby/1.9.1/gems/sidekiq-2.11.2/lib/sidekiq/util.rb:15:in `watchdog'
vendor/bundle/ruby/1.9.1/gems/sidekiq-2.11.2/lib/sidekiq/manager.rb:33:in `stop'
vendor/bundle/ruby/1.9.1/gems/celluloid-0.14.0/lib/celluloid/calls.rb:25:in `public_send'
vendor/bundle/ruby/1.9.1/gems/celluloid-0.14.0/lib/celluloid/calls.rb:25:in `dispatch'
vendor/bundle/ruby/1.9.1/gems/celluloid-0.14.0/lib/celluloid/calls.rb:125:in `dispatch'
vendor/bundle/ruby/1.9.1/gems/celluloid-0.14.0/lib/celluloid/actor.rb:328:in `block in handle_message'
vendor/bundle/ruby/1.9.1/gems/celluloid-0.14.0/lib/celluloid/tasks.rb:42:in `block in initialize'
vendor/bundle/ruby/1.9.1/gems/celluloid-0.14.0/lib/celluloid/tasks/task_fiber.rb:9:in `block in create'

我知道可以通过断开使用redis的任何内容(例如sidekiq,rails cache_store)来轻松解决问题,以便写入该节点的任何后续命令都将重新连接到有效连接。

问题是捕获和处理异常的位置?我最初的想法是进行airbrake全球救援,但不确定这是否是最佳方式。

有没有人遇到过这个问题?

0 个答案:

没有答案