shoryuken 3.0.9 → 3.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.travis.yml +1 -0
- data/CHANGELOG.md +24 -0
- data/Gemfile +1 -0
- data/README.md +15 -110
- data/bin/cli/sqs.rb +15 -1
- data/lib/shoryuken/default_worker_registry.rb +2 -2
- data/lib/shoryuken/environment_loader.rb +31 -11
- data/lib/shoryuken/fetcher.rb +17 -16
- data/lib/shoryuken/launcher.rb +86 -7
- data/lib/shoryuken/manager.rb +38 -79
- data/lib/shoryuken/options.rb +192 -0
- data/lib/shoryuken/polling/base.rb +67 -0
- data/lib/shoryuken/polling/strict_priority.rb +77 -0
- data/lib/shoryuken/polling/weighted_round_robin.rb +66 -0
- data/lib/shoryuken/processor.rb +21 -18
- data/lib/shoryuken/queue.rb +12 -11
- data/lib/shoryuken/runner.rb +3 -15
- data/lib/shoryuken/version.rb +1 -1
- data/lib/shoryuken.rb +39 -173
- data/shoryuken.gemspec +1 -1
- data/spec/integration/launcher_spec.rb +12 -6
- data/spec/shoryuken/environment_loader_spec.rb +3 -12
- data/spec/shoryuken/fetcher_spec.rb +30 -15
- data/spec/shoryuken/manager_spec.rb +9 -17
- data/spec/shoryuken/options_spec.rb +100 -0
- data/spec/shoryuken/{polling_spec.rb → polling/strict_priority_spec.rb} +1 -100
- data/spec/shoryuken/polling/weighted_round_robin_spec.rb +99 -0
- data/spec/shoryuken/processor_spec.rb +20 -39
- data/spec/shoryuken/queue_spec.rb +8 -0
- data/spec/shoryuken/runner_spec.rb +3 -4
- data/spec/shoryuken_spec.rb +0 -59
- data/spec/spec_helper.rb +7 -2
- metadata +14 -7
- data/lib/shoryuken/polling.rb +0 -204
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
RSpec.describe Shoryuken::Polling::WeightedRoundRobin do
|
|
4
|
+
let(:queue1) { 'shoryuken' }
|
|
5
|
+
let(:queue2) { 'uppercut' }
|
|
6
|
+
let(:queues) { Array.new }
|
|
7
|
+
subject { Shoryuken::Polling::WeightedRoundRobin.new(queues) }
|
|
8
|
+
|
|
9
|
+
describe '#next_queue' do
|
|
10
|
+
it 'cycles' do
|
|
11
|
+
# [shoryuken, 2]
|
|
12
|
+
# [uppercut, 1]
|
|
13
|
+
queues << queue1
|
|
14
|
+
queues << queue1
|
|
15
|
+
queues << queue2
|
|
16
|
+
|
|
17
|
+
expect(subject.next_queue).to eq(queue1)
|
|
18
|
+
expect(subject.next_queue).to eq(queue2)
|
|
19
|
+
expect(subject.next_queue).to eq(queue1)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
it 'returns nil if there are no active queues' do
|
|
23
|
+
expect(subject.next_queue).to eq(nil)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
it 'unpauses queues whose pause is expired' do
|
|
27
|
+
# [shoryuken, 2]
|
|
28
|
+
# [uppercut, 1]
|
|
29
|
+
queues << queue1
|
|
30
|
+
queues << queue1
|
|
31
|
+
queues << queue2
|
|
32
|
+
|
|
33
|
+
allow(subject).to receive(:delay).and_return(10)
|
|
34
|
+
|
|
35
|
+
now = Time.now
|
|
36
|
+
allow(Time).to receive(:now).and_return(now)
|
|
37
|
+
|
|
38
|
+
# pause the first queue
|
|
39
|
+
subject.messages_found(queue1, 0)
|
|
40
|
+
expect(subject.next_queue).to eq(queue2)
|
|
41
|
+
|
|
42
|
+
now += 5
|
|
43
|
+
allow(Time).to receive(:now).and_return(now)
|
|
44
|
+
|
|
45
|
+
# pause the second queue
|
|
46
|
+
subject.messages_found(queue2, 0)
|
|
47
|
+
expect(subject.next_queue).to eq(nil)
|
|
48
|
+
|
|
49
|
+
# queue1 should be unpaused now
|
|
50
|
+
now += 6
|
|
51
|
+
allow(Time).to receive(:now).and_return(now)
|
|
52
|
+
expect(subject.next_queue).to eq(queue1)
|
|
53
|
+
|
|
54
|
+
# queue1 should be unpaused and added to the end of queues now
|
|
55
|
+
now += 6
|
|
56
|
+
allow(Time).to receive(:now).and_return(now)
|
|
57
|
+
expect(subject.next_queue).to eq(queue1)
|
|
58
|
+
expect(subject.next_queue).to eq(queue2)
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
describe '#messages_found' do
|
|
63
|
+
it 'pauses a queue if there are no messages found' do
|
|
64
|
+
# [shoryuken, 2]
|
|
65
|
+
# [uppercut, 1]
|
|
66
|
+
queues << queue1
|
|
67
|
+
queues << queue1
|
|
68
|
+
queues << queue2
|
|
69
|
+
|
|
70
|
+
expect(subject).to receive(:pause).with(queue1).and_call_original
|
|
71
|
+
subject.messages_found(queue1, 0)
|
|
72
|
+
expect(subject.instance_variable_get(:@queues)).to eq([queue2])
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
it 'increased the weight if message is found' do
|
|
76
|
+
# [shoryuken, 2]
|
|
77
|
+
# [uppercut, 1]
|
|
78
|
+
queues << queue1
|
|
79
|
+
queues << queue1
|
|
80
|
+
queues << queue2
|
|
81
|
+
|
|
82
|
+
expect(subject.instance_variable_get(:@queues)).to eq([queue1, queue2])
|
|
83
|
+
subject.messages_found(queue1, 1)
|
|
84
|
+
expect(subject.instance_variable_get(:@queues)).to eq([queue1, queue2, queue1])
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
it 'respects the maximum queue weight' do
|
|
88
|
+
# [shoryuken, 2]
|
|
89
|
+
# [uppercut, 1]
|
|
90
|
+
queues << queue1
|
|
91
|
+
queues << queue1
|
|
92
|
+
queues << queue2
|
|
93
|
+
|
|
94
|
+
subject.messages_found(queue1, 1)
|
|
95
|
+
subject.messages_found(queue1, 1)
|
|
96
|
+
expect(subject.instance_variable_get(:@queues)).to eq([queue1, queue2, queue1])
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
end
|
|
@@ -3,7 +3,7 @@ require 'shoryuken/processor'
|
|
|
3
3
|
require 'shoryuken/manager'
|
|
4
4
|
|
|
5
5
|
RSpec.describe Shoryuken::Processor do
|
|
6
|
-
let(:manager) { double Shoryuken::Manager
|
|
6
|
+
let(:manager) { double Shoryuken::Manager }
|
|
7
7
|
let(:sqs_queue) { double Shoryuken::Queue, visibility_timeout: 30 }
|
|
8
8
|
let(:queue) { 'default' }
|
|
9
9
|
|
|
@@ -16,14 +16,14 @@ RSpec.describe Shoryuken::Processor do
|
|
|
16
16
|
receipt_handle: SecureRandom.uuid
|
|
17
17
|
end
|
|
18
18
|
|
|
19
|
-
subject { described_class.new(manager) }
|
|
20
|
-
|
|
21
19
|
before do
|
|
22
20
|
allow(manager).to receive(:async).and_return(manager)
|
|
23
21
|
allow(manager).to receive(:real_thread)
|
|
24
22
|
allow(Shoryuken::Client).to receive(:queues).with(queue).and_return(sqs_queue)
|
|
25
23
|
end
|
|
26
24
|
|
|
25
|
+
subject { described_class.new(queue, sqs_msg) }
|
|
26
|
+
|
|
27
27
|
describe '#process' do
|
|
28
28
|
it 'parses the body into JSON' do
|
|
29
29
|
TestWorker.get_shoryuken_options['body_parser'] = :json
|
|
@@ -34,7 +34,7 @@ RSpec.describe Shoryuken::Processor do
|
|
|
34
34
|
|
|
35
35
|
allow(sqs_msg).to receive(:body).and_return(JSON.dump(body))
|
|
36
36
|
|
|
37
|
-
subject.process
|
|
37
|
+
subject.process
|
|
38
38
|
end
|
|
39
39
|
|
|
40
40
|
it 'parses the body calling the proc' do
|
|
@@ -44,7 +44,7 @@ RSpec.describe Shoryuken::Processor do
|
|
|
44
44
|
|
|
45
45
|
allow(sqs_msg).to receive(:body).and_return('test')
|
|
46
46
|
|
|
47
|
-
subject.process
|
|
47
|
+
subject.process
|
|
48
48
|
end
|
|
49
49
|
|
|
50
50
|
it 'parses the body as text' do
|
|
@@ -56,7 +56,7 @@ RSpec.describe Shoryuken::Processor do
|
|
|
56
56
|
|
|
57
57
|
allow(sqs_msg).to receive(:body).and_return(body)
|
|
58
58
|
|
|
59
|
-
subject.process
|
|
59
|
+
subject.process
|
|
60
60
|
end
|
|
61
61
|
|
|
62
62
|
it 'parses calling `.load`' do
|
|
@@ -72,7 +72,7 @@ RSpec.describe Shoryuken::Processor do
|
|
|
72
72
|
|
|
73
73
|
allow(sqs_msg).to receive(:body).and_return(JSON.dump(body))
|
|
74
74
|
|
|
75
|
-
subject.process
|
|
75
|
+
subject.process
|
|
76
76
|
end
|
|
77
77
|
|
|
78
78
|
it 'parses calling `.parse`' do
|
|
@@ -88,30 +88,21 @@ RSpec.describe Shoryuken::Processor do
|
|
|
88
88
|
|
|
89
89
|
allow(sqs_msg).to receive(:body).and_return(JSON.dump(body))
|
|
90
90
|
|
|
91
|
-
subject.process
|
|
91
|
+
subject.process
|
|
92
92
|
end
|
|
93
93
|
|
|
94
94
|
context 'when parse errors' do
|
|
95
95
|
before do
|
|
96
96
|
TestWorker.get_shoryuken_options['body_parser'] = :json
|
|
97
97
|
|
|
98
|
-
allow(sqs_msg).to receive(:body).and_return('invalid
|
|
98
|
+
allow(sqs_msg).to receive(:body).and_return('invalid JSON')
|
|
99
99
|
end
|
|
100
100
|
|
|
101
|
-
|
|
102
|
-
expect(
|
|
103
|
-
expect(subject.logger).to receive(:error) do |&block|
|
|
104
|
-
expect(block.call).
|
|
105
|
-
to include("unexpected token at 'invalid json'\nbody_parser: json\nsqs_msg.body: invalid json")
|
|
106
|
-
end
|
|
107
|
-
|
|
108
|
-
subject.process(queue, sqs_msg) rescue nil
|
|
109
|
-
end
|
|
101
|
+
specify do
|
|
102
|
+
expect(subject.logger).to receive(:error).twice
|
|
110
103
|
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
expect { subject.process(queue, sqs_msg) }.
|
|
114
|
-
to raise_error(JSON::ParserError, /unexpected token at 'invalid json'/)
|
|
104
|
+
expect { subject.process }.
|
|
105
|
+
to raise_error(JSON::ParserError, /unexpected token at 'invalid JSON'/)
|
|
115
106
|
end
|
|
116
107
|
end
|
|
117
108
|
|
|
@@ -125,7 +116,7 @@ RSpec.describe Shoryuken::Processor do
|
|
|
125
116
|
|
|
126
117
|
allow(sqs_msg).to receive(:body).and_return(body)
|
|
127
118
|
|
|
128
|
-
subject.process
|
|
119
|
+
subject.process
|
|
129
120
|
end
|
|
130
121
|
end
|
|
131
122
|
|
|
@@ -152,7 +143,7 @@ RSpec.describe Shoryuken::Processor do
|
|
|
152
143
|
|
|
153
144
|
context 'server' do
|
|
154
145
|
before do
|
|
155
|
-
allow(Shoryuken).to receive(:server?).and_return(true)
|
|
146
|
+
allow(Shoryuken::Options).to receive(:server?).and_return(true)
|
|
156
147
|
WorkerCalledMiddlewareWorker.instance_variable_set(:@server_chain, nil) # un-memoize middleware
|
|
157
148
|
|
|
158
149
|
Shoryuken.configure_server do |config|
|
|
@@ -171,12 +162,10 @@ RSpec.describe Shoryuken::Processor do
|
|
|
171
162
|
end
|
|
172
163
|
|
|
173
164
|
it 'invokes middleware' do
|
|
174
|
-
expect(manager).to receive(:processor_done).with(queue)
|
|
175
|
-
|
|
176
165
|
expect_any_instance_of(WorkerCalledMiddlewareWorker).to receive(:perform).with(sqs_msg, sqs_msg.body)
|
|
177
166
|
expect_any_instance_of(WorkerCalledMiddlewareWorker).to receive(:called).with(sqs_msg, queue)
|
|
178
167
|
|
|
179
|
-
subject.process
|
|
168
|
+
subject.process
|
|
180
169
|
end
|
|
181
170
|
end
|
|
182
171
|
|
|
@@ -201,12 +190,10 @@ RSpec.describe Shoryuken::Processor do
|
|
|
201
190
|
end
|
|
202
191
|
|
|
203
192
|
it "doesn't invoke middleware" do
|
|
204
|
-
expect(manager).to receive(:processor_done).with(queue)
|
|
205
|
-
|
|
206
193
|
expect_any_instance_of(WorkerCalledMiddlewareWorker).to receive(:perform).with(sqs_msg, sqs_msg.body)
|
|
207
194
|
expect_any_instance_of(WorkerCalledMiddlewareWorker).to_not receive(:called).with(sqs_msg, queue)
|
|
208
195
|
|
|
209
|
-
subject.process
|
|
196
|
+
subject.process
|
|
210
197
|
end
|
|
211
198
|
end
|
|
212
199
|
end
|
|
@@ -214,25 +201,21 @@ RSpec.describe Shoryuken::Processor do
|
|
|
214
201
|
it 'performs with delete' do
|
|
215
202
|
TestWorker.get_shoryuken_options['auto_delete'] = true
|
|
216
203
|
|
|
217
|
-
expect(manager).to receive(:processor_done).with(queue)
|
|
218
|
-
|
|
219
204
|
expect_any_instance_of(TestWorker).to receive(:perform).with(sqs_msg, sqs_msg.body)
|
|
220
205
|
|
|
221
206
|
expect(sqs_queue).to receive(:delete_messages).with(entries: [{ id: '0', receipt_handle: sqs_msg.receipt_handle }])
|
|
222
207
|
|
|
223
|
-
subject.process
|
|
208
|
+
subject.process
|
|
224
209
|
end
|
|
225
210
|
|
|
226
211
|
it 'performs without delete' do
|
|
227
212
|
TestWorker.get_shoryuken_options['auto_delete'] = false
|
|
228
213
|
|
|
229
|
-
expect(manager).to receive(:processor_done).with(queue)
|
|
230
|
-
|
|
231
214
|
expect_any_instance_of(TestWorker).to receive(:perform).with(sqs_msg, sqs_msg.body)
|
|
232
215
|
|
|
233
216
|
expect(sqs_queue).to_not receive(:delete_messages)
|
|
234
217
|
|
|
235
|
-
subject.process
|
|
218
|
+
subject.process
|
|
236
219
|
end
|
|
237
220
|
|
|
238
221
|
context 'when shoryuken_class header' do
|
|
@@ -251,13 +234,11 @@ RSpec.describe Shoryuken::Processor do
|
|
|
251
234
|
it 'performs without delete' do
|
|
252
235
|
Shoryuken.worker_registry.clear # unregister TestWorker
|
|
253
236
|
|
|
254
|
-
expect(manager).to receive(:processor_done).with(queue)
|
|
255
|
-
|
|
256
237
|
expect_any_instance_of(TestWorker).to receive(:perform).with(sqs_msg, sqs_msg.body)
|
|
257
238
|
|
|
258
239
|
expect(sqs_queue).to_not receive(:delete_messages)
|
|
259
240
|
|
|
260
|
-
subject.process
|
|
241
|
+
subject.process
|
|
261
242
|
end
|
|
262
243
|
end
|
|
263
244
|
end
|
|
@@ -22,6 +22,14 @@ RSpec.describe Shoryuken::Queue do
|
|
|
22
22
|
expect(subject.name).to eq(queue_name)
|
|
23
23
|
end
|
|
24
24
|
end
|
|
25
|
+
|
|
26
|
+
context 'when queue name supplied' do
|
|
27
|
+
subject { described_class.new(sqs, queue_name) }
|
|
28
|
+
|
|
29
|
+
specify do
|
|
30
|
+
expect(subject.name).to eq(queue_name)
|
|
31
|
+
end
|
|
32
|
+
end
|
|
25
33
|
end
|
|
26
34
|
|
|
27
35
|
describe '#delete_messages' do
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
require 'spec_helper'
|
|
2
2
|
require 'shoryuken/runner'
|
|
3
|
-
require 'shoryuken/launcher'
|
|
4
3
|
|
|
5
4
|
# rubocop:disable Metrics/BlockLength
|
|
6
5
|
RSpec.describe Shoryuken::Runner do
|
|
@@ -17,10 +16,10 @@ RSpec.describe Shoryuken::Runner do
|
|
|
17
16
|
describe '#run' do
|
|
18
17
|
let(:launcher) { instance_double('Shoryuken::Launcher') }
|
|
19
18
|
|
|
20
|
-
before
|
|
19
|
+
before do
|
|
21
20
|
allow(Shoryuken::Launcher).to receive(:new).and_return(launcher)
|
|
22
|
-
allow(launcher).to receive(:
|
|
23
|
-
allow(launcher).to receive(:stop)
|
|
21
|
+
allow(launcher).to receive(:start).and_raise(Interrupt)
|
|
22
|
+
allow(launcher).to receive(:stop!)
|
|
24
23
|
end
|
|
25
24
|
|
|
26
25
|
it 'does not raise' do
|
data/spec/shoryuken_spec.rb
CHANGED
|
@@ -1,63 +1,4 @@
|
|
|
1
1
|
require 'spec_helper'
|
|
2
2
|
|
|
3
3
|
RSpec.describe Shoryuken do
|
|
4
|
-
describe '.add_queue' do
|
|
5
|
-
after { Shoryuken.queues.clear }
|
|
6
|
-
|
|
7
|
-
it 'adds queues' do
|
|
8
|
-
described_class.add_queue('default')
|
|
9
|
-
expect(described_class.queues).to eq(['default'])
|
|
10
|
-
|
|
11
|
-
described_class.add_queue('high', 2)
|
|
12
|
-
expect(described_class.queues).to eq(%w(default high high))
|
|
13
|
-
end
|
|
14
|
-
end
|
|
15
|
-
|
|
16
|
-
describe '.register_worker' do
|
|
17
|
-
it 'registers a worker' do
|
|
18
|
-
described_class.worker_registry.clear
|
|
19
|
-
described_class.register_worker('default', TestWorker)
|
|
20
|
-
expect(described_class.worker_registry.workers('default')).to eq([TestWorker])
|
|
21
|
-
end
|
|
22
|
-
|
|
23
|
-
it 'registers a batchable worker' do
|
|
24
|
-
described_class.worker_registry.clear
|
|
25
|
-
TestWorker.get_shoryuken_options['batch'] = true
|
|
26
|
-
described_class.register_worker('default', TestWorker)
|
|
27
|
-
expect(described_class.worker_registry.workers('default')).to eq([TestWorker])
|
|
28
|
-
end
|
|
29
|
-
|
|
30
|
-
it 'allows multiple workers' do
|
|
31
|
-
described_class.worker_registry.clear
|
|
32
|
-
described_class.register_worker('default', TestWorker)
|
|
33
|
-
expect(described_class.worker_registry.workers('default')).to eq([TestWorker])
|
|
34
|
-
|
|
35
|
-
class Test2Worker
|
|
36
|
-
include Shoryuken::Worker
|
|
37
|
-
|
|
38
|
-
shoryuken_options queue: 'default'
|
|
39
|
-
|
|
40
|
-
def perform(sqs_msg, body); end
|
|
41
|
-
end
|
|
42
|
-
|
|
43
|
-
expect(described_class.worker_registry.workers('default')).to eq([Test2Worker])
|
|
44
|
-
end
|
|
45
|
-
|
|
46
|
-
it 'raises an exception when mixing batchable with non batchable' do
|
|
47
|
-
described_class.worker_registry.clear
|
|
48
|
-
TestWorker.get_shoryuken_options['batch'] = true
|
|
49
|
-
described_class.register_worker('default', TestWorker)
|
|
50
|
-
|
|
51
|
-
expect {
|
|
52
|
-
class BatchableWorker
|
|
53
|
-
include Shoryuken::Worker
|
|
54
|
-
|
|
55
|
-
shoryuken_options queue: 'default', batch: true
|
|
56
|
-
|
|
57
|
-
def perform(sqs_msg, body); end
|
|
58
|
-
end
|
|
59
|
-
}.to raise_error("Could not register BatchableWorker for 'default', because TestWorker is already registered for this queue, " \
|
|
60
|
-
"and Shoryuken doesn't support a batchable worker for a queue with multiple workers")
|
|
61
|
-
end
|
|
62
|
-
end
|
|
63
4
|
end
|
data/spec/spec_helper.rb
CHANGED
|
@@ -32,14 +32,15 @@ RSpec.configure do |config|
|
|
|
32
32
|
# The AWS_ACCESS_KEY_ID checker is because Travis CI
|
|
33
33
|
# does not expose ENV variables to pull requests from forked repositories
|
|
34
34
|
# http://docs.travis-ci.com/user/pull-requests/
|
|
35
|
-
config.filter_run_excluding slow: true if ENV['SPEC_ALL'] != 'true' || ENV['AWS_ACCESS_KEY_ID'].nil?
|
|
35
|
+
# config.filter_run_excluding slow: true if ENV['SPEC_ALL'] != 'true' || ENV['AWS_ACCESS_KEY_ID'].nil?
|
|
36
|
+
config.filter_run_excluding slow: true
|
|
36
37
|
|
|
37
38
|
config.before do
|
|
38
39
|
Shoryuken::Client.class_variable_set :@@queues, {}
|
|
39
40
|
|
|
40
41
|
Shoryuken::Client.sqs = nil
|
|
41
42
|
|
|
42
|
-
Shoryuken.
|
|
43
|
+
Shoryuken.groups.clear
|
|
43
44
|
|
|
44
45
|
Shoryuken.options[:concurrency] = 1
|
|
45
46
|
Shoryuken.options[:delay] = 1
|
|
@@ -53,5 +54,9 @@ RSpec.configure do |config|
|
|
|
53
54
|
|
|
54
55
|
Shoryuken.worker_registry.clear
|
|
55
56
|
Shoryuken.register_worker('default', TestWorker)
|
|
57
|
+
|
|
58
|
+
Aws.config[:stub_responses] = true
|
|
59
|
+
|
|
60
|
+
allow(Concurrent).to receive(:global_io_executor).and_return(Concurrent::ImmediateExecutor.new)
|
|
56
61
|
end
|
|
57
62
|
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: shoryuken
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 3.0
|
|
4
|
+
version: 3.1.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Pablo Cantero
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2017-
|
|
11
|
+
date: 2017-07-03 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: bundler
|
|
@@ -84,14 +84,14 @@ dependencies:
|
|
|
84
84
|
name: aws-sdk-core
|
|
85
85
|
requirement: !ruby/object:Gem::Requirement
|
|
86
86
|
requirements:
|
|
87
|
-
- - "
|
|
87
|
+
- - ">"
|
|
88
88
|
- !ruby/object:Gem::Version
|
|
89
89
|
version: '2'
|
|
90
90
|
type: :runtime
|
|
91
91
|
prerelease: false
|
|
92
92
|
version_requirements: !ruby/object:Gem::Requirement
|
|
93
93
|
requirements:
|
|
94
|
-
- - "
|
|
94
|
+
- - ">"
|
|
95
95
|
- !ruby/object:Gem::Version
|
|
96
96
|
version: '2'
|
|
97
97
|
- !ruby/object:Gem::Dependency
|
|
@@ -162,7 +162,10 @@ files:
|
|
|
162
162
|
- lib/shoryuken/middleware/server/auto_extend_visibility.rb
|
|
163
163
|
- lib/shoryuken/middleware/server/exponential_backoff_retry.rb
|
|
164
164
|
- lib/shoryuken/middleware/server/timing.rb
|
|
165
|
-
- lib/shoryuken/
|
|
165
|
+
- lib/shoryuken/options.rb
|
|
166
|
+
- lib/shoryuken/polling/base.rb
|
|
167
|
+
- lib/shoryuken/polling/strict_priority.rb
|
|
168
|
+
- lib/shoryuken/polling/weighted_round_robin.rb
|
|
166
169
|
- lib/shoryuken/processor.rb
|
|
167
170
|
- lib/shoryuken/queue.rb
|
|
168
171
|
- lib/shoryuken/runner.rb
|
|
@@ -185,7 +188,9 @@ files:
|
|
|
185
188
|
- spec/shoryuken/middleware/server/auto_extend_visibility_spec.rb
|
|
186
189
|
- spec/shoryuken/middleware/server/exponential_backoff_retry_spec.rb
|
|
187
190
|
- spec/shoryuken/middleware/server/timing_spec.rb
|
|
188
|
-
- spec/shoryuken/
|
|
191
|
+
- spec/shoryuken/options_spec.rb
|
|
192
|
+
- spec/shoryuken/polling/strict_priority_spec.rb
|
|
193
|
+
- spec/shoryuken/polling/weighted_round_robin_spec.rb
|
|
189
194
|
- spec/shoryuken/processor_spec.rb
|
|
190
195
|
- spec/shoryuken/queue_spec.rb
|
|
191
196
|
- spec/shoryuken/runner_spec.rb
|
|
@@ -233,7 +238,9 @@ test_files:
|
|
|
233
238
|
- spec/shoryuken/middleware/server/auto_extend_visibility_spec.rb
|
|
234
239
|
- spec/shoryuken/middleware/server/exponential_backoff_retry_spec.rb
|
|
235
240
|
- spec/shoryuken/middleware/server/timing_spec.rb
|
|
236
|
-
- spec/shoryuken/
|
|
241
|
+
- spec/shoryuken/options_spec.rb
|
|
242
|
+
- spec/shoryuken/polling/strict_priority_spec.rb
|
|
243
|
+
- spec/shoryuken/polling/weighted_round_robin_spec.rb
|
|
237
244
|
- spec/shoryuken/processor_spec.rb
|
|
238
245
|
- spec/shoryuken/queue_spec.rb
|
|
239
246
|
- spec/shoryuken/runner_spec.rb
|
data/lib/shoryuken/polling.rb
DELETED
|
@@ -1,204 +0,0 @@
|
|
|
1
|
-
module Shoryuken
|
|
2
|
-
module Polling
|
|
3
|
-
QueueConfiguration = Struct.new(:name, :options) do
|
|
4
|
-
def hash
|
|
5
|
-
name.hash
|
|
6
|
-
end
|
|
7
|
-
|
|
8
|
-
def ==(other)
|
|
9
|
-
case other
|
|
10
|
-
when String
|
|
11
|
-
if options.empty?
|
|
12
|
-
name == other
|
|
13
|
-
else
|
|
14
|
-
false
|
|
15
|
-
end
|
|
16
|
-
else
|
|
17
|
-
super
|
|
18
|
-
end
|
|
19
|
-
end
|
|
20
|
-
|
|
21
|
-
alias_method :eql?, :==
|
|
22
|
-
|
|
23
|
-
def to_s
|
|
24
|
-
if options.empty?
|
|
25
|
-
name
|
|
26
|
-
else
|
|
27
|
-
"#<QueueConfiguration #{name} options=#{options.inspect}>"
|
|
28
|
-
end
|
|
29
|
-
end
|
|
30
|
-
end
|
|
31
|
-
|
|
32
|
-
class BaseStrategy
|
|
33
|
-
include Util
|
|
34
|
-
|
|
35
|
-
def next_queue
|
|
36
|
-
fail NotImplementedError
|
|
37
|
-
end
|
|
38
|
-
|
|
39
|
-
def messages_found(queue, messages_found)
|
|
40
|
-
fail NotImplementedError
|
|
41
|
-
end
|
|
42
|
-
|
|
43
|
-
def active_queues
|
|
44
|
-
fail NotImplementedError
|
|
45
|
-
end
|
|
46
|
-
|
|
47
|
-
def ==(other)
|
|
48
|
-
case other
|
|
49
|
-
when Array
|
|
50
|
-
@queues == other
|
|
51
|
-
else
|
|
52
|
-
if other.respond_to?(:active_queues)
|
|
53
|
-
active_queues == other.active_queues
|
|
54
|
-
else
|
|
55
|
-
false
|
|
56
|
-
end
|
|
57
|
-
end
|
|
58
|
-
end
|
|
59
|
-
|
|
60
|
-
private
|
|
61
|
-
|
|
62
|
-
def delay
|
|
63
|
-
Shoryuken.options[:delay].to_f
|
|
64
|
-
end
|
|
65
|
-
end
|
|
66
|
-
|
|
67
|
-
class WeightedRoundRobin < BaseStrategy
|
|
68
|
-
def initialize(queues)
|
|
69
|
-
@initial_queues = queues
|
|
70
|
-
@queues = queues.dup.uniq
|
|
71
|
-
@paused_queues = []
|
|
72
|
-
end
|
|
73
|
-
|
|
74
|
-
def next_queue
|
|
75
|
-
unpause_queues
|
|
76
|
-
queue = @queues.shift
|
|
77
|
-
return nil if queue.nil?
|
|
78
|
-
|
|
79
|
-
@queues << queue
|
|
80
|
-
QueueConfiguration.new(queue, {})
|
|
81
|
-
end
|
|
82
|
-
|
|
83
|
-
def messages_found(queue, messages_found)
|
|
84
|
-
if messages_found == 0
|
|
85
|
-
pause(queue)
|
|
86
|
-
return
|
|
87
|
-
end
|
|
88
|
-
|
|
89
|
-
maximum_weight = maximum_queue_weight(queue)
|
|
90
|
-
current_weight = current_queue_weight(queue)
|
|
91
|
-
if maximum_weight > current_weight
|
|
92
|
-
logger.info { "Increasing #{queue} weight to #{current_weight + 1}, max: #{maximum_weight}" }
|
|
93
|
-
@queues << queue
|
|
94
|
-
end
|
|
95
|
-
end
|
|
96
|
-
|
|
97
|
-
def active_queues
|
|
98
|
-
unparse_queues(@queues)
|
|
99
|
-
end
|
|
100
|
-
|
|
101
|
-
private
|
|
102
|
-
|
|
103
|
-
def pause(queue)
|
|
104
|
-
return unless @queues.delete(queue)
|
|
105
|
-
@paused_queues << [Time.now + delay, queue]
|
|
106
|
-
logger.debug "Paused #{queue}"
|
|
107
|
-
end
|
|
108
|
-
|
|
109
|
-
def unpause_queues
|
|
110
|
-
return if @paused_queues.empty?
|
|
111
|
-
return if Time.now < @paused_queues.first[0]
|
|
112
|
-
pause = @paused_queues.shift
|
|
113
|
-
@queues << pause[1]
|
|
114
|
-
logger.debug "Unpaused #{pause[1]}"
|
|
115
|
-
end
|
|
116
|
-
|
|
117
|
-
def current_queue_weight(queue)
|
|
118
|
-
queue_weight(@queues, queue)
|
|
119
|
-
end
|
|
120
|
-
|
|
121
|
-
def maximum_queue_weight(queue)
|
|
122
|
-
queue_weight(@initial_queues, queue)
|
|
123
|
-
end
|
|
124
|
-
|
|
125
|
-
def queue_weight(queues, queue)
|
|
126
|
-
queues.count { |q| q == queue }
|
|
127
|
-
end
|
|
128
|
-
end
|
|
129
|
-
|
|
130
|
-
class StrictPriority < BaseStrategy
|
|
131
|
-
def initialize(queues)
|
|
132
|
-
# Priority ordering of the queues, highest priority first
|
|
133
|
-
@queues = queues
|
|
134
|
-
.group_by { |q| q }
|
|
135
|
-
.sort_by { |_, qs| -qs.count }
|
|
136
|
-
.map(&:first)
|
|
137
|
-
|
|
138
|
-
# Pause status of the queues, default to past time (unpaused)
|
|
139
|
-
@paused_until = queues
|
|
140
|
-
.each_with_object(Hash.new) { |queue, h| h[queue] = Time.at(0) }
|
|
141
|
-
|
|
142
|
-
# Start queues at 0
|
|
143
|
-
reset_next_queue
|
|
144
|
-
end
|
|
145
|
-
|
|
146
|
-
def next_queue
|
|
147
|
-
next_queue = next_active_queue
|
|
148
|
-
next_queue.nil? ? nil : QueueConfiguration.new(next_queue, {})
|
|
149
|
-
end
|
|
150
|
-
|
|
151
|
-
def messages_found(queue, messages_found)
|
|
152
|
-
if messages_found == 0
|
|
153
|
-
pause(queue)
|
|
154
|
-
else
|
|
155
|
-
reset_next_queue
|
|
156
|
-
end
|
|
157
|
-
end
|
|
158
|
-
|
|
159
|
-
def active_queues
|
|
160
|
-
@queues
|
|
161
|
-
.reverse
|
|
162
|
-
.map.with_index(1)
|
|
163
|
-
.reject { |q, _| queue_paused?(q) }
|
|
164
|
-
.reverse
|
|
165
|
-
end
|
|
166
|
-
|
|
167
|
-
private
|
|
168
|
-
|
|
169
|
-
def next_active_queue
|
|
170
|
-
reset_next_queue if queues_unpaused_since?
|
|
171
|
-
|
|
172
|
-
size = @queues.length
|
|
173
|
-
size.times do
|
|
174
|
-
queue = @queues[@next_queue_index]
|
|
175
|
-
@next_queue_index = (@next_queue_index + 1) % size
|
|
176
|
-
return queue unless queue_paused?(queue)
|
|
177
|
-
end
|
|
178
|
-
|
|
179
|
-
nil
|
|
180
|
-
end
|
|
181
|
-
|
|
182
|
-
def queues_unpaused_since?
|
|
183
|
-
last = @last_unpause_check
|
|
184
|
-
now = @last_unpause_check = Time.now
|
|
185
|
-
|
|
186
|
-
last && @paused_until.values.any? { |t| t > last && t <= now }
|
|
187
|
-
end
|
|
188
|
-
|
|
189
|
-
def reset_next_queue
|
|
190
|
-
@next_queue_index = 0
|
|
191
|
-
end
|
|
192
|
-
|
|
193
|
-
def queue_paused?(queue)
|
|
194
|
-
@paused_until[queue] > Time.now
|
|
195
|
-
end
|
|
196
|
-
|
|
197
|
-
def pause(queue)
|
|
198
|
-
return unless delay > 0
|
|
199
|
-
@paused_until[queue] = Time.now + delay
|
|
200
|
-
logger.debug "Paused #{queue}"
|
|
201
|
-
end
|
|
202
|
-
end
|
|
203
|
-
end
|
|
204
|
-
end
|