hanami-events-cloud_pubsub 3.1.2 → 3.3.0

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: c1f785cf3b473394906aadc0906661072aaa1e5721b1a24401fc3333df270e14
4
- data.tar.gz: cc270e1411137b1b5bedfbb0b110d8d7dacbdf8f1d217e13b4ae30f9a17f4fa5
3
+ metadata.gz: de5b89251e969ff280f708bfe2453b6f182b3bca5e50bfd98b9e8be372123562
4
+ data.tar.gz: d71ccf32872fae21acb045a8786b7935f051f92a25ff47ad6ff1340a0c88d44a
5
5
  SHA512:
6
- metadata.gz: 178c1973c428c9bbbdb354526f42304d31a38158c6f4816c502c1772d04b3c99c4f60f9121c771c4307d8aeeaaf0f0e79ae5d6ad81c2feb7b9fe1df2c8b00f8e
7
- data.tar.gz: 0d17cd8d26d82f4803d501ac876accaa43d4226a22308dbf99752e5cd7ccff66777cba192f3daba79a43360a21289390c2da34379bf09fa4e55f652ce6b64da3
6
+ metadata.gz: 7740508a3898f859a59fbb39377bb2d6977ec06f48cf2671d177aa647c355945b50209a3b0b3c6815d1222424df7d998a83bb9402b0918118636c8fe053776fb
7
+ data.tar.gz: dd52a0e85056371712d695c6a2fd9650c49d5dfdaaff2c2afdf41cced04e61b2a12633eb2ee185eaa23dd15c40dcbfebef021ec92e14cdcdfa985de5951e7edd
@@ -11,13 +11,12 @@ module Hanami
11
11
  #
12
12
  # @api private
13
13
  class CloudPubsub
14
- attr_reader :subscribers, :listeners, :topic_registry, :middleware
14
+ attr_reader :listeners, :topic_registry, :middleware
15
15
 
16
16
  def initialize(params)
17
17
  @pubsub = params[:pubsub]
18
18
  @logger = params[:logger] || Logger.new($stdout)
19
19
  @listen = params[:listen] || false
20
- @subscribers = Concurrent::Array.new
21
20
  @listeners = Concurrent::Array.new
22
21
  @serializer_type = params.fetch(:serializer, :json).to_sym
23
22
  @topic_registry = {}
@@ -54,17 +53,16 @@ module Hanami
54
53
  # @param id [String] A unique identifier for the subscriber
55
54
  # @param subscriber_opts [String] Additional options for the subscriber
56
55
  # @param block [Block] to execute when event is broadcasted
57
- def subscribe(name, id:, **subscriber_opts, &block)
56
+ def subscribe(name, id:, auto_ack: true, **subscriber_opts, &block)
58
57
  event_name = namespaced(name)
59
58
  namespaced_id = namespaced(id)
60
59
 
61
60
  logger.debug("Subscribed listener \"#{id}\" for event \"#{event_name}\"")
62
61
 
63
62
  sub = Hanami::Events::CloudPubsub::Subscriber.new(event_name, block, logger)
64
- @subscribers << sub
65
63
  topic = topic_for event_name
66
64
 
67
- register_listener(event_name, topic, namespaced_id, subscriber_opts)
65
+ register_listener(event_name, topic, namespaced_id, auto_ack, subscriber_opts, sub)
68
66
  end
69
67
 
70
68
  def flush_messages
@@ -76,28 +74,31 @@ module Hanami
76
74
 
77
75
  attr_reader :logger
78
76
 
79
- def register_listener(event_name, topic, subscriber_id, subscriber_opts)
77
+ # rubocop:disable Metrics/ParameterLists
78
+ def register_listener(event_name, topic, subscriber_id, auto_ack, subscriber_opts, sub)
80
79
  listener = ::Hanami::Events::CloudPubsub::Listener.new(
81
80
  subscriber_id: subscriber_id,
82
81
  event_name: event_name,
83
- handler: method(:call_subscribers),
82
+ handler: call_subscriber(sub),
84
83
  logger: logger,
85
84
  topic: topic,
86
85
  subscriber_opts: subscriber_opts,
87
- dead_letter_topic: dead_letter_topic
86
+ dead_letter_topic: dead_letter_topic,
87
+ auto_ack: auto_ack
88
88
  )
