manageiq-messaging 0.1.4 → 0.1.5

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
  SHA256:
3
- metadata.gz: 78b0fa6b782a5bb3e21552abd1c0b54f473bdc460c722f896a7fecc7389259b3
4
- data.tar.gz: f0ff3e82e5108b2241cebcfb2cbf9dde8208c3cf21e61b355d1cec9292562a64
3
+ metadata.gz: 6d0c0df7c314aa421d82df4122b949a0c3a1bdb10619e77de5e56453b363d339
4
+ data.tar.gz: 41d110a2e0625e016ba6e9744cfa0c2cf3ae58dae9944aa0ac144b5dbc144905
5
5
  SHA512:
6
- metadata.gz: 331823865454b700433699cb00f8938311ba91cf0ad87a807ffbc955be6e2d7060e5fc3fabbe91fea5f52b72eaf74a49d0909e94a63a665ccb2f44f9e56f76a0
7
- data.tar.gz: a3e6c91fc5ba3a476908d26baf2f5cb3d1d4c768d63e69d3e318442b35f4bf71a3cf501d9dc12bbd53cdba66121f19d2c87ec14dd7eabc19a8aa4aefa40dc302
6
+ metadata.gz: 9abed064106ee030cd2dd04a7571f91f51f75d2a97bf0c2c15be5a36c8150f8cf910c6c8e4b47c47a659ca4e2134e45798d2c78a62fbe1e92c10adafdfde9b31
7
+ data.tar.gz: ea8f2f8c8c7712cc91a7f27a47af03ebdbee2fe1366c05b185bf0293bab04d967e190c5b8914389c9c8db2ce82dd13a9b927ae57c9076a0af0cada7e36294481
data/CHANGES CHANGED
@@ -11,3 +11,6 @@
11
11
  = 0.1.4 - 3-Apr-2019
12
12
  * Add an #ack method to a ReceivedMessage to simplify manual acknowledgements
13
13
  * Allow caller to provide a session_timeout to kafka consumers, default of 30sec
14
+
15
+ = 0.1.5 - 6-Jun-2019
16
+ * Allow caller to provide extra headers to the message
data/README.md CHANGED
@@ -156,6 +156,24 @@ This is the one-to-many publish/subscribe pattern. Multiple subscribers can subs
156
156
 
157
157
  By default, events are delivered to live subscribers only. Some messaging systems support persistence with options.
158
158
 
159
+ ### Add your own headers to a message (Queue or Topic)
160
+
161
+ If you want you can add in your own headers to the send message
162
+
163
+ ```
164
+ client.publish_topic(
165
+ :service => 'provider_events',
166
+ :event => 'powered_on',
167
+ :headers => {:request_id => "12345"},
168
+ :payload => {:ems_ref => 'uid987'}
169
+
170
+ )
171
+
172
+ client.subscribe_topic(:service => 'provider_events', :persist_ref => 'automate_1') do |msg|
173
+ puts "Received event #{msg.message} with request-id: #{msg.headers['request_id']}"
174
+ end
175
+ ```
176
+
159
177
  ## Development
160
178
 
161
179
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -58,6 +58,7 @@ module ManageIQ
58
58
  # * :message (e.g. method name or message type)
59
59
  # * :payload (message body, a string or an user object that can be serialized)
60
60
  # * :sender (optional, identify the sender)
61
+ # * :headers (optional, additional headers to add to the message)
61
62
  # Other options are underlying messaging system specific.
62
63
  #
63
64
  # Optionally a call back block can be provided to wait on the consumer to send
@@ -164,6 +165,7 @@ module ManageIQ
164
165
  # * :event (event name)
165
166
  # * :payload (message body, a string or an user object that can be serialized)
166
167
  # * :sender (optional, identify the sender)
168
+ # * :headers (optional, additional headers to add to the message)
167
169
  # Other options are underlying messaging system specific.
168
170
  #
169
171
  def publish_topic(options)
@@ -53,6 +53,7 @@ module ManageIQ
53
53
  body, kafka_opts = for_publish(options)
54
54
  kafka_opts[:headers][:message_type] = options[:message] if options[:message]
55
55
  kafka_opts[:headers][:class_name] = options[:class_name] if options[:class_name]
56
+ kafka_opts[:headers].merge!(options[:headers].except(*message_header_keys)) if options.key?(:headers)
56
57
 
57
58
  [body, kafka_opts]
58
59
  end
@@ -60,13 +61,15 @@ module ManageIQ
60
61
  def topic_for_publish(options)
61
62
  body, kafka_opts = for_publish(options)
62
63
  kafka_opts[:headers][:event_type] = options[:event] if options[:event]
