carnivore-rabbitmq 0.2.2 → 0.2.4
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/CHANGELOG.md +4 -0
- data/lib/carnivore-rabbitmq/connection.rb +134 -0
- data/lib/carnivore-rabbitmq/rabbitmq.rb +22 -71
- data/lib/carnivore-rabbitmq/version.rb +1 -1
- metadata +3 -3
- data/lib/carnivore-rabbitmq/message_collector.rb +0 -70
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cd27cd6d1b8b40b91acb44542e25092e30fb43d0
|
4
|
+
data.tar.gz: 714920300aee91b2abe11fbf31a9de62d6f30766
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: af4c659d59915ea6ff1a338dd110b74c6ba4a2dd49de9f641623fe9811ae82c1a526effcc3cce03ece0bf88f172c9855e8cf2b0e505195b1c545686567598688
|
7
|
+
data.tar.gz: b04f5248ee265729faaf9b0f1ac60b7b8835d8acc1c3f0e9a4d9b55de7291decee9c052e632d2f5275095c8a4a58ad26da1292ef921a727ffb59ea250cc2843a
|
data/CHANGELOG.md
CHANGED
@@ -0,0 +1,134 @@
|
|
1
|
+
require 'carnivore-rabbitmq'
|
2
|
+
|
3
|
+
module Carnivore
|
4
|
+
class Source
|
5
|
+
class Rabbitmq
|
6
|
+
# RabbitMQ connection
|
7
|
+
class Connection
|
8
|
+
|
9
|
+
include Zoidberg::SoftShell
|
10
|
+
include Zoidberg::Supervise
|
11
|
+
include Carnivore::Utils::Logging
|
12
|
+
|
13
|
+
# @return [Hash]
|
14
|
+
attr_reader :source_args
|
15
|
+
# @return [String]
|
16
|
+
attr_reader :routing_key
|
17
|
+
# @return [Bunny::Connection, MarchHare::Connection]
|
18
|
+
attr_reader :connection
|
19
|
+
# @return [Bunny::Channel, MarchHare::Channel]
|
20
|
+
attr_reader :channel
|
21
|
+
# @return [Bunny::Exchange, MarchHare::Exchange]
|
22
|
+
attr_reader :exchange
|
23
|
+
# @return [Bunny::Queue, MarchHare::Queue]
|
24
|
+
attr_reader :queue
|
25
|
+
# @return [Carnivore::Source::Rabbitmq]
|
26
|
+
attr_reader :source
|
27
|
+
# @return [Queue]
|
28
|
+
attr_reader :message_queue
|
29
|
+
# @return [Signal]
|
30
|
+
attr_reader :source_signal
|
31
|
+
|
32
|
+
# Create new connection
|
33
|
+
#
|
34
|
+
# @param r_source [Carnivore::Source::Rabbitmq] origin
|
35
|
+
# @param m_queue [Queue] common message queue
|
36
|
+
# @return [self]
|
37
|
+
def initialize(r_source, m_queue)
|
38
|
+
@source = r_source
|
39
|
+
@source_args = r_source.args.to_smash
|
40
|
+
@message_queue = m_queue
|
41
|
+
connect
|
42
|
+
end
|
43
|
+
|
44
|
+
# Restart trigger for supervised replacements
|
45
|
+
def restarted
|
46
|
+
connect
|
47
|
+
if(source.collect_messages)
|
48
|
+
async.receive_messages
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
# Establish connection to remote server and setup
|
53
|
+
#
|
54
|
+
# @return [MarchHare::Session, Bunny::Session]
|
55
|
+
def connect
|
56
|
+
unless(source_args[:connection])
|
57
|
+
abort KeyError.new "No configuration defined for connection type (#{connection_library})"
|
58
|
+
end
|
59
|
+
connection_args = Carnivore::Utils.symbolize_hash(source_args[:connection])
|
60
|
+
case connection_library
|
61
|
+
when :bunny
|
62
|
+
require 'bunny'
|
63
|
+
@connection = Bunny.new(connection_args)
|
64
|
+
when :march_hare
|
65
|
+
require 'march_hare'
|
66
|
+
@connection = MarchHare.connect(connection_args)
|
67
|
+
else
|
68
|
+
abort ArgumentError.new("No valid connection arguments defined (:bunny or :march_hare must be defined)")
|
69
|
+
end
|
70
|
+
connection.start
|
71
|
+
@routing_key = source_args[:routing_key]
|
72
|
+
@channel = connection.create_channel
|
73
|
+
@exchange = channel.topic(source_args[:exchange])
|
74
|
+
@queue = channel.queue(source_args[:queue], :auto_delete => false).
|
75
|
+
bind(exchange, :routing_key => routing_key)
|
76
|
+
@connection
|
77
|
+
end
|
78
|
+
|
79
|
+
# Write message to connection
|
80
|
+
#
|
81
|
+
# @param payload [String]
|
82
|
+
def write(payload)
|
83
|
+
if(source_args[:publish_via].to_s == 'exchange')
|
84
|
+
exchange.publish(payload, :routing_key => routing_key)
|
85
|
+
else
|
86
|
+
queue.publish(payload, :routing_key => routing_key)
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
# Send message acknowledgement
|
91
|
+
#
|
92
|
+
# @param tag [String]
|
93
|
+
def ack(tag)
|
94
|
+
channel.acknowledge(tag, false)
|
95
|
+
end
|
96
|
+
|
97
|
+
# Start receiving message
|
98
|
+
def receive_messages
|
99
|
+
queue.subscribe(:block => true, :manual_ack => true) do |info, metadata, payload|
|
100
|
+
if(payload.nil?)
|
101
|
+
payload = metadata
|
102
|
+
metadata = {}
|
103
|
+
end
|
104
|
+
begin
|
105
|
+
payload = MultiJson.load(payload).to_smash
|
106
|
+
rescue MultiJson::ParseError
|
107
|
+
debug 'Received payload not in JSON format. Failed to parse!'
|
108
|
+
end
|
109
|
+
new_message = Smash.new(
|
110
|
+
:raw => Smash.new(
|
111
|
+
:info => info,
|
112
|
+
:metadata => metadata
|
113
|
+
),
|
114
|
+
:content => payload
|
115
|
+
)
|
116
|
+
debug "<#{source}> New message: #{new_message}"
|
117
|
+
source.signal(:new_message, new_message)
|
118
|
+
end
|
119
|
+
true
|
120
|
+
end
|
121
|
+
|
122
|
+
# @return [Symbol] connection library to utilize
|
123
|
+
def connection_library
|
124
|
+
if(source_args[:force_library])
|
125
|
+
source_args[:force_library].to_sym
|
126
|
+
else
|
127
|
+
RUBY_PLATFORM == 'java' ? :march_hare : :bunny
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
@@ -8,22 +8,16 @@ module Carnivore
|
|
8
8
|
|
9
9
|
option :cache_signals
|
10
10
|
|
11
|
-
autoload :
|
11
|
+
autoload :Connection, 'carnivore-rabbitmq/connection'
|
12
12
|
|
13
13
|
# @return [Smash] initialization arguments
|
14
14
|
attr_reader :args
|
15
|
-
# @return [
|
15
|
+
# @return [Connection] current connection
|
16
16
|
attr_reader :connection
|
17
|
-
# @return [
|
18
|
-
attr_reader :
|
19
|
-
# @return [
|
20
|
-
attr_reader :
|
21
|
-
# @return [MarchHare::Queue, Bunny::Queue] current queue
|
22
|
-
attr_reader :queue
|
23
|
-
# @return [String] routing key
|
24
|
-
attr_reader :routing_key
|
25
|
-
# @return [Carnviore::Source::Rabbitmq::MessageCollector] message collector
|
26
|
-
attr_reader :message_collector
|
17
|
+
# @return [Queue]
|
18
|
+
attr_reader :message_queue
|
19
|
+
# @return [TrueClass, FalseClass]
|
20
|
+
attr_reader :collect_messages
|
27
21
|
|
28
22
|
# RabbitMQ source setup
|
29
23
|
#
|
@@ -34,79 +28,43 @@ module Carnivore
|
|
34
28
|
# @option init_args [String, Symbol] :force_library :bunny or :march_hare
|
35
29
|
def setup(init_args={})
|
36
30
|
@args = args.dup
|
37
|
-
@
|
38
|
-
@exchange_name = args[:exchange]
|
31
|
+
@message_queue = Queue.new
|
39
32
|
debug "Creating Rabbitmq source instance <#{name}>"
|
40
33
|
end
|
41
34
|
|
42
35
|
# Connect to the remote server
|
43
36
|
def connect
|
44
|
-
|
45
|
-
end
|
46
|
-
|
47
|
-
# Start the message collection
|
48
|
-
def start_collector
|
49
|
-
unless(@collecting)
|
50
|
-
@message_collector = MessageCollector.new(queue, current_actor)
|
51
|
-
message_collector.start!
|
52
|
-
end
|
37
|
+
@connection = Connection.new(current_self, message_queue)
|
53
38
|
end
|
54
39
|
|
55
40
|
# Destroy message collector
|
56
41
|
#
|
57
42
|
# @return [TrueClass]
|
58
43
|
def terminate
|
59
|
-
|
60
|
-
if(
|
61
|
-
|
44
|
+
super
|
45
|
+
if(connection && connection.alive?)
|
46
|
+
connection.terminate
|
62
47
|
end
|
63
|
-
true
|
64
|
-
end
|
65
|
-
|
66
|
-
# Establish connection to remote server and setup
|
67
|
-
#
|
68
|
-
# @return [MarchHare::Session, Bunny::Session]
|
69
|
-
def establish_connection
|
70
|
-
unless(args[:connection])
|
71
|
-
abort KeyError.new "No configuration defined for connection type (#{connection_library})"
|
72
|
-
end
|
73
|
-
connection_args = Carnivore::Utils.symbolize_hash(args[:connection])
|
74
|
-
case connection_library
|
75
|
-
when :bunny
|
76
|
-
require 'bunny'
|
77
|
-
@connection = Bunny.new(connection_args)
|
78
|
-
when :march_hare
|
79
|
-
require 'march_hare'
|
80
|
-
@connection = MarchHare.connect(connection_args)
|
81
|
-
else
|
82
|
-
abort ArgumentError.new("No valid connection arguments defined (:bunny or :march_hare must be defined)")
|
83
|
-
end
|
84
|
-
connection.start
|
85
|
-
@routing_key = args[:routing_key]
|
86
|
-
@channel = connection.create_channel
|
87
|
-
@exchange = channel.topic(args[:exchange])
|
88
|
-
@queue = channel.queue(args[:queue], :auto_delete => false).
|
89
|
-
bind(exchange, :routing_key => routing_key)
|
90
|
-
@connection
|
91
48
|
end
|
92
49
|
|
93
50
|
# Receive payload from connection
|
94
51
|
#
|
95
52
|
# @return [Hash] payload
|
96
53
|
def receive(*_)
|
97
|
-
|
98
|
-
|
54
|
+
unless(collect_messages)
|
55
|
+
@collect_messages = true
|
56
|
+
connection.async.receive_messages
|
57
|
+
end
|
58
|
+
wait(:new_message)
|
99
59
|
end
|
100
60
|
|
101
61
|
# Transmit payload to connection
|
102
62
|
#
|
103
63
|
# @param payload [Object]
|
104
64
|
def transmit(payload, *_)
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
else
|
109
|
-
queue.publish(payload, :routing_key => routing_key)
|
65
|
+
defer do
|
66
|
+
payload = MultiJson.dump(payload) unless payload.is_a?(String)
|
67
|
+
connection.write(payload)
|
110
68
|
end
|
111
69
|
end
|
112
70
|
|
@@ -114,16 +72,9 @@ module Carnivore
|
|
114
72
|
#
|
115
73
|
# @param message [Carnivore::Message]
|
116
74
|
def confirm(message)
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
# @return [Symbol] connection library to utilize
|
122
|
-
def connection_library
|
123
|
-
if(args[:force_library])
|
124
|
-
args[:force_library].to_sym
|
125
|
-
else
|
126
|
-
RUBY_PLATFORM == 'java' ? :march_hare : :bunny
|
75
|
+
defer do
|
76
|
+
info "Confirming message #{message}"
|
77
|
+
connection.ack(message[:message][:info].delivery_tag)
|
127
78
|
end
|
128
79
|
end
|
129
80
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: carnivore-rabbitmq
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Chris Roberts
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-10-
|
11
|
+
date: 2015-10-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bunny
|
@@ -83,7 +83,7 @@ files:
|
|
83
83
|
- LICENSE
|
84
84
|
- README.md
|
85
85
|
- lib/carnivore-rabbitmq.rb
|
86
|
-
- lib/carnivore-rabbitmq/
|
86
|
+
- lib/carnivore-rabbitmq/connection.rb
|
87
87
|
- lib/carnivore-rabbitmq/rabbitmq.rb
|
88
88
|
- lib/carnivore-rabbitmq/version.rb
|
89
89
|
homepage: https://github.com/heavywater/carnivore-rabbitmq
|
@@ -1,70 +0,0 @@
|
|
1
|
-
require 'carnivore-rabbitmq'
|
2
|
-
|
3
|
-
module Carnivore
|
4
|
-
class Source
|
5
|
-
class Rabbitmq
|
6
|
-
# Message collector
|
7
|
-
class MessageCollector
|
8
|
-
|
9
|
-
include Zoidberg::SoftShell
|
10
|
-
include Zoidberg::Supervise
|
11
|
-
include Carnivore::Utils::Logging
|
12
|
-
|
13
|
-
# @return [Bunny::Queue, MarchHare::Queue] remote queue
|
14
|
-
attr_reader :queue
|
15
|
-
# @return [Celluloid::Actor] actor to notify
|
16
|
-
attr_reader :notify
|
17
|
-
|
18
|
-
# Create new instance
|
19
|
-
#
|
20
|
-
# @param queue [Bunny::Queue, MarchHare::Queue] remote queue
|
21
|
-
# @param notify [Zoidberg::Shell] actor to notify
|
22
|
-
def initialize(queue, notify)
|
23
|
-
@queue = queue
|
24
|
-
@notify = notify
|
25
|
-
end
|
26
|
-
|
27
|
-
# Start message collection when restarted
|
28
|
-
def restarted
|
29
|
-
start!
|
30
|
-
end
|
31
|
-
|
32
|
-
# Start the collector
|
33
|
-
def start!
|
34
|
-
current_self.async.collect_messages
|
35
|
-
end
|
36
|
-
|
37
|
-
# Collect messages from remote queue
|
38
|
-
#
|
39
|
-
# @return [TrueClass]
|
40
|
-
def collect_messages
|
41
|
-
queue.subscribe(:block => true, :manual_ack => true) do |info, metadata, payload|
|
42
|
-
if(payload.nil?)
|
43
|
-
payload = metadata
|
44
|
-
metadata = {}
|
45
|
-
end
|
46
|
-
begin
|
47
|
-
payload = MultiJson.load(payload).to_smash
|
48
|
-
rescue MultiJson::ParseError
|
49
|
-
debug 'Received payload not in JSON format. Failed to parse!'
|
50
|
-
end
|
51
|
-
debug "Message received: #{payload.inspect}"
|
52
|
-
debug "Message info: #{info.inspect}"
|
53
|
-
debug "Message metadata: #{metadata.inspect}"
|
54
|
-
new_message = Smash.new(
|
55
|
-
:raw => Smash.new(
|
56
|
-
:info => info,
|
57
|
-
:metadata => metadata
|
58
|
-
),
|
59
|
-
:content => payload
|
60
|
-
)
|
61
|
-
debug "Sending new message signal to: #{notify}"
|
62
|
-
notify.signal(:new_messages, new_message)
|
63
|
-
end
|
64
|
-
true
|
65
|
-
end
|
66
|
-
|
67
|
-
end
|
68
|
-
end
|
69
|
-
end
|
70
|
-
end
|