89
89
 
90
90
  @listeners << listener
91
91
  listener.register
92
92
  end
93
+ # rubocop:enable Metrics/ParameterLists
93
94
 
94
- def call_subscribers(message)
95
- data = message.data
96
- payload = serializer.deserialize(data)
97
- event_name = message.attributes['event_name']
95
+ def call_subscriber(sub)
96
+ proc do |message|
97
+ data = message.data
98
+ payload = serializer.deserialize(data)
99
+ event_name = message.attributes['event_name']
98
100
 
99
- @subscribers.each do |subscriber|
100
- subscriber.call(event_name, payload, message)
101
+ sub.call(event_name, payload, message)
101
102
  end
102
103
  end
103
104
 
@@ -7,7 +7,6 @@ require 'hanami/events/cloud_pubsub/middleware/stack'
7
7
  require 'hanami/events/cloud_pubsub/middleware/logging'
8
8
  require 'hanami/events/cloud_pubsub/runner'
9
9
  require 'hanami/events/cloud_pubsub/errors'
10
- require 'google/cloud/pubsub'
11
10
  require 'dry-configurable'
12
11
 
13
12
  module Hanami
@@ -97,6 +96,7 @@ module Hanami
97
96
  end
98
97
 
99
98
  def self.finalize_settings!
99
+ require 'google/cloud/pubsub'
100
100
  conf_hash = config.pubsub
101
101
  conf_hash.each { |key, val| Google::Cloud::Pubsub.configure[key] = val }
102
102
  end
@@ -1,8 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'hanami/cli'
4
- require 'hanami/events/cloud_pubsub'
5
- require 'hanami/events/cloud_pubsub/health_check_server'
6
4
 
7
5
  module Hanami
8
6
  module Events
@@ -55,6 +53,7 @@ module Hanami
55
53
  end
56
54
 
57
55
  def start_server
56
+ require 'hanami/events/cloud_pubsub/health_check_server'
58
57
  server = HealthCheckServer.new(runner, logger)
59
58
  on_shutdown = proc { @event_queue << proc { shutdown } }
60
59
  server.run_in_background(on_shutdown: on_shutdown)
@@ -77,6 +76,7 @@ module Hanami
77
76
  def start_runner
78
77
  logger.debug 'Running in emulator mode' if @emulator
79
78
  logger.info "Starting worker (pid: #{Process.pid})"
79
+ GC.compact if GC.respond_to?(:compact)
80
80
  runner.start
81
81
  end
82
82
 
@@ -86,13 +86,14 @@ module Hanami
86
86
  end
87
87
 
88
88
  def build_runner
89
+ require 'hanami/events/cloud_pubsub'
89
90
  Hanami::Components.resolve 'events'
90
91
  events = Hanami::Components['events']
91
92
  @runner = Runner.new(logger: logger, adapter: events.adapter)
92
93
  end
93
94
 
94
95
  def logger
95
- CloudPubsub.logger
96
+ Hanami::Events::CloudPubsub.logger
96
97
  end
97
98
 
98
99
  def setup_signal_handlers
@@ -33,11 +33,10 @@ module Hanami
33
33
  conf.cloud_pubsub.each { |blk| blk.call(config) }
34
34
  end
35
35
 
36
- ::Hanami::Events.initialize(
37
- :cloud_pubsub,
38
- pubsub: Google::Cloud::Pubsub.new,
39
- logger: Hanami::Components['logger']
40
- )
36
+ require 'google/cloud/pubsub'
37
+
38
+ opts = { pubsub: Google::Cloud::Pubsub.new, logger: Hanami::Components['logger'] }
39
+ ::Hanami::Events.initialize(:cloud_pubsub, **opts)
41
40
  end
42
41
  end
43
42
  end
