carnivore-rabbitmq 0.2.0-java → 0.2.8-java

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 298cf52b218557db5decfb95a85beb9bdeaacd17
4
- data.tar.gz: ad3b7d8ff53ceca3f13a4740a41884be812eb29c
3
+ metadata.gz: 9f5fc692f14f0a0c7cfdbc5b2e52fb0d10bfa3b6
4
+ data.tar.gz: 6c8f159c84d001fd8f98e61d36838bdcab77f4f5
5
5
  SHA512:
6
- metadata.gz: f2e07e8e1fe357ff2721bf08505c07b1227cb4289acc1784bcfece31038ca2521fd7548fba376fe3770dce4d477de39103aa9c30adeef98668c06694a5d2c680
7
- data.tar.gz: f1f7de50e2f5c6106c1a2a7704c8ac815bb6003f9215402ec552c1c2f362aa49a236ee147c4f03bfa975dcd1280fded075e7f364fb2147d259a3d571c4720588
6
+ metadata.gz: 8ff6f5f569b0701ec4456781f50eb1917569a8a1e069dd7e5ad6b7f97e5290adbf64a966a6a92473efefbb037b6268dd4dd329b49acfe7fb5ae49f7388257cc7
7
+ data.tar.gz: c1133b8bf9f5e9097326830cf674d602e1d2278b595fdfa9bf42d00db4044064450d872ce1084cd4fd35ee6cdcca08ce866133ffe671e1071fbd8c8d669d761b
data/CHANGELOG.md CHANGED
@@ -1,3 +1,19 @@
1
+ # v0.2.8
2
+ * Ensure custom connection is always killed
3
+ * Close remote connection on termination
4
+
5
+ # v0.2.6
6
+ * Kill connection to server when terminated
7
+
8
+ # v0.2.4
9
+ * Refactor to isolate connection to ease supervision
10
+ * Push received messages via internal signaling
11
+
12
+ # v0.2.2
13
+ * Transfer messages via internal signal
14
+ * Properly cleanup on termination
15
+ * Cache signals on source
16
+
1
17
  # v0.2.0
2
18
  * Update for latest carnivore
3
19
 
@@ -0,0 +1,139 @@
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
+ # Close down the connection if available
53
+ def terminate
54
+ connection.close
55
+ end
56
+
57
+ # Establish connection to remote server and setup
58
+ #
59
+ # @return [MarchHare::Session, Bunny::Session]
60
+ def connect
61
+ unless(source_args[:connection])
62
+ abort KeyError.new "No configuration defined for connection type (#{connection_library})"
63
+ end
64
+ connection_args = Carnivore::Utils.symbolize_hash(source_args[:connection])
65
+ case connection_library
66
+ when :bunny
67
+ require 'bunny'
68
+ @connection = Bunny.new(connection_args)
69
+ when :march_hare
70
+ require 'march_hare'
71
+ @connection = MarchHare.connect(connection_args)
72
+ else
73
+ abort ArgumentError.new("No valid connection arguments defined (:bunny or :march_hare must be defined)")
74
+ end
75
+ connection.start
76
+ @routing_key = source_args[:routing_key]
77
+ @channel = connection.create_channel
78
+ @exchange = channel.topic(source_args[:exchange])
79
+ @queue = channel.queue(source_args[:queue], :auto_delete => false).
80
+ bind(exchange, :routing_key => routing_key)
81
+ @connection
82
+ end
83
+
84
+ # Write message to connection
85
+ #
86
+ # @param payload [String]
87
+ def write(payload)
88
+ if(source_args[:publish_via].to_s == 'exchange')
89
+ exchange.publish(payload, :routing_key => routing_key)
90
+ else
91
+ queue.publish(payload, :routing_key => routing_key)
92
+ end
93
+ end
94
+
95
+ # Send message acknowledgement
96
+ #
97
+ # @param tag [String]
98
+ def ack(tag)
99
+ channel.acknowledge(tag, false)
100
+ end
101
+
102
+ # Start receiving message
103
+ def receive_messages
104
+ queue.subscribe(:block => true, :manual_ack => true) do |info, metadata, payload|
105
+ if(payload.nil?)
106
+ payload = metadata
107
+ metadata = {}
108
+ end
109
+ begin
110
+ payload = MultiJson.load(payload).to_smash
111
+ rescue MultiJson::ParseError
112
+ debug 'Received payload not in JSON format. Failed to parse!'
113
+ end
114
+ new_message = Smash.new(
115
+ :raw => Smash.new(
116
+ :info => info,
117
+ :metadata => metadata
118
+ ),
119
+ :content => payload
120
+ )
121
+ debug "<#{source}> New message: #{new_message}"
122
+ source.signal(:new_message, new_message)
123
+ end
124
+ true
125
+ end
126
+
127
+ # @return [Symbol] connection library to utilize
128
+ def connection_library
129
+ if(source_args[:force_library])
130
+ source_args[:force_library].to_sym
131
+ else
132
+ RUBY_PLATFORM == 'java' ? :march_hare : :bunny
133
+ end
134
+ end
135
+
136
+ end
137
+ end
138
+ end
139
+ end
@@ -6,24 +6,18 @@ module Carnivore
6
6
  # RabbitMQ based carnivore source
