mimi-messaging-sqs_sns 0.8.2 → 0.8.3

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: e106fbe54e012ffd59a6126292242b23c28b3086ff5dfd4866acb64bad559ccf
4
- data.tar.gz: 2fce6feb750d6cf27925e79646b8e76359188343f663ee8d20f30f09eedc2ba0
3
+ metadata.gz: 9ebbd0b876cc696936356711703245e45106ff85ad152bde59fd2826b6cbc21f
4
+ data.tar.gz: ac2167ece90f7de977fee85c66137e5cb9f232e139fcb8ab797c27467cffa540
5
5
  SHA512:
6
- metadata.gz: a830a70afd7b0a5f268d600ca9673a8f3fc36566201510263008f517d32354a020213de0b51497c446e99a768089f2fd9a1392dccdf5bbbc5fc352d3557a2a4e
7
- data.tar.gz: 41dc62d85b21f8736011d974b2749e5f6ce093d3f3cc3746f037f8a986ef01621bc2a61ae60f8f96a5405dcc6672bd1468fec0e3ea695e0b1f1e4a123d71c962
6
+ metadata.gz: 0cdc8a69e02de3533b0871fb9727fbe63e47081fe6a0ac66bf093cacf2f99d16f697034987adc507078bcf14d4864dd7ff78a2025e49305b4ae48c32313b1992
7
+ data.tar.gz: 56665e8adb58d3d17c1fcb7e3b5ef5eea8231d24a130de1bfd589acb90169473bd6088a5a227d3eb6d3141ce19af7939d2a874132607b45579b4a02d4b4c3103
data/examples/event.rb CHANGED
@@ -4,8 +4,8 @@ require "mimi/messaging/sqs_sns"
4
4
 
5
5
  COUNT = 10
6
6
  AWS_REGION = "eu-west-1"
7
- AWS_SQS_ENDPOINT_URL = "http://localstack:4566"
8
- AWS_SNS_ENDPOINT_URL = "http://localstack:4566"
7
+ AWS_SQS_ENDPOINT_URL = "http://sqs-sns:4566"
8
+ AWS_SNS_ENDPOINT_URL = "http://sqs-sns:4566"
9
9
  AWS_ACCESS_KEY_ID = "foo"
10
10
  AWS_SECRET_ACCESS_KEY = "bar"
11
11
 
@@ -6,8 +6,8 @@ COUNT = 10
6
6
  THREADS = 10
7
7
  QUERY_TIMEOUT = 60
8
8
  AWS_REGION = "eu-west-1"
9
- AWS_SQS_ENDPOINT_URL = "http://localstack:4566"
10
- AWS_SNS_ENDPOINT_URL = "http://localstack:4566"
9
+ AWS_SQS_ENDPOINT_URL = "http://sqs-sns:4566"
10
+ AWS_SNS_ENDPOINT_URL = "http://sqs-sns:4566"
11
11
  AWS_ACCESS_KEY_ID = "foo"
12
12
  AWS_SECRET_ACCESS_KEY = "bar"
13
13
  AWS_SQS_SNS_KMS_MASTER_KEY_ID = "blah"
data/examples/query.rb CHANGED
@@ -4,8 +4,8 @@ require "mimi/messaging/sqs_sns"
4
4
 
5
5
  COUNT = 10
6
6
  AWS_REGION = "eu-west-1"
7
- AWS_SQS_ENDPOINT_URL = "http://localstack:4566"
8
- AWS_SNS_ENDPOINT_URL = "http://localstack:4566"
7
+ AWS_SQS_ENDPOINT_URL = "http://sqs-sns:4566"
8
+ AWS_SNS_ENDPOINT_URL = "http://sqs-sns:4566"
9
9
  AWS_ACCESS_KEY_ID = "foo"
10
10
  AWS_SECRET_ACCESS_KEY = "bar"
11
11
  AWS_SQS_SNS_KMS_MASTER_KEY_ID = "blah"
@@ -3,8 +3,8 @@
3
3
  require "mimi/messaging/sqs_sns"
4
4
 
5
5
  AWS_REGION = "eu-west-1"
6
- AWS_SQS_ENDPOINT_URL = "http://localstack:4566"
7
- AWS_SNS_ENDPOINT_URL = "http://localstack:4566"
6
+ AWS_SQS_ENDPOINT_URL = "http://sqs-sns:4566"
7
+ AWS_SNS_ENDPOINT_URL = "http://sqs-sns:4566"
8
8
  AWS_ACCESS_KEY_ID = "foo"
9
9
  AWS_SECRET_ACCESS_KEY = "bar"
10
10
  AWS_SQS_SNS_KMS_MASTER_KEY_ID = "blah"
@@ -3,8 +3,8 @@
3
3
  require "mimi/messaging/sqs_sns"
4
4
 
5
5
  AWS_REGION = "eu-west-1"
6
- AWS_SQS_ENDPOINT_URL = "http://localstack:4566"
7
- AWS_SNS_ENDPOINT_URL = "http://localstack:4566"
6
+ AWS_SQS_ENDPOINT_URL = "http://sqs-sns:4566"
7
+ AWS_SNS_ENDPOINT_URL = "http://sqs-sns:4566"
8
8
  AWS_ACCESS_KEY_ID = "foo"
9
9
  AWS_SECRET_ACCESS_KEY = "bar"
10
10
 
@@ -185,7 +185,7 @@ module Mimi
185
185
  @consumers ||= []
186
186
  opts = opts.dup
187
187
  queue_url = find_or_create_queue(queue_name)
188
- @consumers << Consumer.new(self, queue_url) do |m|
188
+ @consumers << Consumer.new(self, queue_url, worker_pool) do |m|
189
189
  process_request_message(processor, m)
190
190
  end