@@ -26,6 +26,7 @@ module Hanami
26
26
  subscriber_id:,
27
27
  subscriber_opts: {},
28
28
  middleware: CloudPubsub.config.middleware,
29
+ auto_ack: true,
29
30
  dead_letter_topic: nil)
30
31
  @topic = topic
31
32
  @logger = logger
@@ -35,12 +36,13 @@ module Hanami
35
36
  @input_subscriber_opts = subscriber_opts
36
37
  @middleware = middleware
37
38
  @dead_letter_topic = dead_letter_topic
39
+ @auto_ack = auto_ack
38
40
  end
39
41
  # rubocop:enable Metrics/ParameterLists
40
42
 
41
43
  def register
42
44
  subscription = subscription_for(subscriber_id)
43
- apply_retry_options(subscription)
45
+ apply_subscription_options(subscription)
44
46
  listener = subscription.listen(**subscriber_options) { |m| handle_message(m) }
45
47
  logger.debug("Registered listener for #{subscriber_id} with: #{subscriber_options}")
46
48
 
@@ -86,7 +88,7 @@ module Hanami
86
88
 
87
89
  def run_handler(message)
88
90
  middleware.invoke(message) { handler.call(message) }
89
- message.ack!
91
+ message.ack! if @auto_ack
90
92
  rescue StandardError => e
91
93
  run_error_handlers(e, message.message_id.to_s)
92
94
  message.nack! if CloudPubsub.config.auto_retry.enabled
@@ -133,21 +135,38 @@ module Hanami
133
135
  }
134
136
  end
135
137
 
136
- def apply_retry_options(sub)
137
- retry_policy = build_retry_policy
138
+ def apply_subscription_options(sub)
139
+ apply_deadline_options(sub)
140
+ apply_retry_options(sub)
141
+ apply_dead_letter_options(sub)
142
+ rescue StandardError => e
143
+ run_error_handlers(e, nil)
144
+ end
145
+
146
+ def apply_deadline_options(sub)
147
+ sub.deadline = subscriber_options[:deadline] if sub.deadline != subscriber_options[:deadline]
148
+ rescue StandardError => e
149
+ run_error_handlers(e, nil)
150
+ end
151
+
152
+ def apply_dead_letter_options(sub)
138
153
  attempts = CloudPubsub.config.auto_retry.max_attempts
139
154
 
140
- sub.retry_policy = retry_policy if sub.retry_policy&.to_grpc != retry_policy&.to_grpc
141
155
  sub.dead_letter_topic = dead_letter_topic if sub.dead_letter_topic&.name != dead_letter_topic&.name
142
156
  sub.dead_letter_max_delivery_attempts = attempts if sub.dead_letter_topic&.name != dead_letter_topic&.name
157
+ rescue StandardError => e
158
+ run_error_handlers(e, nil)
159
+ end
143
160
 
144
- sub
161
+ def apply_retry_options(sub)
162
+ retry_policy = build_retry_policy
163
+ sub.retry_policy = retry_policy if sub.retry_policy&.to_grpc != retry_policy&.to_grpc
145
164
  rescue StandardError => e
146
165
  run_error_handlers(e, nil)
147
166
  end
148
167
 
149
168
  def build_retry_policy
150
- return unless CloudPubsub.config.auto_retry.enabled
169
+ return unless Hanami::Events::CloudPubsub.config.auto_retry.enabled
151
170
 
152
171
  Google::Cloud::PubSub::RetryPolicy.new(
153
172
  minimum_backoff: CloudPubsub.config.auto_retry.minimum_backoff,
@@ -3,7 +3,7 @@
3
3
  module Hanami
4
4
  module Events
5
5
  module CloudPubsub
6
- VERSION = '3.1.2'
6
+ VERSION = '3.3.0'
7
7
  end
8
8
  end
9
9
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hanami-events-cloud_pubsub
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.1.2
4
+ version: 3.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ian Ker-Seymer
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-02-26 00:00:00.000000000 Z
11
+ date: 2021-05-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dry-configurable