7
7
  class Rabbitmq < Source
8
8
 
9
- autoload :MessageCollector, 'carnivore-rabbitmq/message_collector'
9
+ option :cache_signals
10
+
11
+ autoload :Connection, 'carnivore-rabbitmq/connection'
10
12
 
11
13
  # @return [Smash] initialization arguments
12
14
  attr_reader :args
13
- # @return [MarchHare::Session, Bunny::Session] current connection
15
+ # @return [Connection] current connection
14
16
  attr_reader :connection
15
- # @return [MarchHare::Exchange, Bunny::Exchange] current exchange
16
- attr_reader :exchange
17
- # @return [MarchHare::Channel, Bunny::Channel] current channel
18
- attr_reader :channel
19
- # @return [MarchHare::Queue, Bunny::Queue] current queue
20
- attr_reader :queue
21
- # @return [String] routing key
22
- attr_reader :routing_key
23
- # @return [Queue] message queue
17
+ # @return [Queue]
24
18
  attr_reader :message_queue
25
- # @return [Carnviore::Source::Rabbitmq::MessageCollector] message collector
26
- attr_reader :message_collector
19
+ # @return [TrueClass, FalseClass]
20
+ attr_reader :collect_messages
27
21
 
28
22
  # RabbitMQ source setup
29
23
  #
@@ -33,97 +27,42 @@ module Carnivore
33
27
  # @option init_args [Hash] :connection configuration hash for connection
34
28
  # @option init_args [String, Symbol] :force_library :bunny or :march_hare
35
29
  def setup(init_args={})
36
- require 'carnivore-rabbitmq/message_collector'
37
30
  @args = args.dup
38
31
  @message_queue = Queue.new
39
- @queue_name = args[:queue]
40
- @exchange_name = args[:exchange]
41
32
  debug "Creating Rabbitmq source instance <#{name}>"
42
33
  end
43
34
 
44
35
  # Connect to the remote server
45
36
  def connect
46
- establish_connection
47
- end
48
-
49
- # Start the message collection
50
- def start_collector
51
- unless(@collecting)
52
- @collecting = true
53
- @message_collector = MessageCollector.new(queue, message_queue, current_actor)
54
- message_collector.async.collect_messages
55
- end
56
- end
57
-
58
- # Restart collector if unexpectedly failed
59
- #
60
- # @param object [Actor] crashed actor
61
- # @param reason [Exception, NilClass]
62
- def collector_failure(object, reason)
63
- if(reason && object == message_collector)
64
- error "Message collector unexpectedly failed: #{reason} (restarting)"
65
- start_collector
66
- end
37
+ @connection = Connection.new(current_self, message_queue)
67
38
  end
68
39
 
69
40
  # Destroy message collector
70
41
  #
71
42
  # @return [TrueClass]
72
- def collector_teardown
73
- connection.close if connection
74
- if(message_collector && message_collector.alive?)
75
- message_collector.terminate
76
- end
77
- true
78
- end
79
-
80
- # Establish connection to remote server and setup
81
- #
82
- # @return [MarchHare::Session, Bunny::Session]
83
- def establish_connection
84
- unless(args[:connection])
85
- abort KeyError.new "No configuration defined for connection type (#{connection_library})"
86
- end
87
- connection_args = Carnivore::Utils.symbolize_hash(args[:connection])
88
- case connection_library
89
- when :bunny
90
- require 'bunny'
91
- @connection = Bunny.new(connection_args)
92
- when :march_hare
93
- require 'march_hare'
94
- @connection = MarchHare.connect(connection_args)
95
- else
96
- abort ArgumentError.new("No valid connection arguments defined (:bunny or :march_hare must be defined)")
97
- end
98
- connection.start
99
- @routing_key = args[:routing_key]
100
- @channel = connection.create_channel
101
- @exchange = channel.topic(args[:exchange])
102
- @queue = channel.queue(args[:queue], :auto_delete => false).
103
- bind(exchange, :routing_key => routing_key)
104
- @connection
43
+ def terminate
44
+ super
45
+ connection.terminate
105
46
  end
