Skip to content

Commit 0b107e5

Browse files
committed
Properly retry on timeouts in all strategies
1 parent 977182c commit 0b107e5

6 files changed

Lines changed: 45 additions & 10 deletions

File tree

lib/userlist/push/strategies/active_job/worker.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class Push
55
module Strategies
66
class ActiveJob
77
class Worker < ::ActiveJob::Base
8-
retry_on Timeout::Error, wait: :polynomially_longer, attempts: 10
8+
retry_on Userlist::TimeoutError, Userlist::RequestError, wait: :polynomially_longer, attempts: 10
99

1010
def perform(method, *args)
1111
client = Userlist::Push::Client.new

lib/userlist/push/strategies/direct.rb

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,15 @@ def client
2020

2121
def retryable
2222
@retryable ||= Userlist::Retryable.new do |error|
23-
next false unless error.is_a?(Userlist::RequestError)
24-
25-
status = error.status
26-
status >= 500 || status == 429
23+
case error
24+
when Userlist::RequestError
25+
status = error.status
26+
status >= 500 || status == 429
27+
when Userlist::TimeoutError
28+
true
29+
else
30+
false
31+
end
2732
end
2833
end
2934
end

lib/userlist/push/strategies/threaded/worker.rb

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,15 @@ def client
4747

4848
def retryable
4949
@retryable ||= Userlist::Retryable.new do |error|
50-
next false unless error.is_a?(Userlist::RequestError)
51-
52-
status = error.status
53-
status >= 500 || status == 429
50+
case error
51+
when Userlist::RequestError
52+
status = error.status
53+
status >= 500 || status == 429
54+
when Userlist::TimeoutError
55+
true
56+
else
57+
false
58+
end
5459
end
5560
end
5661
end

spec/userlist/push/strategies/active_job/worker_spec.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,14 @@
3030
end
3131

3232
it 'should retry the job on failure' do
33-
allow(client).to receive(:post).and_raise(Timeout::Error)
33+
allow(client).to receive(:post).and_raise(Userlist::RequestError.new(double(code: '500', body: 'Internal Error')))
34+
35+
expect { described_class.perform_now('post', '/events', payload) }
36+
.to change(described_class.queue_adapter.enqueued_jobs, :size).by(1)
37+
end
38+
39+
it 'should retry the job on timeouts' do
40+
allow(client).to receive(:post).and_raise(Userlist::TimeoutError)
3441

3542
expect { described_class.perform_now('post', '/events', payload) }
3643
.to change(described_class.queue_adapter.enqueued_jobs, :size).by(1)

spec/userlist/push/strategies/direct_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,14 @@
3737

3838
subject.call(:post, '/users', payload)
3939
end
40+
41+
it 'should retry on timeouts' do
42+
payload = { foo: :bar }
43+
44+
expect(client).to receive(:post) { raise Userlist::TimeoutError }.exactly(11).times
45+
expect_any_instance_of(Userlist::Retryable).to receive(:sleep).exactly(10).times
46+
47+
subject.call(:post, '/users', payload)
48+
end
4049
end
4150
end

spec/userlist/push/strategies/threaded/worker_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,15 @@
5151
queue.push([:post, '/users', payload])
5252
end
5353

54+
it 'should retry on timeouts' do
55+
payload = { foo: :bar }
56+
57+
expect(client).to receive(:post) { raise Userlist::TimeoutError }.exactly(11).times
58+
expect_any_instance_of(Userlist::Retryable).to receive(:sleep).exactly(10).times
59+
60+
queue.push([:post, '/users', payload])
61+
end
62+
5463
it 'should log failed requests' do
5564
allow(client).to receive(:post).and_raise(StandardError)
5665
queue.push([:post, '/events', payload])

0 commit comments

Comments
 (0)