hermes_messenger_of_the_gods 3.0.0.rc1 → 3.0.0.rc3

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: 4c476e60bbfdcf74e23ae4c0678a170d0db9d74252f754f0597c2c9ffec3f087
4
- data.tar.gz: 07a7a6be81771b775f975e67e4c068e6a63462e978b6dda90706714c0283f264
3
+ metadata.gz: cd5650b206ac5ee874b867a415dc4b1fa35a59df94dd9ee686f6d4a568d0ec18
4
+ data.tar.gz: 1095c1beb1db82beafc7eb23818fcba5bca0e65a0511237d796c5b7bc67a2d8c
5
5
  SHA512:
6
- metadata.gz: 2e95976b36e7e11d10f083a6e7aab7786a6e8caa651c83907babf6b62d403d55256b0dd7156c571fffc585c4105475b95ea8a44b336ff277dc05ae329c45be74
7
- data.tar.gz: 23e2e98b7b448148d4c9641526568c5806e8c6e98ea3aa06d33e16317f06aed6ee90188e610bdd5749f675e3c515a8daec4ee31c8e6a4a12a047e415eaaad916
6
+ metadata.gz: c09be9e2e461be0406068d22c779d851b1d0858414f0a8273f74fe0cc56839d253ed00c538026a80c6213bf12d05ddc193b071884cd2d944694d5a2ef9bce0fd
7
+ data.tar.gz: 68fa8509b6d12e21dadbb46d2bbdcfb2b7dbda972e975de2d34977823ee4e7abfde1403c181022fdbe6ddc44e55d936980bbd4885e7ee114f564c3a2afc4492d
@@ -7,10 +7,8 @@ jobs:
7
7
  strategy:
8
8
  fail-fast: false
9
9
  matrix:
10
- rubyimage: ["getterminus/ruby-docker-images:3.0-je-20210920", "getterminus/ruby-docker-images:2.7-je-20210920",]
10
+ rubyVersion: ["2.7.8", "3.0.4", "3.1.2", "3.2.2"]
11
11
  runs-on: ubuntu-latest
12
- container:
13
- image: ${{ matrix.rubyimage }}
14
12
  steps:
15
13
  - name: Install SSH Key
16
14
  uses: webfactory/ssh-agent@v0.5.2
@@ -21,7 +19,11 @@ jobs:
21
19
  uses: actions/checkout@v2
22
20
  with:
23
21
  path: ${{ github.workspace }}/src/github.com/${{ github.repository }}
24
-
22
+ - uses: ruby/setup-ruby@v1
23
+ with:
24
+ ruby-version: ${{ matrix.rubyVersion }}
25
+ bundler-cache: true
26
+ cache-version: "${{ matrix.rubyVersion }}-1"
25
27
  - name: Run Test
26
28
  working-directory: ${{ github.workspace }}/src/github.com/${{ github.repository }}
27
29
  run: >
@@ -16,7 +16,7 @@ module HermesMessengerOfTheGods
16
16
  attr_accessor :original_message, :source_endpoint
17
17
 
18
18
  def validate!
19
- raise HermesMessengerOfTheGods::ValidationError, self unless valid?
19
+ raise HermesMessengerOfTheGods::ValidationError.new(self) unless valid?
20
20
  end
21
21
 
22
22
  def dispatch
@@ -70,6 +70,12 @@ module HermesMessengerOfTheGods
70
70
  end
71
71
 
72
72
  class_methods do
73
+ def send_messages(messages, options = {})
74
+ raise ArgumentError, "All messages must be #{self.class}" unless messages.all?{|m| m.is_a?(self) }
75
+ messages.map(&:validate!)
76
+ messages.first.endpoint.bulk_dispatch!(messages, options)
77
+ end
78
+
73
79
  def from_message(attrs = {}, &blk)
74
80
  attrs = attrs.slice(*_defined_attributes.map(&:to_s))
75
81
  new(attrs, &blk)
@@ -25,10 +25,22 @@ module HermesMessengerOfTheGods
25
25
  false
26
26
  end
27
27
 
