vx-consumer 0.0.2 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/vx/consumer/ack.rb +4 -4
- data/lib/vx/consumer/session.rb +19 -5
- data/lib/vx/consumer/subscribe.rb +28 -15
- data/lib/vx/consumer/subscriber.rb +37 -12
- data/lib/vx/consumer/version.rb +1 -1
- data/lib/vx/consumer.rb +7 -3
- data/spec/lib/consumer_spec.rb +17 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 98dc0b2f078519ea2ccf52a5467cdfbebce98652
|
4
|
+
data.tar.gz: 405b389c6672e9ce777c44759e2b7422aa4d1555
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c880219d1bd3c939a1a0a217b36053447736ed054b7b05bb00f15c00ad23d9a88395260b407e54c7a44619b1ad7150ad0c4d4ec5231ea8517e95ba4749cb8a2c
|
7
|
+
data.tar.gz: ecff22e7ec25e566353fb6526dca1f967c6f3bae88d827d7efcb707abbfc2433db413166bb471c9dce42a9a0c6870b4a713e0bf3ae7fc6ffc19182eaea2076ae
|
data/lib/vx/consumer/ack.rb
CHANGED
@@ -8,8 +8,8 @@ module Vx
|
|
8
8
|
properties: properties,
|
9
9
|
multiple: multiple,
|
10
10
|
}
|
11
|
-
if
|
12
|
-
|
11
|
+
if _channel.open?
|
12
|
+
_channel.ack delivery_info.delivery_tag, multiple
|
13
13
|
instrument("ack", instrumentation)
|
14
14
|
true
|
15
15
|
else
|
@@ -25,8 +25,8 @@ module Vx
|
|
25
25
|
multiple: multiple,
|
26
26
|
requeue: requeue,
|
27
27
|
}
|
28
|
-
if
|
29
|
-
|
28
|
+
if _channel.open?
|
29
|
+
_channel.ack delivery_info.delivery_tag, multiple, requeue
|
30
30
|
instrument("nack", instrumentation)
|
31
31
|
true
|
32
32
|
else
|
data/lib/vx/consumer/session.rb
CHANGED
@@ -7,20 +7,34 @@ module Vx
|
|
7
7
|
|
8
8
|
include Instrument
|
9
9
|
|
10
|
-
@@session_lock
|
10
|
+
@@session_lock = Mutex.new
|
11
|
+
|
12
|
+
@@shutdown_lock = Mutex.new
|
13
|
+
@@shutdown = ConditionVariable.new
|
14
|
+
@@live = false
|
11
15
|
|
12
16
|
attr_reader :conn
|
13
17
|
|
14
18
|
def shutdown
|
15
|
-
|
19
|
+
@@shutdown_lock.synchronize do
|
20
|
+
@@live = false
|
21
|
+
@@shutdown.broadcast
|
22
|
+
end
|
16
23
|
end
|
17
24
|
|
18
|
-
def
|
19
|
-
|
25
|
+
def live?
|
26
|
+
@@live
|
20
27
|
end
|
21
28
|
|
22
29
|
def resume
|
23
|
-
|
30
|
+
@@live = true
|
31
|
+
end
|
32
|
+
|
33
|
+
def wait_shutdown(timeout = nil)
|
34
|
+
@@shutdown_lock.synchronize do
|
35
|
+
@@shutdown.wait(@@shutdown_lock, timeout)
|
36
|
+
not live?
|
37
|
+
end
|
24
38
|
end
|
25
39
|
|
26
40
|
def close
|
@@ -4,31 +4,44 @@ module Vx
|
|
4
4
|
|
5
5
|
def subscribe
|
6
6
|
ch, q = bind
|
7
|
-
bunny_consumer = q.subscribe(block: false, ack: params.ack) do |delivery_info, properties, payload|
|
8
|
-
payload = decode_payload properties, payload
|
9
7
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
8
|
+
subscriber = Subscriber.new(
|
9
|
+
ch,
|
10
|
+
q,
|
11
|
+
ch.generate_consumer_tag,
|
12
|
+
!params.ack
|
13
|
+
)
|
14
|
+
subscriber.vx_consumer_name = params.consumer_name
|
15
15
|
|
16
|
-
|
17
|
-
|
18
|
-
instrument("process", instrumentation) do
|
19
|
-
run_instance delivery_info, properties, payload, ch
|
20
|
-
end
|
21
|
-
end
|
16
|
+
subscriber.on_delivery do |delivery_info, properties, payload|
|
17
|
+
handle_delivery ch, delivery_info, properties, payload
|
22
18
|
end
|
23
19
|
|
24
|
-
|
20
|
+
q.subscribe_with(subscriber)
|
21
|
+
end
|
22
|
+
|
23
|
+
def handle_delivery(channel, delivery_info, properties, payload)
|
24
|
+
payload = decode_payload properties, payload
|
25
|
+
|
26
|
+
instrumentation = {
|
27
|
+
consumer: params.consumer_name,
|
28
|
+
payload: payload,
|
29
|
+
properties: properties,
|
30
|
+
}
|
31
|
+
|
32
|
+
with_middlewares :sub, instrumentation do
|
33
|
+
instrument("start_processing", instrumentation)
|
34
|
+
instrument("process", instrumentation) do
|
35
|
+
run_instance delivery_info, properties, payload, channel
|
36
|
+
end
|
37
|
+
end
|
25
38
|
end
|
26
39
|
|
27
40
|
def run_instance(delivery_info, properties, payload, channel)
|
28
41
|
new.tap do |inst|
|
29
42
|
inst.properties = properties
|
30
43
|
inst.delivery_info = delivery_info
|
31
|
-
inst.
|
44
|
+
inst._channel = channel
|
32
45
|
end.perform payload
|
33
46
|
end
|
34
47
|
|
@@ -1,24 +1,49 @@
|
|
1
|
+
require 'thread'
|
2
|
+
require 'bunny/consumer'
|
3
|
+
|
1
4
|
module Vx
|
2
5
|
module Consumer
|
3
|
-
Subscriber
|
6
|
+
class Subscriber < Bunny::Consumer
|
7
|
+
|
8
|
+
include Instrument
|
9
|
+
|
10
|
+
attr_accessor :vx_consumer_name
|
11
|
+
|
12
|
+
def initialize(*args)
|
13
|
+
super(*args)
|
14
|
+
@lock = Mutex.new
|
15
|
+
end
|
16
|
+
|
17
|
+
def graceful_shutdown
|
18
|
+
instrument('graceful_shutdown_consumer', consumer: vx_consumer_name)
|
19
|
+
in_progress { cancel }
|
20
|
+
end
|
21
|
+
|
22
|
+
def in_progress
|
23
|
+
@lock.synchronize do
|
24
|
+
yield
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def call(*args)
|
29
|
+
in_progress do
|
30
|
+
@on_delivery.call(*args) if @on_delivery
|
31
|
+
end
|
32
|
+
end
|
4
33
|
|
5
34
|
def cancel
|
6
|
-
consumer
|
7
|
-
|
35
|
+
instrument('cancel_consumer', consumer: vx_consumer_name)
|
36
|
+
super
|
37
|
+
channel.close unless channel.closed?
|
8
38
|
end
|
9
39
|
|
10
40
|
def join
|
11
|
-
|
41
|
+
channel.work_pool.join
|
12
42
|
end
|
13
43
|
|
14
|
-
def
|
15
|
-
|
16
|
-
|
17
|
-
cancel
|
18
|
-
break
|
19
|
-
end
|
20
|
-
sleep Consumer.configuration.pool_timeout
|
21
|
-
end
|
44
|
+
def wait_shutdown
|
45
|
+
Consumer.wait_shutdown
|
46
|
+
cancel
|
22
47
|
end
|
23
48
|
|
24
49
|
end
|
data/lib/vx/consumer/version.rb
CHANGED
data/lib/vx/consumer.rb
CHANGED
@@ -19,7 +19,7 @@ module Vx
|
|
19
19
|
|
20
20
|
attr_accessor :properties
|
21
21
|
attr_accessor :delivery_info
|
22
|
-
attr_accessor :
|
22
|
+
attr_accessor :_channel
|
23
23
|
|
24
24
|
def self.included(base)
|
25
25
|
base.extend ClassMethods
|
@@ -95,8 +95,12 @@ module Vx
|
|
95
95
|
session.shutdown
|
96
96
|
end
|
97
97
|
|
98
|
-
def
|
99
|
-
session.
|
98
|
+
def live?
|
99
|
+
session.live?
|
100
|
+
end
|
101
|
+
|
102
|
+
def wait_shutdown(timeout = nil)
|
103
|
+
session.wait_shutdown(timeout)
|
100
104
|
end
|
101
105
|
|
102
106
|
def configure
|
data/spec/lib/consumer_spec.rb
CHANGED
@@ -93,9 +93,9 @@ describe Vx::Consumer do
|
|
93
93
|
Bob.publish a: 1
|
94
94
|
|
95
95
|
th = Thread.new {
|
96
|
-
consumer.
|
96
|
+
consumer.wait_shutdown
|
97
97
|
}
|
98
|
-
sleep
|
98
|
+
sleep 0.2
|
99
99
|
Vx::Consumer.shutdown
|
100
100
|
|
101
101
|
Timeout.timeout(1) do
|
@@ -105,6 +105,21 @@ describe Vx::Consumer do
|
|
105
105
|
expect(Bob._collected).to eq(["a" => 1])
|
106
106
|
end
|
107
107
|
|
108
|
+
it "should work with graceful shutdown" do
|
109
|
+
consumer = Bob.subscribe
|
110
|
+
10.times do |n|
|
111
|
+
Bob.publish a: n
|
112
|
+
end
|
113
|
+
|
114
|
+
sleep 0.2
|
115
|
+
Timeout.timeout(1) do
|
116
|
+
consumer.graceful_shutdown
|
117
|
+
end
|
118
|
+
|
119
|
+
expect(Bob._collected).to have_at_least(2).item
|
120
|
+
expect(Bob._collected).to have_at_most(8).item
|
121
|
+
end
|
122
|
+
|
108
123
|
def handle_errors
|
109
124
|
begin
|
110
125
|
yield
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: vx-consumer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dmitry Galinsky
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-02-
|
11
|
+
date: 2014-02-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bunny
|