vx-common-amqp 0.2.8 → 0.3.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/lib/vx/common/amqp.rb +13 -10
- data/lib/vx/common/amqp/config.rb +6 -13
- data/lib/vx/common/amqp/consumer.rb +3 -3
- data/lib/vx/common/amqp/consumer/ack.rb +15 -2
- data/lib/vx/common/amqp/consumer/publish.rb +8 -2
- data/lib/vx/common/amqp/consumer/subscribe.rb +43 -25
- data/lib/vx/common/amqp/mixins/instrument.rb +13 -0
- data/lib/vx/common/amqp/session.rb +14 -12
- data/lib/vx/common/amqp/supervisor/threaded.rb +10 -5
- data/lib/vx/common/amqp/version.rb +1 -1
- data/spec/lib/amqp/config_spec.rb +0 -26
- data/spec/spec_helper.rb +2 -0
- data/spec/support/amqp.rb +2 -2
- metadata +4 -6
- data/lib/vx/common/amqp/mixins/callbacks.rb +0 -35
- data/lib/vx/common/amqp/mixins/logger.rb +0 -17
- data/spec/lib/amqp/mixins/callbacks_spec.rb +0 -26
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 85cc179ec72270468903a0aa0de0ea77aa2b11fa
|
4
|
+
data.tar.gz: 3bb45885f9845f638981c9aa91b23e0b4c176fe4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9154ed670fa2c0ff153d4ad05dc94fa0cb862ef32b285ad234c1d3b389005f90330072ca35ef13becedcbece50c8762fd4dac9ffe93996f7a3193280a8a52b72
|
7
|
+
data.tar.gz: f92e3d96b49d225f2fe20cddcd8d6b94c571a357cbf03bc3f76329237d84ee868fe3be3ee7c04d750e5d51a895cab4588d344f4d3a9d6c1a2663054194c4271d
|
data/lib/vx/common/amqp.rb
CHANGED
@@ -14,8 +14,7 @@ module Vx
|
|
14
14
|
autoload :Threaded, File.expand_path("../amqp/supervisor/threaded", __FILE__)
|
15
15
|
end
|
16
16
|
|
17
|
-
autoload :
|
18
|
-
autoload :Callbacks, File.expand_path("../amqp/mixins/callbacks", __FILE__)
|
17
|
+
autoload :Instrument, File.expand_path("../amqp/mixins/instrument", __FILE__)
|
19
18
|
|
20
19
|
extend self
|
21
20
|
|
@@ -46,14 +45,6 @@ module Vx
|
|
46
45
|
session.close
|
47
46
|
end
|
48
47
|
|
49
|
-
def logger
|
50
|
-
config.logger
|
51
|
-
end
|
52
|
-
|
53
|
-
def logger=(val)
|
54
|
-
config.logger = val
|
55
|
-
end
|
56
|
-
|
57
48
|
def shutdown
|
58
49
|
Common::AMQP::Session.shutdown
|
59
50
|
Vx::Common::AMQP::Supervisor::Threaded.shutdown
|
@@ -63,6 +54,18 @@ module Vx
|
|
63
54
|
Common::AMQP::Session.shutdown?
|
64
55
|
end
|
65
56
|
|
57
|
+
def instrument(name, payload, &block)
|
58
|
+
if config.debug?
|
59
|
+
STDOUT.puts "name: #{name} payload: #{payload.inspect}"
|
60
|
+
end
|
61
|
+
|
62
|
+
if config && config.instrumenter
|
63
|
+
config.instrumenter.instrument(name, payload, &block)
|
64
|
+
else
|
65
|
+
yield if block_given?
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
66
69
|
end
|
67
70
|
end
|
68
71
|
end
|
@@ -8,7 +8,7 @@ module Vx
|
|
8
8
|
|
9
9
|
attr_accessor :url, :default_exchange_options, :default_queue_options,
|
10
10
|
:default_publish_options, :default_exchange_type, :logger, :pool_timeout,
|
11
|
-
:heartbeat, :spawn_attempts, :content_type, :
|
11
|
+
:heartbeat, :spawn_attempts, :content_type, :instrumenter, :debug
|
12
12
|
|
13
13
|
def initialize
|
14
14
|
reset!
|
@@ -18,22 +18,14 @@ module Vx
|
|
18
18
|
Common::AMQP::Formatter
|
19
19
|
end
|
20
20
|
|
21
|
-
%w{ before after }.each do |p|
|
22
|
-
%w{ subscribe publish recieve }.each do |m|
|
23
|
-
define_method "#{p}_#{m}" do |&callback|
|
24
|
-
callbacks["#{p}_#{m}".to_sym] = callback
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
def on_error(&block)
|
30
|
-
callbacks[:on_error] = block
|
31
|
-
end
|
32
|
-
|
33
21
|
def default_exchange_name
|
34
22
|
"amq.#{default_exchange_type}"
|
35
23
|
end
|
36
24
|
|
25
|
+
def debug?
|
26
|
+
@debug || ENV['VX_COMMON_AMQP_DEBUG']
|
27
|
+
end
|
28
|
+
|
37
29
|
def reset!
|
38
30
|
@url = nil
|
39
31
|
@logger = ::Logger.new(STDOUT)
|
@@ -50,6 +42,7 @@ module Vx
|
|
50
42
|
@content_type = 'application/json'
|
51
43
|
|
52
44
|
@callbacks = {}
|
45
|
+
@instrumenter = nil
|
53
46
|
|
54
47
|
@default_exchange_options = {
|
55
48
|
durable: true,
|
@@ -11,12 +11,13 @@ module Vx
|
|
11
11
|
autoload :Sleep, File.expand_path("../consumer/sleep", __FILE__)
|
12
12
|
autoload :Ack, File.expand_path("../consumer/ack", __FILE__)
|
13
13
|
|
14
|
+
include Common::AMQP::Instrument
|
14
15
|
include Common::AMQP::Consumer::Ack
|
15
|
-
include Common::AMQP::Logger
|
16
16
|
|
17
17
|
attr_accessor :delivery_info
|
18
18
|
attr_accessor :properties
|
19
19
|
attr_accessor :channel
|
20
|
+
attr_accessor :uuid
|
20
21
|
|
21
22
|
@@classes = []
|
22
23
|
|
@@ -34,8 +35,7 @@ module Vx
|
|
34
35
|
include Common::AMQP::Consumer::Configuration
|
35
36
|
include Common::AMQP::Consumer::Publish
|
36
37
|
include Common::AMQP::Consumer::Subscribe
|
37
|
-
include Common::AMQP::
|
38
|
-
include Common::AMQP::Callbacks
|
38
|
+
include Common::AMQP::Instrument
|
39
39
|
|
40
40
|
def shutdown?
|
41
41
|
Common::AMQP.shutdown?
|
@@ -4,13 +4,26 @@ module Vx
|
|
4
4
|
module Consumer::Ack
|
5
5
|
|
6
6
|
def ack!(multiple = false)
|
7
|
+
instrumentation = {
|
8
|
+
consumer_id: self.class.consumer_id,
|
9
|
+
consumer: self.class.consumer_name,
|
10
|
+
multiple: multiple,
|
11
|
+
uuid: uuid,
|
12
|
+
}
|
7
13
|
self.class.session.channel.ack delivery_info.delivery_tag, multiple
|
8
|
-
|
14
|
+
instrument("ack.consumer.amqp", instrumentation)
|
9
15
|
end
|
10
16
|
|
11
17
|
def nack!(multiple = false, requeue = false)
|
18
|
+
instrumentation = {
|
19
|
+
consumer_id: self.class.consumer_id,
|
20
|
+
consumer: self.class.consumer_name,
|
21
|
+
multiple: multiple,
|
22
|
+
requeue: requeue,
|
23
|
+
uuid: uuid,
|
24
|
+
}
|
12
25
|
self.class.session.channel.ack delivery_info.delivery_tag, multiple, requeue
|
13
|
-
|
26
|
+
instrument("nack.consumer.amqp", instrumentation)
|
14
27
|
end
|
15
28
|
|
16
29
|
end
|
@@ -13,12 +13,18 @@ module Vx
|
|
13
13
|
|
14
14
|
x = declare_exchange
|
15
15
|
|
16
|
-
|
16
|
+
instrumentation = {
|
17
|
+
message: message.inspect,
|
18
|
+
exchange: x.name,
|
19
|
+
consumer: consumer_id
|
20
|
+
}
|
21
|
+
|
22
|
+
instrument("start_publishing.consumer.amqp", instrumentation)
|
23
|
+
instrument("process_publishing.consumer.amqp", instrumentation) do
|
17
24
|
m = serialize_message message, options[:content_type]
|
18
25
|
x.publish m, options
|
19
26
|
end
|
20
27
|
|
21
|
-
debug "published #{message.inspect} to #{x.name}"
|
22
28
|
self
|
23
29
|
end
|
24
30
|
|
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'securerandom'
|
2
|
+
|
1
3
|
module Vx
|
2
4
|
module Common
|
3
5
|
module AMQP
|
@@ -16,15 +18,13 @@ module Vx
|
|
16
18
|
x = declare_exchange
|
17
19
|
q = declare_queue
|
18
20
|
|
19
|
-
|
20
|
-
|
21
|
-
q.bind(x, bind_options)
|
22
|
-
debug "successfuly subscribed to #{q.name}:#{x.name}"
|
21
|
+
instrumentation = { exchange: x.name, queue: q.name, consumer: consumer_id }
|
22
|
+
instrument("start.consumer.amqp", instrumentation)
|
23
23
|
|
24
|
-
|
25
|
-
|
24
|
+
q.bind(x, bind_options)
|
25
|
+
rs = yield(x, q) if block_given?
|
26
26
|
|
27
|
-
|
27
|
+
instrument("shutdown.consumer.amqp", instrumentation)
|
28
28
|
end
|
29
29
|
rs
|
30
30
|
end
|
@@ -33,9 +33,16 @@ module Vx
|
|
33
33
|
unpacked = nil
|
34
34
|
delivery_info, properties, payload = q.pop(ack: ack)
|
35
35
|
|
36
|
+
instrumentation = {
|
37
|
+
properties: properties,
|
38
|
+
consumer_id: consumer_id,
|
39
|
+
consumer: consumer_name
|
40
|
+
}
|
41
|
+
|
36
42
|
if payload
|
37
|
-
|
38
|
-
|
43
|
+
instrument("unpacking.consumer.amqp", instrumentation) do
|
44
|
+
unpacked = deserialize_message properties, payload
|
45
|
+
end
|
39
46
|
end
|
40
47
|
|
41
48
|
[unpacked, delivery_info, properties]
|
@@ -51,10 +58,22 @@ module Vx
|
|
51
58
|
|
52
59
|
if payload
|
53
60
|
result = nil
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
61
|
+
uuid = SecureRandom.uuid
|
62
|
+
|
63
|
+
instrumentation = {
|
64
|
+
properties: properties,
|
65
|
+
consumer_id: consumer_id,
|
66
|
+
consumer: consumer_name,
|
67
|
+
uuid: uuid
|
68
|
+
}
|
69
|
+
|
70
|
+
instrument("start_processing.consumer.amqp", instrumentation)
|
71
|
+
instrument("process.consumer.amqp", instrumentation) do
|
72
|
+
instrument("unpacking.consumer.amqp", instrumentation) do
|
73
|
+
payload = deserialize_message properties, payload
|
74
|
+
end
|
75
|
+
result = run_instance delivery_info, properties, payload, uuid
|
76
|
+
end
|
58
77
|
|
59
78
|
break if result == :shutdown
|
60
79
|
else
|
@@ -63,21 +82,20 @@ module Vx
|
|
63
82
|
end
|
64
83
|
end
|
65
84
|
|
66
|
-
def run_instance(delivery_info, properties, payload)
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
inst.delivery_info = delivery_info
|
73
|
-
end.perform payload
|
74
|
-
end
|
85
|
+
def run_instance(delivery_info, properties, payload, uuid)
|
86
|
+
new.tap do |inst|
|
87
|
+
inst.properties = properties
|
88
|
+
inst.delivery_info = delivery_info
|
89
|
+
inst.uuid = uuid
|
90
|
+
end.perform payload
|
75
91
|
end
|
76
92
|
|
77
93
|
def deserialize_message(properties, payload)
|
78
|
-
Common::AMQP::Formatter.unpack
|
79
|
-
|
80
|
-
|
94
|
+
Common::AMQP::Formatter.unpack(
|
95
|
+
properties[:content_type],
|
96
|
+
model,
|
97
|
+
payload
|
98
|
+
)
|
81
99
|
end
|
82
100
|
|
83
101
|
end
|
@@ -6,7 +6,7 @@ module Vx
|
|
6
6
|
module AMQP
|
7
7
|
class Session
|
8
8
|
|
9
|
-
include Common::AMQP::
|
9
|
+
include Common::AMQP::Instrument
|
10
10
|
|
11
11
|
CHANNEL_KEY = :vx_amqp_channel
|
12
12
|
|
@@ -31,18 +31,15 @@ module Vx
|
|
31
31
|
def close
|
32
32
|
if open?
|
33
33
|
@@session_lock.synchronize do
|
34
|
-
info "closing connection"
|
35
34
|
begin
|
36
35
|
conn.close
|
37
36
|
rescue Bunny::ChannelError => e
|
38
37
|
warn e
|
39
38
|
end
|
40
|
-
info "wait..."
|
41
39
|
while conn.status != :closed
|
42
40
|
sleep 0.01
|
43
41
|
end
|
44
42
|
@conn = nil
|
45
|
-
info "connection closed"
|
46
43
|
end
|
47
44
|
end
|
48
45
|
end
|
@@ -55,14 +52,19 @@ module Vx
|
|
55
52
|
|
56
53
|
@conn ||= Bunny.new config.url, heartbeat: :server
|
57
54
|
|
58
|
-
|
59
|
-
info
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
55
|
+
instrumentation = {
|
56
|
+
info: conn_info
|
57
|
+
}
|
58
|
+
|
59
|
+
instrument("start_connecting.consumer.amqp", instrumentation)
|
60
|
+
|
61
|
+
instrument("connect.consumer.amqp", instrumentation) do
|
62
|
+
unless conn.open?
|
63
|
+
conn.start
|
64
|
+
while conn.connecting?
|
65
|
+
sleep 0.01
|
66
|
+
end
|
64
67
|
end
|
65
|
-
info "connected successfuly (#{server_name})"
|
66
68
|
end
|
67
69
|
end
|
68
70
|
|
@@ -114,7 +116,7 @@ module Vx
|
|
114
116
|
|
115
117
|
def conn_info
|
116
118
|
if conn
|
117
|
-
"
|
119
|
+
"amqp://#{conn.user}@#{conn.host}:#{conn.port}/#{conn.vhost}"
|
118
120
|
end
|
119
121
|
end
|
120
122
|
|
@@ -5,8 +5,7 @@ module Vx
|
|
5
5
|
module AMQP
|
6
6
|
class Supervisor::Threaded
|
7
7
|
|
8
|
-
include Common::AMQP::
|
9
|
-
include Common::AMQP::Callbacks
|
8
|
+
include Common::AMQP::Instrument
|
10
9
|
|
11
10
|
POOL_INTERVAL = 0.5
|
12
11
|
|
@@ -136,7 +135,8 @@ module Vx
|
|
136
135
|
new_task.attempt = attempt
|
137
136
|
new_task.start_at = Time.now
|
138
137
|
new_task.freeze
|
139
|
-
|
138
|
+
instrument("spawn.consumer.ampq", task: new_task)
|
139
|
+
new_task
|
140
140
|
end
|
141
141
|
end
|
142
142
|
|
@@ -148,8 +148,13 @@ module Vx
|
|
148
148
|
nil
|
149
149
|
rescue Exception => e
|
150
150
|
STDERR.puts "#{e.inspect} in #{task.inspect}"
|
151
|
-
STDERR.puts e.backtrace.join("\n")
|
152
|
-
|
151
|
+
STDERR.puts e.backtrace.map{|b| " #{b}" }.join("\n")
|
152
|
+
|
153
|
+
instrument("exception.consumer.amqp", payload: {
|
154
|
+
exception: e,
|
155
|
+
task: task.inspect
|
156
|
+
})
|
157
|
+
|
153
158
|
e
|
154
159
|
end
|
155
160
|
end
|
@@ -3,30 +3,4 @@ require 'spec_helper'
|
|
3
3
|
describe Vx::Common::AMQP::Config do
|
4
4
|
let(:config) { described_class.new }
|
5
5
|
|
6
|
-
context '(callbacks)' do
|
7
|
-
%w{ before after }.each do |p|
|
8
|
-
%w{ subscribe recieve publish }.each do |m|
|
9
|
-
name = "#{p}_#{m}"
|
10
|
-
it name do
|
11
|
-
config.public_send name do |value|
|
12
|
-
value
|
13
|
-
end
|
14
|
-
|
15
|
-
val = config.callbacks[name.to_sym].call("value")
|
16
|
-
expect(val).to eq 'value'
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
context "on_error" do
|
22
|
-
it "should be success" do
|
23
|
-
config.on_error do |e|
|
24
|
-
e
|
25
|
-
end
|
26
|
-
val = config.callbacks[:on_error].call "value"
|
27
|
-
expect(val).to eq 'value'
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
6
|
end
|
data/spec/spec_helper.rb
CHANGED
data/spec/support/amqp.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
def delete_queue(q)
|
2
|
-
|
2
|
+
STDOUT.puts "[AMQP] delete queue #{q.inspect[0..30]}"
|
3
3
|
if q
|
4
4
|
q.purge
|
5
5
|
q.delete if_unused: false, if_empty: false
|
@@ -8,7 +8,7 @@ def delete_queue(q)
|
|
8
8
|
end
|
9
9
|
|
10
10
|
def delete_exchange(x)
|
11
|
-
|
11
|
+
STDOUT.puts "[AMQP] delete exchnage #{x.inspect[0..30]}"
|
12
12
|
x.delete if x
|
13
13
|
true
|
14
14
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: vx-common-amqp
|
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-01-
|
11
|
+
date: 2014-01-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bunny
|
@@ -119,8 +119,7 @@ files:
|
|
119
119
|
- lib/vx/common/amqp/consumer/publish.rb
|
120
120
|
- lib/vx/common/amqp/consumer/subscribe.rb
|
121
121
|
- lib/vx/common/amqp/formatter.rb
|
122
|
-
- lib/vx/common/amqp/mixins/
|
123
|
-
- lib/vx/common/amqp/mixins/logger.rb
|
122
|
+
- lib/vx/common/amqp/mixins/instrument.rb
|
124
123
|
- lib/vx/common/amqp/session.rb
|
125
124
|
- lib/vx/common/amqp/supervisor/threaded.rb
|
126
125
|
- lib/vx/common/amqp/testing.rb
|
@@ -130,7 +129,6 @@ files:
|
|
130
129
|
- spec/lib/amqp/config_spec.rb
|
131
130
|
- spec/lib/amqp/consumer_spec.rb
|
132
131
|
- spec/lib/amqp/formatter_spec.rb
|
133
|
-
- spec/lib/amqp/mixins/callbacks_spec.rb
|
134
132
|
- spec/lib/amqp/session_spec.rb
|
135
133
|
- spec/lib/amqp/supervisor/threaded_spec.rb
|
136
134
|
- spec/lib/amqp_spec.rb
|
@@ -168,10 +166,10 @@ test_files:
|
|
168
166
|
- spec/lib/amqp/config_spec.rb
|
169
167
|
- spec/lib/amqp/consumer_spec.rb
|
170
168
|
- spec/lib/amqp/formatter_spec.rb
|
171
|
-
- spec/lib/amqp/mixins/callbacks_spec.rb
|
172
169
|
- spec/lib/amqp/session_spec.rb
|
173
170
|
- spec/lib/amqp/supervisor/threaded_spec.rb
|
174
171
|
- spec/lib/amqp_spec.rb
|
175
172
|
- spec/spec_helper.rb
|
176
173
|
- spec/support/amqp.rb
|
177
174
|
- spec/support/ignore_me_error.rb
|
175
|
+
has_rdoc:
|
@@ -1,35 +0,0 @@
|
|
1
|
-
module Vx
|
2
|
-
module Common
|
3
|
-
module AMQP
|
4
|
-
module Callbacks
|
5
|
-
|
6
|
-
def run_callbacks(name, *args)
|
7
|
-
before = "before_#{name}".to_sym
|
8
|
-
after = "after_#{name}".to_sym
|
9
|
-
if f = Common::AMQP.config.callbacks[before]
|
10
|
-
f.call(*args)
|
11
|
-
end
|
12
|
-
|
13
|
-
rs = yield if block_given?
|
14
|
-
|
15
|
-
if f = Common::AMQP.config.callbacks[after]
|
16
|
-
f.call(*args)
|
17
|
-
end
|
18
|
-
|
19
|
-
rs
|
20
|
-
end
|
21
|
-
|
22
|
-
def run_on_error_callback(e)
|
23
|
-
if f = Common::AMQP.config.callbacks[:on_error]
|
24
|
-
begin
|
25
|
-
f.call e
|
26
|
-
rescue Exception => e
|
27
|
-
$stderr.puts "ERROR on error callback: #{e.inspect}"
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
@@ -1,26 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Vx::Common::AMQP::Callbacks do
|
4
|
-
let(:output) { [] }
|
5
|
-
let(:object) { Object.new.extend described_class }
|
6
|
-
|
7
|
-
before do
|
8
|
-
Vx::Common::AMQP.config.reset!
|
9
|
-
Vx::Common::AMQP.configure do |c|
|
10
|
-
c.before_publish { |v| output << "before:#{v}" }
|
11
|
-
c.after_publish { |v| output << "after:#{v}" }
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
after { Vx::Common::AMQP.config.reset! }
|
16
|
-
|
17
|
-
context 'run_callbacks' do
|
18
|
-
it "should be success" do
|
19
|
-
object.run_callbacks :publish, "call" do
|
20
|
-
output << "call"
|
21
|
-
end
|
22
|
-
expect(output).to eq %w{ before:call call after:call }
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|