carnivore-rabbitmq 0.1.0 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md CHANGED
@@ -1,2 +1,7 @@
1
+ # v0.1.2
2
+ * Include option to provide routing key
3
+ * Only start message collection if source is receiving
4
+ * Parse payload within collector on receipt
5
+
1
6
  # v0.1.0
2
7
  * Initial release
@@ -30,10 +30,16 @@ module Carnivore
30
30
  # @return [TrueClass]
31
31
  def collect_messages
32
32
  queue.subscribe(:block => true, :ack => true) do |info, metadata, payload|
33
+ begin
34
+ payload = MultiJson.load(payload)
35
+ rescue MultiJson::ParseError
36
+ warn 'Received payload not in JSON format. Failed to parse!'
37
+ end
33
38
  debug "Message received: #{payload.inspect}"
34
39
  debug "Message info: #{info.inspect}"
35
40
  debug "Message metadata: #{metadata.inspect}"
36
41
  message_queue << {:info => info, :metadata => metadata, :payload => payload}
42
+ debug "Sending new messages signal to: #{notify} (current queue size: #{message_queue.size})"
37
43
  notify.signal(:new_messages)
38
44
  end
39
45
  true
@@ -18,6 +18,8 @@ module Carnivore
18
18
  attr_reader :channel
19
19
  # @return [MarchHare::Queue, Bunny::Queue] current queue
20
20
  attr_reader :queue
21
+ # @return [String] routing key
22
+ attr_reader :routing_key
21
23
  # @return [Queue] message queue
22
24
  attr_reader :message_queue
23
25
  # @return [Carnviore::Source::Rabbitmq::MessageCollector] message collector
@@ -28,32 +30,33 @@ module Carnivore
28
30
 
29
31
  # RabbitMQ source setup
30
32
  #
31
- # @param args [Hash] initialization configuration
32
- # @option args [String] :queue name of queue
33
- # @option args [String] :exchange name of exchange
34
- # @option args [Hash] :connection configuration hash for connection
35
- # @option args [String, Symbol] :force_library :bunny or :march_hare
36
- def setup(args={})
33
+ # @param init_args [Hash] initialization configuration
34
+ # @option init_args [String] :queue name of queue
35
+ # @option init_args [String] :exchange name of exchange
36
+ # @option init_args [Hash] :connection configuration hash for connection
37
+ # @option init_args [String, Symbol] :force_library :bunny or :march_hare
38
+ def setup(init_args={})
37
39
  require 'carnivore-rabbitmq/message_collector'
38
40
  @args = args.dup
39
41
  @message_queue = Queue.new
40
42
  @queue_name = args[:queue]
41
43
  @exchange_name = args[:exchange]
42
- @notifier = Celluloid::Signals.new
43
44
  debug "Creating Rabbitmq source instance <#{name}>"
44
45
  end
45
46
 
46
47
  # Connect to the remote server
47
48
  def connect
48
49
  establish_connection
49
- start_collector
50
50
  end
51
51
 
52
52
  # Start the message collection
53
53
  def start_collector
54
- @message_collector = MessageCollector.new(queue, message_queue, current_actor)
55
- self.link message_collector
56
- message_collector.async.collect_messages
54
+ unless(@collecting)
55
+ @collecting = true
56
+ @message_collector = MessageCollector.new(queue, message_queue, current_actor)
57
+ self.link message_collector
58
+ message_collector.async.collect_messages
59
+ end
57
60
  end
58
61
 
59
62
  # Restart collector if unexpectedly failed
@@ -71,8 +74,8 @@ module Carnivore
71
74
  #
72
75
  # @return [TrueClass]
73
76
  def collector_teardown
74
- connection.close
75
- if(message_collector.alive?)
77
+ connection.close if connection
78
+ if(message_collector && message_collector.alive?)
76
79
  message_collector.terminate
77
80
  end
78
81
  true
@@ -99,7 +102,9 @@ module Carnivore
99
102
  connection.start
100
103
  @channel = connection.create_channel
101
104
  @exchange = channel.topic(args[:exchange])
102
- @queue = channel.queue(args[:queue], :auto_delete => false).bind(exchange) # TODO: Add topic key
105
+ @queue = channel.queue(args[:queue], :auto_delete => false).
106
+ bind(exchange, :routing_key => routing_key)
107
+ @routing_key = args.fetch(:routing_key, '_default')
103
108
  @connection
104
109
  end
105
110
 
@@ -107,6 +112,7 @@ module Carnivore
107
112
  #
108
113
  # @return [Hash] payload
109
114
  def receive(*_)
115
+ start_collector
110
116
  while(message_queue.empty?)
111
117
  wait(:new_messages)
112
118
  end
@@ -118,7 +124,7 @@ module Carnivore
118
124
  # @param payload [Object]
119
125
  def transmit(payload, *_)
120
126
  payload = MultiJson.dump(payload) unless payload.is_a?(String)
121
- queue.publish(payload)
127
+ queue.publish(payload, :routing_key => routing_key)
122
128
  end
123
129
 
124
130
  # Confirm message processing
@@ -4,6 +4,6 @@ module Carnivore
4
4
  class Version < Gem::Version
5
5
  end
6
6
  # Current version of library
7
- VERSION = Version.new('0.1.0')
7
+ VERSION = Version.new('0.1.2')
8
8
  end
9
9
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: carnivore-rabbitmq
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-05-13 00:00:00.000000000 Z
12
+ date: 2014-06-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: carnivore