64
+ kafka_opts[:headers].merge!(options[:headers].except(*event_header_keys)) if options.key?(:headers)
63
65
 
64
66
  [body, kafka_opts]
65
67
  end
66
68
 
67
69
  def for_publish(options)
68
- kafka_opts = {:topic => address(options), :headers => {}}
69
- kafka_opts[:partition_key] = options[:group_name] if options[:group_name]
70
+ kafka_opts = {:topic => address(options)}
71
+ kafka_opts[:partition_key] = options[:group_name] if options[:group_name]
72
+ kafka_opts[:headers] = {}
70
73
  kafka_opts[:headers][:sender] = options[:sender] if options[:sender]
71
74
 
72
75
  body = options[:payload] || ''
@@ -85,26 +88,39 @@ module ManageIQ
85
88
  def process_queue_message(queue, message)
86
89
  payload = decode_body(message.headers, message.value)
87
90
  sender, message_type, class_name = parse_message_headers(message.headers)
91
+ client_headers = message.headers.except(*message_header_keys)
92
+
88
93
  logger.info("Message received: queue(#{queue}), message(#{payload_log(payload)}), sender(#{sender}), type(#{message_type})")
89
- [sender, message_type, class_name, payload]
94
+ [sender, message_type, class_name, payload, client_headers]
90
95
  end
91
96
 
92
97
  def process_topic_message(topic, message)
93
98
  begin
94
99
  payload = decode_body(message.headers, message.value)
95
100
  sender, event_type = parse_event_headers(message.headers)
101
+ client_headers = message.headers.except(*event_header_keys)
102
+
96
103
  logger.info("Event received: topic(#{topic}), event(#{payload_log(payload)}), sender(#{sender}), type(#{event_type})")
97
- yield ManageIQ::Messaging::ReceivedMessage.new(sender, event_type, payload, message, self)
104
+ yield ManageIQ::Messaging::ReceivedMessage.new(sender, event_type, payload, client_headers, message, self)
98
105
  logger.info("Event processed")
99
106
  rescue StandardError => e
100
107
  logger.error("Event processing error: #{e.message}")
101
108
  logger.error(e.backtrace.join("\n"))
109
+ raise
102
110
  end
103
111
  end
104
112
 
113
+ def message_header_keys
114
+ ['sender', 'message_type', 'class_name']
115
+ end
116
+
105
117
  def parse_message_headers(headers)
106
118
  return [nil, nil, nil] unless headers.kind_of?(Hash)
107
- headers.values_at('sender', 'message_type', 'class_name')
119
+ headers.values_at(*message_header_keys)
120
+ end
121
+
122
+ def event_header_keys
123
+ ['sender', 'event_type']
108
124
  end
109
125
 
110
126
  def parse_event_headers(headers)
@@ -29,7 +29,7 @@ module ManageIQ
29
29
  begin
30
30
  messages = batch.messages.collect do |message|
31
31
  sender, message_type, _class_name, payload = process_queue_message(topic, message)
32
- ManageIQ::Messaging::ReceivedMessage.new(sender, message_type, payload, message, self)
32
+ ManageIQ::Messaging::ReceivedMessage.new(sender, message_type, payload, headers, message, self)
33
33
  end
34
34
 
35
35
  yield messages
@@ -1,10 +1,10 @@
1
1
  module ManageIQ
2
2
  module Messaging
3
3
  class ReceivedMessage
4
- attr_accessor :sender, :message, :payload, :ack_ref, :client
4
+ attr_accessor :sender, :message, :payload, :headers, :ack_ref, :client
5
5
 
6
- def initialize(sender, message, payload, ack_ref, client)
7
- @sender, @message, @payload, @ack_ref, @client = sender, message, payload, ack_ref, client
6
+ def initialize(sender, message, payload, headers, ack_ref, client)
7
+ @sender, @message, @payload, @headers, @ack_ref, @client = sender, message, payload, headers, ack_ref, client
8
8
  end
9
9
 
10
10
  def ack
@@ -17,6 +17,8 @@ module ManageIQ
17
17
  address = "queue/#{options[:service]}.#{affinity}"
18
18
 
19
19
  headers = {:"destination-type" => 'ANYCAST', :persistent => true}
20
+ headers.merge!(options[:headers].except(*internal_header_keys)) if options.key?(:headers)
21
+
20
22
  headers[:expires] = options[:expires_on].to_i * 1000 if options[:expires_on]
21
23
  headers[:AMQ_SCHEDULED_TIME] = options[:deliver_on].to_i * 1000 if options[:deliver_on]
22
24
  headers[:priority] = options[:priority] if options[:priority]
@@ -38,6 +40,8 @@ module ManageIQ
38
40
  address = "topic/#{options[:service]}"
