sneakers 0.0.7 → 1.0.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 +7 -0
- data/.gitignore +4 -1
- data/CHANGELOG.md +20 -0
- data/LICENSE.txt +2 -2
- data/README.md +15 -0
- data/ROADMAP.md +18 -0
- data/Rakefile +1 -0
- data/examples/benchmark_worker.rb +0 -1
- data/examples/max_retry_handler.rb +78 -0
- data/examples/metrics_worker.rb +1 -1
- data/examples/newrelic_metrics_worker.rb +40 -0
- data/examples/profiling_worker.rb +35 -21
- data/examples/workflow_worker.rb +0 -1
- data/lib/sneakers/cli.rb +7 -3
- data/lib/sneakers/concerns/logging.rb +1 -1
- data/lib/sneakers/configuration.rb +47 -0
- data/lib/sneakers/handlers/maxretry.rb +183 -0
- data/lib/sneakers/handlers/oneshot.rb +11 -10
- data/lib/sneakers/metrics/newrelic_metrics.rb +37 -0
- data/lib/sneakers/metrics/statsd_metrics.rb +1 -1
- data/lib/sneakers/publisher.rb +11 -12
- data/lib/sneakers/queue.rb +22 -7
- data/lib/sneakers/runner.rb +4 -4
- data/lib/sneakers/support/utils.rb +1 -1
- data/lib/sneakers/tasks.rb +1 -1
- data/lib/sneakers/version.rb +1 -1
- data/lib/sneakers/worker.rb +53 -23
- data/lib/sneakers/workergroup.rb +2 -2
- data/lib/sneakers.rb +23 -49
- data/sneakers.gemspec +22 -22
- data/spec/sneakers/concerns/{logging.rb → logging_spec.rb} +1 -1
- data/spec/sneakers/configuration_spec.rb +42 -0
- data/spec/sneakers/publisher_spec.rb +58 -17
- data/spec/sneakers/queue_spec.rb +64 -18
- data/spec/sneakers/sneakers_spec.rb +15 -6
- data/spec/sneakers/support/utils_spec.rb +44 -0
- data/spec/sneakers/worker_handlers_spec.rb +350 -0
- data/spec/sneakers/worker_spec.rb +130 -45
- data/spec/spec_helper.rb +3 -1
- metadata +52 -67
- data/Gemfile.lock +0 -153
- data/lib/sneakers/support/queue_name.rb +0 -14
- /data/spec/sneakers/concerns/{metrics.rb → metrics_spec.rb} +0 -0
|
@@ -1,37 +1,78 @@
|
|
|
1
1
|
require 'spec_helper'
|
|
2
2
|
require 'sneakers'
|
|
3
3
|
|
|
4
|
-
|
|
5
4
|
describe Sneakers::Publisher do
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
5
|
+
describe '#publish' do
|
|
6
|
+
it 'should publish a message to an exchange' do
|
|
7
|
+
xchg = Object.new
|
|
8
|
+
mock(xchg).publish('test msg', routing_key: 'downloads')
|
|
9
|
+
|
|
10
|
+
p = Sneakers::Publisher.new
|
|
11
|
+
p.instance_variable_set(:@exchange, xchg)
|
|
12
|
+
|
|
13
|
+
mock(p).ensure_connection! {}
|
|
14
|
+
p.publish('test msg', to_queue: 'downloads')
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
it 'should publish with the persistence specified' do
|
|
18
|
+
xchg = Object.new
|
|
19
|
+
mock(xchg).publish('test msg', routing_key: 'downloads', persistence: true)
|
|
20
|
+
|
|
21
|
+
p = Sneakers::Publisher.new
|
|
22
|
+
p.instance_variable_set(:@exchange, xchg)
|
|
23
|
+
|
|
24
|
+
mock(p).ensure_connection! {}
|
|
25
|
+
p.publish('test msg', to_queue: 'downloads', persistence: true)
|
|
26
|
+
end
|
|
9
27
|
|
|
10
|
-
|
|
11
|
-
it "should publish a message to an exchange" do
|
|
28
|
+
it 'should publish with arbitrary metadata specified' do
|
|
12
29
|
xchg = Object.new
|
|
13
|
-
mock(xchg).publish(
|
|
30
|
+
mock(xchg).publish('test msg', routing_key: 'downloads', expiration: 1, headers: {foo: 'bar'})
|
|
14
31
|
|
|
15
32
|
p = Sneakers::Publisher.new
|
|
16
|
-
p.exchange
|
|
33
|
+
p.instance_variable_set(:@exchange, xchg)
|
|
17
34
|
|
|
18
|
-
mock(p).ensure_connection!{}
|
|
19
|
-
p.publish(
|
|
35
|
+
mock(p).ensure_connection! {}
|
|
36
|
+
p.publish('test msg', to_queue: 'downloads', expiration: 1, headers: {foo: 'bar'})
|
|
20
37
|
end
|
|
21
38
|
|
|
22
|
-
it
|
|
39
|
+
it 'should not reconnect if already connected' do
|
|
23
40
|
xchg = Object.new
|
|
24
|
-
mock(xchg).publish(
|
|
41
|
+
mock(xchg).publish('test msg', routing_key: 'downloads')
|
|
25
42
|
|
|
26
43
|
p = Sneakers::Publisher.new
|
|
27
|
-
p.exchange
|
|
28
|
-
|
|
44
|
+
p.instance_variable_set(:@exchange, xchg)
|
|
45
|
+
|
|
46
|
+
mock(p).connected? { true }
|
|
29
47
|
mock(p).ensure_connection!.times(0)
|
|
30
48
|
|
|
31
|
-
p.publish(
|
|
49
|
+
p.publish('test msg', to_queue: 'downloads')
|
|
32
50
|
end
|
|
33
|
-
end
|
|
34
51
|
|
|
52
|
+
it 'should connect to rabbitmq configured on Sneakers.configure' do
|
|
53
|
+
logger = Logger.new('/dev/null')
|
|
54
|
+
Sneakers.configure(
|
|
55
|
+
amqp: 'amqp://someuser:somepassword@somehost:5672',
|
|
56
|
+
heartbeat: 1, exchange: 'another_exchange',
|
|
57
|
+
exchange_type: :topic,
|
|
58
|
+
log: logger,
|
|
59
|
+
durable: false)
|
|
35
60
|
|
|
36
|
-
|
|
61
|
+
channel = Object.new
|
|
62
|
+
mock(channel).exchange('another_exchange', type: :topic, durable: false) do
|
|
63
|
+
mock(Object.new).publish('test msg', routing_key: 'downloads')
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
bunny = Object.new
|
|
67
|
+
mock(bunny).start
|
|
68
|
+
mock(bunny).create_channel { channel }
|
|
69
|
+
|
|
70
|
+
mock(Bunny).new('amqp://someuser:somepassword@somehost:5672', heartbeat: 1, vhost: '/', logger: logger) { bunny }
|
|
37
71
|
|
|
72
|
+
p = Sneakers::Publisher.new
|
|
73
|
+
|
|
74
|
+
p.publish('test msg', to_queue: 'downloads')
|
|
75
|
+
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
end
|
data/spec/sneakers/queue_spec.rb
CHANGED
|
@@ -5,12 +5,12 @@ require 'sneakers'
|
|
|
5
5
|
|
|
6
6
|
describe Sneakers::Queue do
|
|
7
7
|
before do
|
|
8
|
-
Sneakers.configure
|
|
8
|
+
Sneakers.configure
|
|
9
9
|
end
|
|
10
10
|
|
|
11
11
|
describe "#subscribe" do
|
|
12
|
-
|
|
13
|
-
|
|
12
|
+
let :queue_vars do
|
|
13
|
+
{
|
|
14
14
|
:prefetch => 25,
|
|
15
15
|
:durable => true,
|
|
16
16
|
:ack => true,
|
|
@@ -18,27 +18,73 @@ describe Sneakers::Queue do
|
|
|
18
18
|
:vhost => '/',
|
|
19
19
|
:exchange => "sneakers",
|
|
20
20
|
:exchange_type => :direct
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
mkchan = Object.new
|
|
24
|
-
mkex = Object.new
|
|
25
|
-
mkqueue = Object.new
|
|
21
|
+
}
|
|
22
|
+
end
|
|
26
23
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
24
|
+
before do
|
|
25
|
+
@mkbunny = Object.new
|
|
26
|
+
@mkchan = Object.new
|
|
27
|
+
@mkex = Object.new
|
|
28
|
+
@mkqueue = Object.new
|
|
29
|
+
@mkqueue_nondurable = Object.new
|
|
30
|
+
@mkworker = Object.new
|
|
30
31
|
|
|
31
|
-
mock(
|
|
32
|
-
mock(
|
|
33
|
-
mock(
|
|
32
|
+
mock(@mkbunny).start {}
|
|
33
|
+
mock(@mkbunny).create_channel{ @mkchan }
|
|
34
|
+
mock(Bunny).new(anything, :vhost => '/', :heartbeat => 2){ @mkbunny }
|
|
34
35
|
|
|
35
|
-
mock(
|
|
36
|
-
mock(
|
|
36
|
+
mock(@mkchan).prefetch(25)
|
|
37
|
+
mock(@mkchan).exchange("sneakers", :type => :direct, :durable => true){ @mkex }
|
|
37
38
|
|
|
38
|
-
|
|
39
|
+
stub(@mkworker).opts { { :exchange => 'test-exchange' } }
|
|
39
40
|
end
|
|
40
|
-
end
|
|
41
41
|
|
|
42
|
+
it "should setup a bunny queue according to configuration values" do
|
|
43
|
+
mock(@mkchan).queue("downloads", :durable => true) { @mkqueue }
|
|
44
|
+
q = Sneakers::Queue.new("downloads", queue_vars)
|
|
45
|
+
|
|
46
|
+
mock(@mkqueue).bind(@mkex, :routing_key => "downloads")
|
|
47
|
+
mock(@mkqueue).subscribe(:block => false, :ack => true)
|
|
48
|
+
|
|
49
|
+
q.subscribe(@mkworker)
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
it "supports multiple routing_keys" do
|
|
53
|
+
mock(@mkchan).queue("downloads", :durable => true) { @mkqueue }
|
|
54
|
+
q = Sneakers::Queue.new("downloads",
|
|
55
|
+
queue_vars.merge(:routing_key => ["alpha", "beta"]))
|
|
56
|
+
|
|
57
|
+
mock(@mkqueue).bind(@mkex, :routing_key => "alpha")
|
|
58
|
+
mock(@mkqueue).bind(@mkex, :routing_key => "beta")
|
|
59
|
+
mock(@mkqueue).subscribe(:block => false, :ack => true)
|
|
42
60
|
|
|
61
|
+
q.subscribe(@mkworker)
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
it "will use whatever handler the worker specifies" do
|
|
65
|
+
mock(@mkchan).queue("downloads", :durable => true) { @mkqueue }
|
|
66
|
+
@handler = Object.new
|
|
67
|
+
worker_opts = { :handler => @handler }
|
|
68
|
+
stub(@mkworker).opts { worker_opts }
|
|
69
|
+
mock(@handler).new(@mkchan, @mkqueue, worker_opts).once
|
|
70
|
+
|
|
71
|
+
stub(@mkqueue).bind
|
|
72
|
+
stub(@mkqueue).subscribe
|
|
73
|
+
q = Sneakers::Queue.new("downloads", queue_vars)
|
|
74
|
+
q.subscribe(@mkworker)
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
it "creates a non-durable queue if :queue_durable => false" do
|
|
78
|
+
mock(@mkchan).queue("test_nondurable", :durable => false) { @mkqueue_nondurable }
|
|
79
|
+
queue_vars[:queue_durable] = false
|
|
80
|
+
q = Sneakers::Queue.new("test_nondurable", queue_vars)
|
|
81
|
+
|
|
82
|
+
mock(@mkqueue_nondurable).bind(@mkex, :routing_key => "test_nondurable")
|
|
83
|
+
mock(@mkqueue_nondurable).subscribe(:block => false, :ack => true)
|
|
84
|
+
|
|
85
|
+
q.subscribe(@mkworker)
|
|
86
|
+
myqueue = q.instance_variable_get(:@queue)
|
|
87
|
+
end
|
|
88
|
+
end
|
|
43
89
|
end
|
|
44
90
|
|
|
@@ -1,6 +1,13 @@
|
|
|
1
1
|
require 'spec_helper'
|
|
2
2
|
require 'sneakers'
|
|
3
3
|
|
|
4
|
+
class EnvWorker
|
|
5
|
+
include Sneakers::Worker
|
|
6
|
+
from_queue 'defaults'
|
|
7
|
+
|
|
8
|
+
def work(msg)
|
|
9
|
+
end
|
|
10
|
+
end
|
|
4
11
|
|
|
5
12
|
|
|
6
13
|
describe Sneakers do
|
|
@@ -10,20 +17,21 @@ describe Sneakers do
|
|
|
10
17
|
|
|
11
18
|
describe 'self' do
|
|
12
19
|
it 'should have defaults set up' do
|
|
13
|
-
Sneakers::
|
|
20
|
+
Sneakers::CONFIG[:log].must_equal(STDOUT)
|
|
14
21
|
end
|
|
15
22
|
|
|
16
23
|
it 'should configure itself' do
|
|
17
24
|
Sneakers.configure
|
|
18
25
|
Sneakers.logger.wont_be_nil
|
|
26
|
+
Sneakers.configured?.must_equal(true)
|
|
19
27
|
end
|
|
20
28
|
end
|
|
21
29
|
|
|
22
30
|
describe '.daemonize!' do
|
|
23
31
|
it 'should set a logger to a default info level and not daemonize' do
|
|
24
32
|
Sneakers.daemonize!
|
|
25
|
-
Sneakers::
|
|
26
|
-
Sneakers::
|
|
33
|
+
Sneakers::CONFIG[:log].must_equal('sneakers.log')
|
|
34
|
+
Sneakers::CONFIG[:daemonize].must_equal(true)
|
|
27
35
|
Sneakers.logger.level.must_equal(Logger::INFO)
|
|
28
36
|
end
|
|
29
37
|
|
|
@@ -33,13 +41,14 @@ describe Sneakers do
|
|
|
33
41
|
end
|
|
34
42
|
end
|
|
35
43
|
|
|
44
|
+
|
|
36
45
|
describe '.clear!' do
|
|
37
46
|
it 'must reset dirty configuration to default' do
|
|
38
|
-
Sneakers::
|
|
47
|
+
Sneakers::CONFIG[:log].must_equal(STDOUT)
|
|
39
48
|
Sneakers.configure(:log => 'foobar.log')
|
|
40
|
-
Sneakers::
|
|
49
|
+
Sneakers::CONFIG[:log].must_equal('foobar.log')
|
|
41
50
|
Sneakers.clear!
|
|
42
|
-
Sneakers::
|
|
51
|
+
Sneakers::CONFIG[:log].must_equal(STDOUT)
|
|
43
52
|
end
|
|
44
53
|
end
|
|
45
54
|
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
require 'sneakers'
|
|
3
|
+
|
|
4
|
+
describe Sneakers::Utils do
|
|
5
|
+
describe '::parse_workers' do
|
|
6
|
+
before(:all) do
|
|
7
|
+
class Foo; end
|
|
8
|
+
class Bar; end
|
|
9
|
+
class Baz
|
|
10
|
+
class Quux; end
|
|
11
|
+
class Corge; end
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
describe 'given a single class name' do
|
|
16
|
+
describe 'without namespace' do
|
|
17
|
+
it 'returns the worker class name' do
|
|
18
|
+
Sneakers::Utils.parse_workers('Foo').must_equal([[Foo],[]])
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
describe 'with namespace' do
|
|
23
|
+
it 'returns the worker class name' do
|
|
24
|
+
Sneakers::Utils.parse_workers('Baz::Quux').must_equal([[Baz::Quux],[]])
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
describe 'given a list of class names' do
|
|
30
|
+
describe 'without namespaces' do
|
|
31
|
+
it 'returns all worker class names' do
|
|
32
|
+
Sneakers::Utils.parse_workers('Foo,Bar').must_equal([[Foo,Bar],[]])
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
describe 'with namespaces' do
|
|
37
|
+
it 'returns all worker class names' do
|
|
38
|
+
workers = Sneakers::Utils.parse_workers('Baz::Quux,Baz::Corge')
|
|
39
|
+
workers.must_equal([[Baz::Quux,Baz::Corge],[]])
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
@@ -0,0 +1,350 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
require 'sneakers'
|
|
3
|
+
require 'timeout'
|
|
4
|
+
require 'sneakers/handlers/oneshot'
|
|
5
|
+
require 'sneakers/handlers/maxretry'
|
|
6
|
+
require 'json'
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
# Specific tests of the Handler implementations you can use to deal with job
|
|
10
|
+
# results. These tests only make sense with a worker that requires acking.
|
|
11
|
+
|
|
12
|
+
class HandlerTestWorker
|
|
13
|
+
include Sneakers::Worker
|
|
14
|
+
from_queue 'defaults',
|
|
15
|
+
:ack => true
|
|
16
|
+
|
|
17
|
+
def work(msg)
|
|
18
|
+
if msg.is_a?(StandardError)
|
|
19
|
+
raise msg
|
|
20
|
+
elsif msg.is_a?(String)
|
|
21
|
+
hash = maybe_json(msg)
|
|
22
|
+
if hash.is_a?(Hash)
|
|
23
|
+
hash['response'].to_sym
|
|
24
|
+
else
|
|
25
|
+
hash
|
|
26
|
+
end
|
|
27
|
+
else
|
|
28
|
+
msg
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def maybe_json(string)
|
|
33
|
+
JSON.parse(string)
|
|
34
|
+
rescue
|
|
35
|
+
string
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
class TestPool
|
|
40
|
+
def process(*args,&block)
|
|
41
|
+
block.call
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
describe 'Handlers' do
|
|
47
|
+
let(:channel) { Object.new }
|
|
48
|
+
let(:queue) { Object.new }
|
|
49
|
+
let(:worker) { HandlerTestWorker.new(@queue, TestPool.new) }
|
|
50
|
+
|
|
51
|
+
before(:each) do
|
|
52
|
+
Sneakers.configure(:daemonize => true, :log => 'sneakers.log')
|
|
53
|
+
Sneakers::Worker.configure_logger(Logger.new('/dev/null'))
|
|
54
|
+
Sneakers::Worker.configure_metrics
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
describe 'Oneshot' do
|
|
58
|
+
before(:each) do
|
|
59
|
+
@opts = Object.new
|
|
60
|
+
@handler = Sneakers::Handlers::Oneshot.new(channel, queue, @opts)
|
|
61
|
+
|
|
62
|
+
@header = Object.new
|
|
63
|
+
stub(@header).delivery_tag { 37 }
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
describe '#do_work' do
|
|
67
|
+
it 'should work and handle acks' do
|
|
68
|
+
mock(channel).acknowledge(37, false)
|
|
69
|
+
|
|
70
|
+
worker.do_work(@header, nil, :ack, @handler)
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
it 'should work and handle rejects' do
|
|
74
|
+
mock(channel).reject(37, false)
|
|
75
|
+
|
|
76
|
+
worker.do_work(@header, nil, :reject, @handler)
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
it 'should work and handle requeues' do
|
|
80
|
+
mock(channel).reject(37, true)
|
|
81
|
+
|
|
82
|
+
worker.do_work(@header, nil, :requeue, @handler)
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
it 'should work and handle user-land timeouts' do
|
|
86
|
+
mock(channel).reject(37, false)
|
|
87
|
+
|
|
88
|
+
worker.do_work(@header, nil, :timeout, @handler)
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
it 'should work and handle user-land error' do
|
|
92
|
+
mock(channel).reject(37, false)
|
|
93
|
+
|
|
94
|
+
worker.do_work(@header, nil, StandardError.new('boom!'), @handler)
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
it 'should work and handle noops' do
|
|
98
|
+
worker.do_work(@header, nil, :wait, @handler)
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
describe 'Maxretry' do
|
|
105
|
+
let(:max_retries) { nil }
|
|
106
|
+
|
|
107
|
+
before(:each) do
|
|
108
|
+
@opts = {
|
|
109
|
+
:exchange => 'sneakers',
|
|
110
|
+
:durable => 'true',
|
|
111
|
+
}.tap do |opts|
|
|
112
|
+
opts[:retry_max_times] = max_retries unless max_retries.nil?
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
mock(queue).name { 'downloads' }
|
|
116
|
+
|
|
117
|
+
@retry_exchange = Object.new
|
|
118
|
+
@error_exchange = Object.new
|
|
119
|
+
@requeue_exchange = Object.new
|
|
120
|
+
|
|
121
|
+
@retry_queue = Object.new
|
|
122
|
+
@error_queue = Object.new
|
|
123
|
+
|
|
124
|
+
mock(channel).exchange('downloads-retry',
|
|
125
|
+
:type => 'topic',
|
|
126
|
+
:durable => 'true').once { @retry_exchange }
|
|
127
|
+
mock(channel).exchange('downloads-error',
|
|
128
|
+
:type => 'topic',
|
|
129
|
+
:durable => 'true').once { @error_exchange }
|
|
130
|
+
mock(channel).exchange('downloads-retry-requeue',
|
|
131
|
+
:type => 'topic',
|
|
132
|
+
:durable => 'true').once { @requeue_exchange }
|
|
133
|
+
|
|
134
|
+
mock(channel).queue('downloads-retry',
|
|
135
|
+
:durable => 'true',
|
|
136
|
+
:arguments => {
|
|
137
|
+
:'x-dead-letter-exchange' => 'downloads-retry-requeue',
|
|
138
|
+
:'x-message-ttl' => 60000
|
|
139
|
+
}
|
|
140
|
+
).once { @retry_queue }
|
|
141
|
+
mock(@retry_queue).bind(@retry_exchange, :routing_key => '#')
|
|
142
|
+
|
|
143
|
+
mock(channel).queue('downloads-error',
|
|
144
|
+
:durable => 'true').once { @error_queue }
|
|
145
|
+
mock(@error_queue).bind(@error_exchange, :routing_key => '#')
|
|
146
|
+
|
|
147
|
+
mock(queue).bind(@requeue_exchange, :routing_key => '#')
|
|
148
|
+
|
|
149
|
+
@handler = Sneakers::Handlers::Maxretry.new(channel, queue, @opts)
|
|
150
|
+
|
|
151
|
+
@header = Object.new
|
|
152
|
+
stub(@header).delivery_tag { 37 }
|
|
153
|
+
|
|
154
|
+
@props = {}
|
|
155
|
+
@props_with_x_death = {
|
|
156
|
+
:headers => {
|
|
157
|
+
"x-death" => [
|
|
158
|
+
{
|
|
159
|
+
"reason" => "expired",
|
|
160
|
+
"queue" => "downloads-retry",
|
|
161
|
+
"time" => Time.now,
|
|
162
|
+
"exchange" => "RawMail-retry",
|
|
163
|
+
"routing-keys" => ["RawMail"]
|
|
164
|
+
},
|
|
165
|
+
{
|
|
166
|
+
"reason" => "rejected",
|
|
167
|
+
"queue" => "downloads",
|
|
168
|
+
"time" => Time.now,
|
|
169
|
+
"exchange" => "",
|
|
170
|
+
"routing-keys" => ["RawMail"]
|
|
171
|
+
}
|
|
172
|
+
]
|
|
173
|
+
},
|
|
174
|
+
:delivery_mode => 1}
|
|
175
|
+
end
|
|
176
|
+
|
|
177
|
+
# it 'allows overriding the retry exchange name'
|
|
178
|
+
# it 'allows overriding the error exchange name'
|
|
179
|
+
# it 'allows overriding the retry timeout'
|
|
180
|
+
|
|
181
|
+
describe '#do_work' do
|
|
182
|
+
before do
|
|
183
|
+
@now = Time.now
|
|
184
|
+
end
|
|
185
|
+
|
|
186
|
+
# Used to stub out the publish method args. Sadly RR doesn't support
|
|
187
|
+
# this, only proxying existing methods.
|
|
188
|
+
module MockPublish
|
|
189
|
+
attr_reader :data, :opts, :called
|
|
190
|
+
|
|
191
|
+
def publish(data, opts)
|
|
192
|
+
@data = data
|
|
193
|
+
@opts = opts
|
|
194
|
+
@called = true
|
|
195
|
+
end
|
|
196
|
+
end
|
|
197
|
+
|
|
198
|
+
it 'should work and handle acks' do
|
|
199
|
+
mock(channel).acknowledge(37, false)
|
|
200
|
+
|
|
201
|
+
worker.do_work(@header, @props, :ack, @handler)
|
|
202
|
+
end
|
|
203
|
+
|
|
204
|
+
describe 'rejects' do
|
|
205
|
+
describe 'more retries ahead' do
|
|
206
|
+
it 'should work and handle rejects' do
|
|
207
|
+
mock(channel).reject(37, false)
|
|
208
|
+
|
|
209
|
+
worker.do_work(@header, @props_with_x_death, :reject, @handler)
|
|
210
|
+
end
|
|
211
|
+
end
|
|
212
|
+
|
|
213
|
+
describe 'no more retries' do
|
|
214
|
+
let(:max_retries) { 1 }
|
|
215
|
+
|
|
216
|
+
it 'sends the rejection to the error queue' do
|
|
217
|
+
mock(@header).routing_key { '#' }
|
|
218
|
+
mock(channel).acknowledge(37, false)
|
|
219
|
+
|
|
220
|
+
@error_exchange.extend MockPublish
|
|
221
|
+
worker.do_work(@header, @props_with_x_death, :reject, @handler)
|
|
222
|
+
@error_exchange.called.must_equal(true)
|
|
223
|
+
@error_exchange.opts.must_equal({ :routing_key => '#' })
|
|
224
|
+
data = JSON.parse(@error_exchange.data)
|
|
225
|
+
data['error'].must_equal('reject')
|
|
226
|
+
data['num_attempts'].must_equal(2)
|
|
227
|
+
data['payload'].must_equal(Base64.encode64(:reject.to_s))
|
|
228
|
+
Time.parse(data['failed_at']).wont_be_nil
|
|
229
|
+
end
|
|
230
|
+
|
|
231
|
+
end
|
|
232
|
+
end
|
|
233
|
+
|
|
234
|
+
describe 'requeues' do
|
|
235
|
+
it 'should work and handle requeues' do
|
|
236
|
+
mock(channel).reject(37, true)
|
|
237
|
+
|
|
238
|
+
worker.do_work(@header, @props_with_x_death, :requeue, @handler)
|
|
239
|
+
end
|
|
240
|
+
|
|
241
|
+
describe 'no more retries left' do
|
|
242
|
+
let(:max_retries) { 1 }
|
|
243
|
+
|
|
244
|
+
it 'continues to reject with requeue' do
|
|
245
|
+
mock(channel).reject(37, true)
|
|
246
|
+
|
|
247
|
+
worker.do_work(@header, @props_with_x_death, :requeue, @handler)
|
|
248
|
+
end
|
|
249
|
+
end
|
|
250
|
+
|
|
251
|
+
end
|
|
252
|
+
|
|
253
|
+
describe 'timeouts' do
|
|
254
|
+
describe 'more retries ahead' do
|
|
255
|
+
it 'should reject the message' do
|
|
256
|
+
mock(channel).reject(37, false)
|
|
257
|
+
|
|
258
|
+
worker.do_work(@header, @props_with_x_death, :timeout, @handler)
|
|
259
|
+
end
|
|
260
|
+
end
|
|
261
|
+
|
|
262
|
+
describe 'no more retries left' do
|
|
263
|
+
let(:max_retries) { 1 }
|
|
264
|
+
|
|
265
|
+
it 'sends the rejection to the error queue' do
|
|
266
|
+
mock(@header).routing_key { '#' }
|
|
267
|
+
mock(channel).acknowledge(37, false)
|
|
268
|
+
@error_exchange.extend MockPublish
|
|
269
|
+
|
|
270
|
+
worker.do_work(@header, @props_with_x_death, :timeout, @handler)
|
|
271
|
+
@error_exchange.called.must_equal(true)
|
|
272
|
+
@error_exchange.opts.must_equal({ :routing_key => '#' })
|
|
273
|
+
data = JSON.parse(@error_exchange.data)
|
|
274
|
+
data['error'].must_equal('timeout')
|
|
275
|
+
data['num_attempts'].must_equal(2)
|
|
276
|
+
data['payload'].must_equal(Base64.encode64(:timeout.to_s))
|
|
277
|
+
Time.parse(data['failed_at']).wont_be_nil
|
|
278
|
+
end
|
|
279
|
+
end
|
|
280
|
+
end
|
|
281
|
+
|
|
282
|
+
describe 'exceptions' do
|
|
283
|
+
describe 'more retries ahead' do
|
|
284
|
+
it 'should reject the message' do
|
|
285
|
+
mock(channel).reject(37, false)
|
|
286
|
+
|
|
287
|
+
worker.do_work(@header, @props_with_x_death, StandardError.new('boom!'), @handler)
|
|
288
|
+
end
|
|
289
|
+
end
|
|
290
|
+
|
|
291
|
+
describe 'no more retries left' do
|
|
292
|
+
let(:max_retries) { 1 }
|
|
293
|
+
|
|
294
|
+
it 'sends the rejection to the error queue' do
|
|
295
|
+
mock(@header).routing_key { '#' }
|
|
296
|
+
mock(channel).acknowledge(37, false)
|
|
297
|
+
@error_exchange.extend MockPublish
|
|
298
|
+
|
|
299
|
+
worker.do_work(@header, @props_with_x_death, StandardError.new('boom!'), @handler)
|
|
300
|
+
@error_exchange.called.must_equal(true)
|
|
301
|
+
@error_exchange.opts.must_equal({ :routing_key => '#' })
|
|
302
|
+
data = JSON.parse(@error_exchange.data)
|
|
303
|
+
data['error'].must_equal('boom!')
|
|
304
|
+
data['error_class'].must_equal(StandardError.to_s)
|
|
305
|
+
data['backtrace'].wont_be_nil
|
|
306
|
+
data['num_attempts'].must_equal(2)
|
|
307
|
+
data['payload'].must_equal(Base64.encode64('boom!'))
|
|
308
|
+
Time.parse(data['failed_at']).wont_be_nil
|
|
309
|
+
end
|
|
310
|
+
end
|
|
311
|
+
end
|
|
312
|
+
|
|
313
|
+
it 'should work and handle user-land error' do
|
|
314
|
+
mock(channel).reject(37, false)
|
|
315
|
+
|
|
316
|
+
worker.do_work(@header, @props, StandardError.new('boom!'), @handler)
|
|
317
|
+
end
|
|
318
|
+
|
|
319
|
+
it 'should work and handle noops' do
|
|
320
|
+
worker.do_work(@header, @props, :wait, @handler)
|
|
321
|
+
end
|
|
322
|
+
|
|
323
|
+
# Since we encode in json, we want to make sure if the actual payload is
|
|
324
|
+
# json, then it's something you can get back out.
|
|
325
|
+
describe 'JSON payloads' do
|
|
326
|
+
let(:max_retries) { 1 }
|
|
327
|
+
|
|
328
|
+
it 'properly encodes the json payload' do
|
|
329
|
+
mock(@header).routing_key { '#' }
|
|
330
|
+
mock(channel).acknowledge(37, false)
|
|
331
|
+
@error_exchange.extend MockPublish
|
|
332
|
+
|
|
333
|
+
payload = {
|
|
334
|
+
data: 'hello',
|
|
335
|
+
response: :timeout
|
|
336
|
+
}
|
|
337
|
+
worker.do_work(@header, @props_with_x_death, payload.to_json, @handler)
|
|
338
|
+
@error_exchange.called.must_equal(true)
|
|
339
|
+
@error_exchange.opts.must_equal({ :routing_key => '#' })
|
|
340
|
+
data = JSON.parse(@error_exchange.data)
|
|
341
|
+
data['error'].must_equal('timeout')
|
|
342
|
+
data['num_attempts'].must_equal(2)
|
|
343
|
+
data['payload'].must_equal(Base64.encode64(payload.to_json))
|
|
344
|
+
end
|
|
345
|
+
|
|
346
|
+
end
|
|
347
|
+
|
|
348
|
+
end
|
|
349
|
+
end
|
|
350
|
+
end
|