Skip to content

Commit 30c62bb

Browse files
author
Shaun Carlson
committed
handle redis cache upstream failure by falling back to RPC call
1 parent 3f74bcd commit 30c62bb

4 files changed

Lines changed: 73 additions & 0 deletions

File tree

lib/active_remote/cached.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,9 @@ def self.#{method_name}(#{expanded_method_args}, __active_remote_cached_options
190190
].compact
191191
192192
::ActiveRemote::Cached.cache.exist?(cache_key)
193+
rescue => e
194+
return false if e.message.include?("upstream failure")
195+
raise
193196
end
194197
RUBY
195198

@@ -217,6 +220,9 @@ def self.#{method_name}(#{expanded_method_args}, __active_remote_cached_options
217220
].compact
218221
219222
::ActiveRemote::Cached.cache.exist?(cache_key)
223+
rescue => e
224+
return false if e.message.include?("upstream failure")
225+
raise
220226
end
221227
RUBY
222228

@@ -263,6 +269,9 @@ def self.#{method_name}(#{expanded_method_args}, __active_remote_cached_options
263269
self.find(#{expanded_search_args})
264270
end
265271
end
272+
rescue => e
273+
return self.find(#{expanded_search_args}) if e.message.include?("upstream failure")
274+
raise
266275
end
267276
RUBY
268277
end
@@ -311,6 +320,9 @@ def self.#{method_name}(#{expanded_method_args}, __active_remote_cached_options
311320
self.search(#{expanded_search_args})
312321
end
313322
end
323+
rescue => e
324+
return self.search(#{expanded_search_args}) if e.message.include?("upstream failure")
325+
raise
314326
end
315327
RUBY
316328
end
@@ -369,6 +381,13 @@ def self.#{method_name}(#{expanded_method_args}, __active_remote_cached_options
369381
raise ::ActiveRemote::RemoteRecordNotFound.new(self.class) if results.first.nil?
370382
results
371383
end
384+
rescue => e
385+
if e.message.include?("upstream failure")
386+
results = self.search(#{expanded_search_args})
387+
raise ::ActiveRemote::RemoteRecordNotFound.new(self.class) if results.first.nil?
388+
return results
389+
end
390+
raise
372391
end
373392
RUBY
374393
end

spec/active_remote/cached_exist_methods_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,5 +126,19 @@ def self.search; nil; end
126126
it "creates 'cached_exist_search_by_client_guid_and_user_guid_and_derp?'" do
127127
_(ExistMethodClass).must_respond_to("cached_exist_search_by_client_guid_and_user_guid_and_derp?")
128128
end
129+
130+
describe "when cache raises upstream failure redis error" do
131+
it "returns false" do
132+
::ActiveRemote::Cached.cache.expects(:exist?).raises(::RuntimeError, "upstream failure")
133+
_(ExistMethodClass.cached_exist_search_by_guid?(:guid)).must_equal(false)
134+
end
135+
end
136+
137+
describe "when cache raises any other kind of error" do
138+
it "allows error to pass through" do
139+
::ActiveRemote::Cached.cache.expects(:exist?).raises(::RuntimeError, "kaBOOM")
140+
assert_raises(::RuntimeError, "kaBOOM") { ExistMethodClass.cached_exist_search_by_guid?(:guid) }
141+
end
142+
end
129143
end
130144
end

spec/active_remote/cached_find_methods_spec.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,26 @@ def self.search; nil; end
9999
end
100100
end
101101
end
102+
103+
describe "when cache raises upstream failure redis error" do
104+
it "falls back to calling find" do
105+
::ActiveRemote::Cached.cache.expects(:fetch).raises(::RuntimeError, "upstream failure")
106+
107+
FindMethodClass.stub(:find, "foo") do
108+
_(FindMethodClass.cached_find_by_guid(:guid)).must_equal("foo")
109+
end
110+
end
111+
end
112+
113+
describe "when cache raises any other kind of error" do
114+
it "allows error to pass through" do
115+
::ActiveRemote::Cached.cache.expects(:fetch).raises(::RuntimeError, "kaBOOM")
116+
117+
FindMethodClass.stub(:find, "foo") do
118+
assert_raises(::RuntimeError, "kaBOOM") { FindMethodClass.cached_find_by_guid(:guid) }
119+
end
120+
end
121+
end
102122
end
103123

104124
describe "#cached_find_by_foo" do

spec/active_remote/cached_search_methods_spec.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,26 @@ def self.search; nil; end
166166
end
167167
end
168168
end
169+
170+
describe "when cache raises upstream failure redis error" do
171+
it "falls back to calling search" do
172+
::ActiveRemote::Cached.cache.expects(:fetch).raises(::RuntimeError, "upstream failure")
173+
174+
SearchMethodClass.stub(:search, "foo") do
175+
_(SearchMethodClass.cached_search_by_guid(:guid)).must_equal("foo")
176+
end
177+
end
178+
end
179+
180+
describe "when cache raises any other kind of error" do
181+
it "allows error to pass through" do
182+
::ActiveRemote::Cached.cache.expects(:fetch).raises(::RuntimeError, "kaBOOM")
183+
184+
SearchMethodClass.stub(:search, "foo") do
185+
assert_raises(::RuntimeError, "kaBOOM") { SearchMethodClass.cached_search_by_guid(:guid) }
186+
end
187+
end
188+
end
169189
end
170190

171191
describe "#cached_search_by_foo" do

0 commit comments

Comments
 (0)