28
+ def _transmit_payload(message, options)
29
+ to_transmit_payload(transform_message(message), message, options)
30
+ end
31
+
32
+ def bulk_dispatch!(messages, options)
33
+ payloads = messages.map do |message|
34
+ _transmit_payload(message, options)
35
+ end
36
+
37
+ bulk_transmit(payloads)
38
+ end
39
+
28
40
  def dispatch!(message, options = {})
29
41
  retry_number = 0
30
42
  begin
31
- self.result = transmit(transform_message(message), message, options)
43
+ self.result = transmit(_transmit_payload(message, options))
32
44
  rescue StandardError => e
33
45
  errors << e
34
46
  retry_number += 1
@@ -6,18 +6,43 @@ require 'json'
6
6
  module HermesMessengerOfTheGods
7
7
  module Endpoints
8
8
  class Sns < Base
9
- def sns_topic
10
- @sns_topic ||= Aws::SNS::Topic.new(endpoint,
11
- {
12
- client: HermesMessengerOfTheGods.configuration.sns_client,
13
- }.merge(options[:client_options] || {}))
9
+ def sns_client
10
+ HermesMessengerOfTheGods.configuration.sns_client
14
11
  end
15
12
 
16
- def transmit(message, raw_message, dispatch_options = {})
13
+ def to_transmit_payload(message, raw_message, dispatch_options = {})
17
14
  pub_opts = fetch_option(:publish_options, raw_message) || {}
18
15
 
19
16
  message = JSON.dump(message) if options[:jsonify]
20
- sns_topic.publish(pub_opts.merge(dispatch_options, message: message))
17
+
18
+ pub_opts.merge(dispatch_options, message: message)
19
+ end
20
+
21
+ def transmit(payload)
22
+ bulk_transmit([payload])
23
+ end
24
+
25
+ def bulk_transmit(payloads)
26
+ # Batch Publish requires a message id, so we'll generate one if it's not set
27
+ all_publish_batch_request_entries = payloads.map do |payload|
28
+ {id: SecureRandom.uuid}.merge(payload)
29
+ end
30
+
31
+ failed_msgs = []
32
+
33
+ all_publish_batch_request_entries.each_slice(10) do |publish_batch_request_entries|
34
+ resp = sns_client.publish_batch(topic_arn: endpoint, publish_batch_request_entries: publish_batch_request_entries)
35
+ failed_msgs.concat(resp.failed)
36
+ end
37
+
38
+ if failed_msgs.any?
39
+ all_sender_fault = failed_msgs.all?(&:sender_fault)
40
+ raise FatalError, "Error in dispatching: #{failed_msgs[0].message}" if all_sender_fault
41
+
42
+ raise "Some messages failed to send due to recoverable error #{failed_msgs[0].message}"
43
+ end
44
+
45
+ true
21
46
  end
22
47
  end
23
48
  end
@@ -114,6 +114,10 @@ module HermesMessengerOfTheGods
114
114
  false
115
115
  end
116
116
 
117
+ def sqs_client
118
+ HermesMessengerOfTheGods.configuration.sqs_client
119
+ end
120
+
117
121
  def queue