191
191
  end
@@ -202,7 +202,7 @@ module Mimi
202
202
  topic_arn = find_or_create_topic(topic_name) # TODO: or find_topic!(...) ?
203
203
  queue_url = find_or_create_queue(queue_name)
204
204
  subscribe_topic_queue(topic_arn, queue_url)
205
- @consumers << Consumer.new(self, queue_url) do |m|
205
+ @consumers << Consumer.new(self, queue_url, worker_pool) do |m|
206
206
  process_event_message(processor, m)
207
207
  end
208
208
  end
@@ -10,12 +10,20 @@ module Mimi
10
10
  # (seconds) determines how soon the NACK-ed message becomes visible to other consumers
11
11
  NACK_VISIBILITY_TIMEOUT = 1
12
12
 
13
- def initialize(adapter, queue_url, &block)
13
+ # Creates a new Consumer to read and process messages from the given queue.
14
+ #
15
+ # @param adapter [Mimi::Messaging::SQS_SNS::Adapter]
16
+ # @param queue_url [String]
17
+ # @param worker_pool [Concurrent::ThreadPoolExecutor, nil] an optional worker pool to be used
18
+ #
19
+ def initialize(adapter, queue_url, worker_pool = nil, &block)
14
20
  @stop_requested = false
15
- Mimi::Messaging.log "Starting consumer for: #{queue_url}"
21
+ @worker_pool = worker_pool
22
+ Mimi::Messaging.log "Starting consumer for: #{queue_url}, " \
23
+ " worker_pool: #{worker_pool ? "yes" : "no"}"
16
24
  @consumer_thread = Thread.new do
17
25
  while not @stop_requested
18
- read_and_process_message(adapter, queue_url, block)
26
+ read_and_process_message(adapter, queue_url, worker_pool, block)
19
27
  end
20
28
  Mimi::Messaging.log "Stopping consumer for: #{queue_url}"
21
29
  end
@@ -41,30 +49,33 @@ module Mimi
41
49
  #
42
50
  # @param adapter [Mimi::Messaging::SQS_SNS::Adapter]
43
51
  # @param queue_url [String]
52
+ # @param worker_pool [Concurrent::ThreadPoolExecutor,nil]
44
53
  # @param block [Proc] a block to be invoked when a message is received
45
54
  #
46
- def read_and_process_message(adapter, queue_url, block)
55
+ def read_and_process_message(adapter, queue_url, worker_pool, block)
47
56
  message = read_message(adapter, queue_url)
48
57
  return unless message
49
58
 
50
59
  Mimi::Messaging.log "Read message from: #{queue_url}"
51
- begin
52
- adapter.worker_pool.post do
53
- process_message(adapter, queue_url, message, block)
54
- end
55
- rescue Concurrent::RejectedExecutionError
56
- # the backlog is overflown, put the message back
57
- Mimi::Messaging.log "Worker pool backlog is full, nack-ing the message " \
58
- "(workers:#{adapter.worker_pool.length}, backlog:#{adapter.worker_pool.queue_length})"
59
- nack_message(adapter, queue_url, message)
60
+ if worker_pool
61
+ process_message_worker_pool(adapter, queue_url, worker_pool, message, block)
62
+ else
63
+ process_message(adapter, queue_url, message, block)
60
64
  end
61
65
  rescue StandardError => e
62
66
  Mimi::Messaging.logger&.error(
63
67
  "#{self.class}: failed to read or process message from: #{queue_url}," \
68
+ " worker_pool: #{worker_pool ? "yes" : "no"}" \
64
69
  " error: (#{e.class}) #{e}"
65
70
  )
66
71
  end
67
72
 
73
+ # Returns a message read from SQS queue
74
+ #
75
+ # @param adapter [Mimi::Messaging::SQS_SNS::Adapter]
76
+ # @param queue_url [String]
77
+ # @return [Hash] with stringified keys
78
+ #
68
79
  def read_message(adapter, queue_url)
69
80
  result = adapter.sqs_client.receive_message(
70
81
  queue_url: queue_url,
@@ -78,6 +89,26 @@ module Mimi
78
89
  raise Mimi::Messaging::ConnectionError, "Unexpected number of messages read"
79
90
  end
80
91
 
92
+ # Processes a received message using the worker pool
93
+ #
94
+ # @param adapter [Mimi::Messaging::SQS_SNS::Adapter]
95
+ # @param queue_url [String]
96
+ # @param worker_pool [Concurrent::ThreadPoolExecutor,nil]
97
+ # @param block [Proc] a block to be invoked when a message is received
98
+ #
99
+ def process_message_worker_pool(adapter, queue_url, worker_pool, message, block)
100
+ begin
101
+ worker_pool.post do
102
+ process_message(adapter, queue_url, message, block)
103
+ end
104
+ rescue Concurrent::RejectedExecutionError
105
+ # the backlog is overflown, put the message back
106
+ Mimi::Messaging.log "Worker pool backlog is full, nack-ing the message " \
107
+ "(workers:#{worker_pool.length}, backlog:#{worker_pool.queue_length})"
108
+ nack_message(adapter, queue_url, message)
109
+ end
110
+ end
111
+
81
112
  def process_message(adapter, queue_url, message, block)
82
113
  block.call(message)
83
114
  ack_message(adapter, queue_url, message)
@@ -3,7 +3,7 @@
3
3
  module Mimi
4
4
  module Messaging
5
5
  module SQS_SNS
6
- VERSION = "0.8.2"
6
+ VERSION = "0.8.3"
7
7
  end
8
8
  end
9
9
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mimi-messaging-sqs_sns
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.2
4
+ version: 0.8.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Kukushkin
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-03-15 00:00:00.000000000 Z
11
+ date: 2022-03-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mimi-messaging