39
41
 
40
42
  headers = {:"destination-type" => 'MULTICAST', :persistent => true}
43
+ headers.merge!(options[:headers].except(*internal_header_keys)) if options.key?(:headers)
44
+
41
45
  headers[:expires] = options[:expires_on].to_i * 1000 if options[:expires_on]
42
46
  headers[:AMQ_SCHEDULED_TIME] = options[:deliver_on].to_i * 1000 if options[:deliver_on]
43
47
  headers[:priority] = options[:priority] if options[:priority]
@@ -63,6 +67,10 @@ module ManageIQ
63
67
  raw_publish(address, result || '', response_headers.merge(:correlation_id => correlation_ref))
64
68
  end
65
69
 
70
+ def internal_header_keys
71
+ [:"destination-type", :persistent, :expires, :AMQ_SCHEDULED_TIME, :priority, :_AMQ_GROUP_ID]
72
+ end
73
+
66
74
  def receive_response(service, correlation_ref)
67
75
  response_options = {
68
76
  :service => "#{service}.response",
@@ -33,9 +33,15 @@ module ManageIQ
33
33
  sender = msg.headers['sender']
34
34
  message_type = msg.headers['message_type']
35
35
  message_body = decode_body(msg.headers, msg.body)
36
+ client_headers = msg.headers.except(*internal_header_keys)
37
+
36
38
  logger.info("Message received: queue(#{queue_name}), msg(#{payload_log(message_body)}), headers(#{msg.headers})")
37
39
 
38
- result = yield [ManageIQ::Messaging::ReceivedMessage.new(sender, message_type, message_body, msg, self)]
40
+ messages = [
41
+ ManageIQ::Messaging::ReceivedMessage.new(sender, message_type, message_body, client_headers, msg, self)
42
+ ]
43
+
44
+ result = yield messages
39
45
  logger.info("Message processed")
40
46
 
41
47
  correlation_ref = msg.headers['correlation_id']
@@ -21,9 +21,11 @@ module ManageIQ
21
21
 
22
22
  sender = event.headers['sender']
23
23
  event_type = event.headers['event_type']
24
+ client_headers = event.headers.except(*internal_header_keys)
25
+
24
26
  event_body = decode_body(event.headers, event.body)
25
27
  logger.info("Event received: queue(#{queue_name}), event(#{event_body}), headers(#{event.headers})")
26
- yield ManageIQ::Messaging::ReceivedMessage.new(sender, event_type, event_body, event, self)
28
+ yield ManageIQ::Messaging::ReceivedMessage.new(sender, event_type, event_body, client_headers, event, self)
27
29
  logger.info("Event processed")
28
30
  rescue => e
29
31
  logger.error("Event processing error: #{e.message}")
@@ -1,5 +1,5 @@
1
1
  module ManageIQ
2
2
  module Messaging
3
- VERSION = "0.1.4"
3
+ VERSION = "0.1.5"
4
4
  end
5
5
  end
@@ -24,10 +24,11 @@ Gem::Specification.new do |spec|
24
24
  spec.add_dependency 'ruby-kafka', '~> 0.7.0'
25
25
  spec.add_dependency 'stomp', '~> 1.4.4'
26
26
 
27
- spec.add_development_dependency "bundler", "~> 1.13"
27
+ spec.add_development_dependency "bundler"
28
28
  spec.add_development_dependency "codeclimate-test-reporter", "~> 1.0.0"
29
29
  spec.add_development_dependency "rake", "~> 10.0"
30
30
  spec.add_development_dependency "rspec", "~> 3.0"
31
31
  spec.add_development_dependency "rubocop"
32
+ spec.add_development_dependency "rubocop-performance"
32
33
  spec.add_development_dependency "simplecov"
33
34
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: manageiq-messaging
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - ManageIQ Authors
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-04-03 00:00:00.000000000 Z
11
+ date: 2019-06-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -56,16 +56,16 @@ dependencies:
56
56
  name: bundler
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - "~>"
59
+ - - ">="
60
60
  - !ruby/object:Gem::Version
61
- version: '1.13'
61
+ version: '0'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - "~>"
66
+ - - ">="
67
67
  - !ruby/object:Gem::Version
68
- version: '1.13'
68
+ version: '0'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: codeclimate-test-reporter
71
71
  requirement: !ruby/object:Gem::Requirement
@@ -122,6 +122,20 @@ dependencies:
122
122
  - - ">="
123
123
  - !ruby/object:Gem::Version
124
124
  version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: rubocop-performance
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
125
139
  - !ruby/object:Gem::Dependency
126
140
  name: simplecov
127
141
  requirement: !ruby/object:Gem::Requirement