118
122
  @queue ||= Aws::SQS::Queue.new(
119
123
  endpoint,
@@ -138,11 +142,38 @@ module HermesMessengerOfTheGods
138
142
  approximate_pending_messages.positive?
139
143
  end
140
144
 
141
- def transmit(message, raw_message, dispatch_options = {})
145
+ def to_transmit_payload(message, raw_message, dispatch_options = {})
142
146
  send_opts = fetch_option(:send_options, raw_message) || {}
143
147
 
144
148
  message = JSON.dump(message) if options[:jsonify]
145
- queue.send_message(send_opts.merge(dispatch_options, message_body: message))
149
+
150
+ send_opts.merge(dispatch_options, message_body: message)
151
+ end
152
+
153
+ def transmit(payload)
154
+ bulk_transmit([payload])
155
+ end
156
+
157
+ def bulk_transmit(payloads)
158
+ all_entries = payloads.map! do |payload|
159
+ {id: SecureRandom.uuid}.merge(payload)
160
+ end
161
+
162
+ failed_msgs = []
163
+
164
+ all_entries.each_slice(10) do |entries|
165
+ resp = sqs_client.send_message_batch(queue_url: endpoint, entries: entries)
166
+ failed_msgs.concat(resp.failed)
167
+ end
168
+
169
+ if failed_msgs.any?
170
+ all_sender_fault = failed_msgs.failed.all?(&:sender_fault)
171
+ raise FatalError, "Error in dispatching: #{failed_msgs[0].message}" if all_sender_fault
172
+
173
+ raise "Some messages failed to send due to recoverable error #{failed_msgs[0].message}"
174
+ end
175
+
176
+ true
146
177
  end
147
178
 
148
179
  private
@@ -1,19 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module HermesMessengerOfTheGods
4
- if defined?(ActiveModel::ValidationError)
5
- class ValidationError < ActiveModel::ValidationError; end
6
- else
7
- class ValidationError < StandardError
8
- attr_reader :model
3
+ require 'active_model/validations'
9
4
 
10
- def initialize(model)
11
- @model = model
12
- errors = @model.errors.full_messages.join(', ')
13
- super(errors)
14
- end
15
- end
16
- end
5
+ module HermesMessengerOfTheGods
6
+ class ValidationError < ActiveModel::ValidationError; end
17
7
 
18
8
  class MessageDispatchFailed < StandardError
19
9
  attr_accessor :exceptions
@@ -4,8 +4,6 @@ module HermesMessengerOfTheGods
4
4
  module Endpoints
5
5
  class Local < Base
6
6
  class << self
7
- attr_accessor :transmit_method
8
-
9
7
  def clear_queue!
10
8
  @received = nil
11
9
  end
@@ -21,20 +19,23 @@ module HermesMessengerOfTheGods
21
19
  self.class.received[endpoint]
22
20
  end
23
21
 
24
- def transmit(msg, raw_msg, opts = {})
25
- if self.class.transmit_method
26
- self.class.transmit_method.call(self, msg, raw_msg)
27
- else
28
- do_transmit(msg, opts, raw_msg)
29
- end
22
+ def to_transmit_payload(message, raw_message, dispatch_options = {})
23
+ { message: message, raw_message: raw_message, options: dispatch_options }
24
+ end
25
+
26
+ def transmit(payload)
27
+ bulk_transmit([payload])
30
28
  end
31
29
 
32
- def do_transmit(msg, opts, raw_message = {})
33
- pub_opts = fetch_option(:publish_options, raw_message) || {}
34
- new_msg = { message: msg, options: opts.merge(pub_opts) }
35
- received << new_msg
36
- on_receive&.call(new_msg)
37
- "Recorded msg ##{self.class.received[endpoint].length} for #{endpoint}"
30
+ def bulk_transmit(payloads)
31
+ payloads.each do |msg|
32
+ pub_opts = fetch_option(:publish_options, msg[:raw_message]) || {}
33
+
34
+ new_msg = { message: msg[:message], options: msg[:options].merge(pub_opts) }
35
+ received << new_msg
36
+ on_receive&.call(new_msg)
37
+ "Recorded msg ##{self.class.received[endpoint].length} for #{endpoint}"
38
+ end
38
39
  end
39
40
  end
40
41
  end
@@ -44,7 +45,6 @@ if defined?(RSpec)
44
45
  RSpec.configure do |config|
45
46
  config.after(:each) do
46
47
  HermesMessengerOfTheGods::Endpoints::Local.clear_queue!
47
- HermesMessengerOfTheGods::Endpoints::Local.transmit_method = nil
48
48
  end
49
49
  end
50
50
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module HermesMessengerOfTheGods
4
- VERSION = '3.0.0.rc1'
4
+ VERSION = '3.0.0.rc3'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hermes_messenger_of_the_gods
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.0.rc1
4
+ version: 3.0.0.rc3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian Malinconico
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2023-10-06 00:00:00.000000000 Z
12
+ date: 2023-10-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activemodel