vx-lib-consumer 0.2.2 → 0.3.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/lib/consumer.rb +0 -3
- data/lib/vx/lib/consumer/ack.rb +0 -17
- data/lib/vx/lib/consumer/configuration.rb +1 -2
- data/lib/vx/lib/consumer/publish.rb +5 -7
- data/lib/vx/lib/consumer/rpc.rb +19 -31
- data/lib/vx/lib/consumer/session.rb +9 -21
- data/lib/vx/lib/consumer/subscribe.rb +5 -26
- data/lib/vx/lib/consumer/subscriber.rb +0 -6
- data/lib/vx/lib/consumer/version.rb +1 -1
- data/vx-lib-consumer.gemspec +1 -1
- metadata +2 -3
- data/lib/vx/lib/consumer/instrument.rb +0 -27
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6608092d8377c6b8f5a270f962489a5b334db63e
|
4
|
+
data.tar.gz: cb6ded459721df23d54fd4291c2ab9d46c0fd3d8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6b25277ae9e0d282e9792acbd81b8d5f5a4a68bf0d0eb257b87394edeaf99d5a9b3d9414c5e605a0e50026a8fff6cc557ed8ebe410adfd47f06845dbb2f87acf
|
7
|
+
data.tar.gz: b5bb94f0eeb0800d44f5cdfdfb746f0027f41eccbf9c27544468f308f2899e658f6d4cff1db949f6d8a79b122b286ac0ac2ab703d0cb6ee1ae7f7d861e89781f
|
data/lib/vx/lib/consumer.rb
CHANGED
@@ -2,7 +2,6 @@
|
|
2
2
|
version
|
3
3
|
error
|
4
4
|
configuration
|
5
|
-
instrument
|
6
5
|
session
|
7
6
|
params
|
8
7
|
serializer
|
@@ -25,12 +24,10 @@ module Vx
|
|
25
24
|
|
26
25
|
def self.included(base)
|
27
26
|
base.extend ClassMethods
|
28
|
-
base.extend Instrument
|
29
27
|
base.extend Publish
|
30
28
|
base.extend Subscribe
|
31
29
|
base.extend Rpc
|
32
30
|
base.send :include, Ack
|
33
|
-
base.send :include, Instrument
|
34
31
|
end
|
35
32
|
|
36
33
|
module ClassMethods
|
data/lib/vx/lib/consumer/ack.rb
CHANGED
@@ -4,36 +4,19 @@ module Vx
|
|
4
4
|
module Ack
|
5
5
|
|
6
6
|
def ack(multiple = false)
|
7
|
-
instrumentation = {
|
8
|
-
consumer: self.class.params.consumer_name,
|
9
|
-
properties: properties,
|
10
|
-
multiple: multiple,
|
11
|
-
channel: _channel.id
|
12
|
-
}
|
13
7
|
if _channel.open?
|
14
8
|
_channel.ack delivery_info.delivery_tag, multiple
|
15
|
-
instrument("ack", instrumentation)
|
16
9
|
true
|
17
10
|
else
|
18
|
-
instrument("ack_failed", instrumentation)
|
19
11
|
false
|
20
12
|
end
|
21
13
|
end
|
22
14
|
|
23
15
|
def nack(multiple = false, requeue = false)
|
24
|
-
instrumentation = {
|
25
|
-
consumer: self.class.params.consumer_name,
|
26
|
-
properties: properties,
|
27
|
-
multiple: multiple,
|
28
|
-
requeue: requeue,
|
29
|
-
channel: channel.id
|
30
|
-
}
|
31
16
|
if _channel.open?
|
32
17
|
_channel.ack delivery_info.delivery_tag, multiple, requeue
|
33
|
-
instrument("nack", instrumentation)
|
34
18
|
true
|
35
19
|
else
|
36
|
-
instrument("nack_failed", instrumentation)
|
37
20
|
false
|
38
21
|
end
|
39
22
|
end
|
@@ -10,7 +10,7 @@ module Vx
|
|
10
10
|
|
11
11
|
attr_accessor :default_exchange_options, :default_queue_options,
|
12
12
|
:default_publish_options, :default_exchange_type, :pool_timeout,
|
13
|
-
:heartbeat, :spawn_attempts, :content_type, :
|
13
|
+
:heartbeat, :spawn_attempts, :content_type, :debug,
|
14
14
|
:on_error, :builders, :prefetch
|
15
15
|
|
16
16
|
def initialize
|
@@ -40,7 +40,6 @@ module Vx
|
|
40
40
|
@content_type = 'application/json'
|
41
41
|
@prefetch = 1
|
42
42
|
|
43
|
-
@instrumenter = nil
|
44
43
|
@on_error = ->(e, env){ nil }
|
45
44
|
|
46
45
|
@builders = {
|
@@ -17,20 +17,18 @@ module Vx
|
|
17
17
|
|
18
18
|
name = params.exchange_name
|
19
19
|
|
20
|
-
|
20
|
+
env = {
|
21
21
|
payload: payload,
|
22
22
|
exchange: name,
|
23
23
|
consumer: params.consumer_name,
|
24
24
|
properties: options,
|
25
25
|
}
|
26
26
|
|
27
|
-
with_middlewares :pub,
|
27
|
+
with_middlewares :pub, env do
|
28
28
|
session.with_pub_channel do |ch|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
x.publish encoded, options
|
33
|
-
end
|
29
|
+
encoded = encode_payload(payload, options[:content_type])
|
30
|
+
x = session.declare_exchange ch, name, params.exchange_options
|
31
|
+
x.publish encoded, options
|
34
32
|
end
|
35
33
|
end
|
36
34
|
end
|
data/lib/vx/lib/consumer/rpc.rb
CHANGED
@@ -97,7 +97,7 @@ module Vx
|
|
97
97
|
payload = ::JSON.parse(payload)
|
98
98
|
end
|
99
99
|
|
100
|
-
|
100
|
+
env = {
|
101
101
|
consumer: consumer.params.consumer_name,
|
102
102
|
queue: @q.name,
|
103
103
|
rpc: REP,
|
@@ -106,18 +106,15 @@ module Vx
|
|
106
106
|
properties: properties
|
107
107
|
}
|
108
108
|
|
109
|
-
consumer.with_middlewares :sub,
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
@wakeup.synchronize do
|
119
|
-
c.signal
|
120
|
-
end
|
109
|
+
consumer.with_middlewares :sub, env do
|
110
|
+
call_id = properties[:correlation_id]
|
111
|
+
c = @mutex.synchronize{ @await.delete(call_id) }
|
112
|
+
if c
|
113
|
+
@mutex.synchronize do
|
114
|
+
@await[call_id] = [properties, payload]
|
115
|
+
end
|
116
|
+
@wakeup.synchronize do
|
117
|
+
c.signal
|
121
118
|
end
|
122
119
|
end
|
123
120
|
end
|
@@ -141,7 +138,7 @@ module Vx
|
|
141
138
|
consumer.session.with_pub_channel do |ch|
|
142
139
|
exch = ch.exchange RPC_EXCHANGE_NAME
|
143
140
|
|
144
|
-
|
141
|
+
env = {
|
145
142
|
payload: message,
|
146
143
|
rpc: REQ,
|
147
144
|
exchange: exch.name,
|
@@ -152,16 +149,14 @@ module Vx
|
|
152
149
|
|
153
150
|
@mutex.synchronize { @await[call_id] = cond }
|
154
151
|
|
155
|
-
consumer.with_middlewares :pub,
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
)
|
164
|
-
end
|
152
|
+
consumer.with_middlewares :pub, env do
|
153
|
+
exch.publish(
|
154
|
+
message.to_json,
|
155
|
+
routing_key: routing_key,
|
156
|
+
correlation_id: call_id,
|
157
|
+
reply_to: q.name,
|
158
|
+
content_type: JSON_CONTENT_TYPE
|
159
|
+
)
|
165
160
|
end
|
166
161
|
|
167
162
|
@wakeup.synchronize{
|
@@ -192,13 +187,6 @@ module Vx
|
|
192
187
|
end
|
193
188
|
|
194
189
|
def cancel
|
195
|
-
instrumentation = {
|
196
|
-
consumer: consumer.params.consumer_name,
|
197
|
-
rpc: 'consume'
|
198
|
-
}
|
199
|
-
|
200
|
-
consumer.instrument('cancel_consumer', instrumentation)
|
201
|
-
|
202
190
|
if subscriber?
|
203
191
|
@subscriber.cancel
|
204
192
|
@subscriber = nil
|
@@ -6,8 +6,6 @@ module Vx
|
|
6
6
|
module Consumer
|
7
7
|
class Session
|
8
8
|
|
9
|
-
include Instrument
|
10
|
-
|
11
9
|
@@session_lock = Mutex.new
|
12
10
|
|
13
11
|
@@shutdown_lock = Mutex.new
|
@@ -41,17 +39,13 @@ module Vx
|
|
41
39
|
def close
|
42
40
|
if open?
|
43
41
|
@@session_lock.synchronize do
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
conn.close
|
49
|
-
while conn.status != :closed
|
50
|
-
sleep 0.01
|
51
|
-
end
|
52
|
-
rescue Bunny::ChannelError, Bunny::ClientTimeout => e
|
53
|
-
Consumer.exception_handler(e, {})
|
42
|
+
begin
|
43
|
+
conn.close
|
44
|
+
while conn.status != :closed
|
45
|
+
sleep 0.01
|
54
46
|
end
|
47
|
+
rescue Bunny::ChannelError, Bunny::ClientTimeout => e
|
48
|
+
Consumer.exception_handler(e, {})
|
55
49
|
end
|
56
50
|
@conn = nil
|
57
51
|
end
|
@@ -71,15 +65,9 @@ module Vx
|
|
71
65
|
automatically_recover: false
|
72
66
|
)
|
73
67
|
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
instrument("connect", instrumentation) do
|
79
|
-
conn.start
|
80
|
-
while conn.connecting?
|
81
|
-
sleep 0.01
|
82
|
-
end
|
68
|
+
conn.start
|
69
|
+
while conn.connecting?
|
70
|
+
sleep 0.01
|
83
71
|
end
|
84
72
|
end
|
85
73
|
end
|
@@ -25,19 +25,16 @@ module Vx
|
|
25
25
|
def handle_delivery(channel, delivery_info, properties, payload)
|
26
26
|
payload = decode_payload properties, payload
|
27
27
|
|
28
|
-
|
28
|
+
env = {
|
29
29
|
consumer: params.consumer_name,
|
30
30
|
payload: payload,
|
31
31
|
properties: properties,
|
32
32
|
channel: channel.id
|
33
33
|
}
|
34
34
|
|
35
|
-
with_middlewares :sub,
|
36
|
-
|
37
|
-
|
38
|
-
allocate_pub_channel do
|
39
|
-
run_instance delivery_info, properties, payload, channel
|
40
|
-
end
|
35
|
+
with_middlewares :sub, env do
|
36
|
+
allocate_pub_channel do
|
37
|
+
run_instance delivery_info, properties, payload, channel
|
41
38
|
end
|
42
39
|
end
|
43
40
|
end
|
@@ -59,10 +56,6 @@ module Vx
|
|
59
56
|
def bind(options = {})
|
60
57
|
qname = options[:queue] || params.queue_name
|
61
58
|
|
62
|
-
instrumentation = {
|
63
|
-
consumer: params.consumer_name
|
64
|
-
}
|
65
|
-
|
66
59
|
session.open
|
67
60
|
|
68
61
|
ch = session.conn.create_channel
|
@@ -78,22 +71,8 @@ module Vx
|
|
78
71
|
|
79
72
|
q = session.declare_queue ch, qname, params.queue_options
|
80
73
|
|
81
|
-
instrumentation.merge!(
|
82
|
-
exchange: x_name,
|
83
|
-
queue: q.name,
|
84
|
-
queue_options: params.queue_options,
|
85
|
-
)
|
86
|
-
|
87
74
|
if x_name != ''
|
88
|
-
|
89
|
-
exchange_options: params.exchange_options,
|
90
|
-
bind: params.bind_options
|
91
|
-
)
|
92
|
-
instrument("bind_queue", instrumentation) do
|
93
|
-
q.bind(x, params.bind_options)
|
94
|
-
end
|
95
|
-
else
|
96
|
-
instrument("using_default_exchange", instrumentation)
|
75
|
+
q.bind(x, params.bind_options)
|
97
76
|
end
|
98
77
|
|
99
78
|
[ch, q]
|
@@ -6,8 +6,6 @@ module Vx
|
|
6
6
|
module Consumer
|
7
7
|
class Subscriber < Bunny::Consumer
|
8
8
|
|
9
|
-
include Instrument
|
10
|
-
|
11
9
|
attr_accessor :vx_consumer_name, :queue_name
|
12
10
|
|
13
11
|
def initialize(*args)
|
@@ -16,17 +14,14 @@ module Vx
|
|
16
14
|
end
|
17
15
|
|
18
16
|
def graceful_shutdown
|
19
|
-
instrument('try_graceful_shutdown_consumer', consumer: vx_consumer_name)
|
20
17
|
in_progress do
|
21
18
|
cancel
|
22
|
-
instrument('graceful_shutdown_consumer', consumer: vx_consumer_name)
|
23
19
|
end
|
24
20
|
end
|
25
21
|
|
26
22
|
def try_graceful_shutdown
|
27
23
|
if @lock.try_lock
|
28
24
|
begin
|
29
|
-
instrument('graceful_shutdown_consumer', consumer: vx_consumer_name)
|
30
25
|
cancel
|
31
26
|
ensure
|
32
27
|
@lock.unlock
|
@@ -55,7 +50,6 @@ module Vx
|
|
55
50
|
end
|
56
51
|
|
57
52
|
def cancel
|
58
|
-
instrument('cancel_consumer', consumer: vx_consumer_name, channel: channel.id)
|
59
53
|
unless closed?
|
60
54
|
super
|
61
55
|
channel.close unless closed?
|
data/vx-lib-consumer.gemspec
CHANGED
@@ -18,7 +18,7 @@ Gem::Specification.new do |spec|
|
|
18
18
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
19
|
spec.require_paths = ["lib"]
|
20
20
|
|
21
|
-
spec.add_runtime_dependency 'bunny',
|
21
|
+
spec.add_runtime_dependency 'bunny', '= 1.6.3'
|
22
22
|
spec.add_runtime_dependency 'vx-lib-rack-builder', '>= 0.0.2'
|
23
23
|
|
24
24
|
spec.add_development_dependency "bundler", "~> 1.5"
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: vx-lib-consumer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.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-12-
|
11
|
+
date: 2014-12-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bunny
|
@@ -98,7 +98,6 @@ files:
|
|
98
98
|
- lib/vx/lib/consumer/ack.rb
|
99
99
|
- lib/vx/lib/consumer/configuration.rb
|
100
100
|
- lib/vx/lib/consumer/error.rb
|
101
|
-
- lib/vx/lib/consumer/instrument.rb
|
102
101
|
- lib/vx/lib/consumer/params.rb
|
103
102
|
- lib/vx/lib/consumer/publish.rb
|
104
103
|
- lib/vx/lib/consumer/rpc.rb
|
@@ -1,27 +0,0 @@
|
|
1
|
-
module Vx
|
2
|
-
module Lib
|
3
|
-
module Consumer
|
4
|
-
module Instrument
|
5
|
-
|
6
|
-
def instrument(name, payload, &block)
|
7
|
-
name = "#{name}.consumer.vx".freeze
|
8
|
-
|
9
|
-
if Consumer.configuration.debug?
|
10
|
-
$stdout.puts " --> #{name}: #{payload}"
|
11
|
-
end
|
12
|
-
|
13
|
-
if Consumer.configuration.instrumenter
|
14
|
-
Consumer.configuration.instrumenter.instrument(name, payload, &block)
|
15
|
-
else
|
16
|
-
begin
|
17
|
-
yield if block_given?
|
18
|
-
rescue Exception => e
|
19
|
-
Consumer.handle_exception(e, {})
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|