106
47
 
107
48
  # Receive payload from connection
108
49
  #
109
50
  # @return [Hash] payload
110
51
  def receive(*_)
111
- start_collector
112
- while(message_queue.empty?)
113
- wait(:new_messages)
52
+ unless(collect_messages)
53
+ @collect_messages = true
54
+ connection.async.receive_messages
114
55
  end
115
- message_queue.pop
56
+ wait(:new_message)
116
57
  end
117
58
 
118
59
  # Transmit payload to connection
119
60
  #
120
61
  # @param payload [Object]
121
62
  def transmit(payload, *_)
122
- payload = MultiJson.dump(payload) unless payload.is_a?(String)
123
- if(args[:publish_via].to_s == 'exchange')
124
- exchange.publish(payload, :routing_key => routing_key)
125
- else
126
- queue.publish(payload, :routing_key => routing_key)
63
+ defer do
64
+ payload = MultiJson.dump(payload) unless payload.is_a?(String)
65
+ connection.write(payload)
127
66
  end
128
67
  end
129
68
 
@@ -131,16 +70,9 @@ module Carnivore
131
70
  #
132
71
  # @param message [Carnivore::Message]
133
72
  def confirm(message)
134
- info "Confirming message #{message}"
135
- channel.acknowledge(message[:message][:info].delivery_tag, false)
136
- end
137
-
138
- # @return [Symbol] connection library to utilize
139
- def connection_library
140
- if(args[:force_library])
141
- args[:force_library].to_sym
142
- else
143
- RUBY_PLATFORM == 'java' ? :march_hare : :bunny
73
+ defer do
74
+ info "Confirming message #{message}"
75
+ connection.ack(message[:message][:info].delivery_tag)
144
76
  end
145
77
  end
146
78
 
@@ -1,6 +1,6 @@
1
1
  module Carnivore
2
2
  module Rabbitmq
3
3
  # Current version of library
4
- VERSION = Gem::Version.new('0.2.0')
4
+ VERSION = Gem::Version.new('0.2.8')
5
5
  end
6
6
  end
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.0
4
+ version: 0.2.8
5
5
  platform: java
6
6
  authors:
7
7
  - Chris Roberts
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-09-25 00:00:00.000000000 Z
11
+ date: 2015-10-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: march_hare
@@ -83,7 +83,7 @@ files:
83
83
  - LICENSE
84
84
  - README.md
85
85
  - lib/carnivore-rabbitmq.rb
86
- - lib/carnivore-rabbitmq/message_collector.rb
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,64 +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 [Queue] local message bucket
16
- attr_reader :message_queue
17
- # @return [Celluloid::Actor] actor to notify
18
- attr_reader :notify
19
-
20
- # Create new instance
21
- #
22
- # @param queue [Bunny::Queue, MarchHare::Queue] remote queue
23
- # @param message_queue [Queue] local message bucket
24
- # @param notify [Celluloid::Actor] actor to notify
25
- def initialize(queue, message_queue, notify)
26
- @queue = queue
27
- @message_queue = message_queue
28
- @notify = notify
29
- end
30
-
31
- # Collect messages from remote queue
32
- #
33
- # @return [TrueClass]
34
- def collect_messages
35
- queue.subscribe(:block => true, :manual_ack => true) do |info, metadata, payload|
36
- if(payload.nil?)
37
- payload = metadata
38
- metadata = {}
39
- end
40
- begin
41
- payload = MultiJson.load(payload).to_smash
42
- rescue MultiJson::ParseError
43
- warn 'Received payload not in JSON format. Failed to parse!'
44
- end
45
- debug "Message received: #{payload.inspect}"
46
- debug "Message info: #{info.inspect}"
47
- debug "Message metadata: #{metadata.inspect}"
48
- message_queue << Smash.new(
49
- :raw => Smash.new(
50
- :info => info,
51
- :metadata => metadata
52
- ),
53
- :content => payload
54
- )
55
- debug "Sending new messages signal to: #{notify} (current queue size: #{message_queue.size})"
56
- notify.signal(:new_messages)
57
- end
58
- true
59
- end
60
-
61
- end
62
- end
63
- end
64
- end