hanami-events-cloud_pubsub 2.0.0 → 2.1.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: b786cbc6b85949059720828605ac3263973bda40906a06d0c879984e1f293e9c
4
- data.tar.gz: 3850d7de58d0da55bd0c975b1cae04ee0fd218a47e9a1da08791e823361da33e
3
+ metadata.gz: 639fa55622e6b2d507f561c134d807559f514262fa046e853bac4c0ba6b9af1d
4
+ data.tar.gz: 61fe66adcaf2b93d19ba8b45f5aae9c0fed36820f75336e43ecb9d9d903796cd
5
5
  SHA512:
6
- metadata.gz: baa476518202b7e5e71c589c16b1c31f49a5b2f8a04c90cc697ce59740fc6b5de04bf7dfa0d7c6b73169ea5d6a8bc840246c20060b9cd6f2e18ae7e21047c00a
7
- data.tar.gz: 37fe813d6d91d89bf79d3b6e16fa40973a8bdd9f2ce2f7d0aea7bc3fe07ba91367c08e75b1d71af8fef09adeff0d2ef114bc31a88df7accf0815bc88c16964e5
6
+ metadata.gz: 667da0f37844d1101ad21e9ff32dccee22785a22deecb149bb068428d3a9af971dd017346df762ca4b3fb5ed4bd44acf00023e77f15b5720d43c9b7bc013bd72
7
+ data.tar.gz: 94c662671929bc356a0b7d4f9dec885b0916fa49d0205bb4bc092b40111b9e67d69a593987014316ff807c30a18baa254e9adc1fa652cc0489baf0c374bd0092
data/Gemfile.lock CHANGED
@@ -8,7 +8,7 @@ GIT
8
8
  PATH
9
9
  remote: .
10
10
  specs:
11
- hanami-events-cloud_pubsub (2.0.0)
11
+ hanami-events-cloud_pubsub (2.1.0)
12
12
  dry-configurable (~> 0.8.0)
13
13
  google-cloud-pubsub (~> 0.34.0)
14
14
  hanami-cli (>= 0.2.0)
@@ -49,7 +49,7 @@ GEM
49
49
  googleauth (>= 0.6.2, < 0.10.0)
50
50
  grpc (>= 1.7.2, < 2.0)
51
51
  rly (~> 0.2.3)
52
- google-protobuf (3.7.0)
52
+ google-protobuf (3.7.0-x86_64-linux)
53
53
  googleapis-common-protos (1.3.8)
54
54
  google-protobuf (~> 3.0)
55
55
  googleapis-common-protos-types (~> 1.0)
@@ -63,7 +63,7 @@ GEM
63
63
  multi_json (~> 1.11)
64
64
  os (>= 0.9, < 2.0)
65
65
  signet (~> 0.7)
66
- grpc (1.19.0)
66
+ grpc (1.19.0-x86_64-linux)
67
67
  google-protobuf (~> 3.1)
68
68
  googleapis-common-protos-types (~> 1.0.0)
69
69
  grpc-google-iam-v1 (0.6.9)
data/README.md CHANGED
@@ -30,6 +30,9 @@ Hanami.configure do
30
30
  config.pubsub = { project_id: 'emulator' } # optional
31
31
  config.logger = Hanami.logger # optional
32
32
  config.namespace = :staging # optional
33
+ config.auto_create_topics = false # optional
34
+ config.auto_create_subscriptions = false # optional
35
+ config.error_handlers << ->(err, message) { MyErrorReporter.report(err) }
33
36
  # ...
34
37
  end
35
38
  end
@@ -93,11 +93,12 @@ module Hanami
93
93
  @serializer ||= Hanami::Events::Serializer[@serializer_type].new
94
94
  end
95
95
 
96
- def topic_for(event_name)
97
- @topic_registry[event_name.to_s] ||=
98
- begin
99
- @pubsub.find_topic(event_name) || @pubsub.create_topic(event_name)
100
- end
96
+ def topic_for(name)
97
+ @topic_registry[name.to_s] ||= begin
98
+ @pubsub.find_topic(name) ||
99
+ (CloudPubsub.auto_create_topics && @pubsub.create_topic(name)) ||
100
+ raise(CloudPubsub::Errors::TopicNotFoundError, "no topic named: #{name}")
101
+ end
101
102
  end
102
103
 
103
104
  def namespaced(val, sep: '.')
@@ -7,6 +7,7 @@ require 'hanami/events/cloud_pubsub/middleware/stack'
7
7
  require 'hanami/events/cloud_pubsub/middleware/logging'
8
8
  require 'hanami/events/cloud_pubsub/middleware/auto_retry'
9
9
  require 'hanami/events/cloud_pubsub/runner'
10
+ require 'hanami/events/cloud_pubsub/errors'
10
11
  require 'google/cloud/pubsub'
11
12
  require 'dry-configurable'
12
13
 
@@ -31,6 +32,8 @@ module Hanami
31
32
  end
32
33
 
33
34
  setting :project_id, reader: true
35
+ setting :auto_create_subscriptions, false, reader: true
36
+ setting :auto_create_topics, false, reader: true
34
37
  setting :logger, Logger.new(STDOUT), reader: true
35
38
  setting :subscriptions_loader, proc {
36
39
  abort <<~MSG
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Hanami
4
+ module Events
5
+ module CloudPubsub
6
+ # Errors
7
+ module Errors
8
+ class Error < StandardError; end
9
+ class TopicNotFoundError < Error; end
10
+ class SubscriptionNotFoundError < Error; end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -17,8 +17,6 @@ module Hanami
17
17
  :event_name,
18
18
  :subscriber_opts,
19
19
  :middleware
20
-
21
- # rubocop:disable Metrics/ParameterLists
22
20
  def initialize(topic:,
23
21
  logger:,
24
22
  handler:,
@@ -34,7 +32,6 @@ module Hanami
34
32
  @subscriber_opts = CloudPubsub.config.subscriber.to_h.merge(subscriber_opts)
35
33
  @middleware = middleware
36
34
  end
37
- # rubocop:enable Metrics/ParameterLists
38
35
 
39
36
  def register
40
37
  subscription = subscription_for(subscriber_id)
@@ -90,9 +87,9 @@ module Hanami
90
87
  end
91
88
 
92
89
  def subscription_for(name)
93
- topic.create_subscription(name)
94
- rescue Google::Cloud::AlreadyExistsError
95
- topic.find_subscription(name)
90
+ topic.find_subscription(name) ||
91
+ (CloudPubsub.auto_create_subscriptions && topic.create_subscription(name)) ||
92
+ raise(Errors::SubscriptionNotFoundError, "no subscription named: #{name}")
96
93
  end
97
94
 
98
95
  def ensure_subscriber!
@@ -11,7 +11,7 @@ module Hanami
11
11
  @logger = logger
12
12
  end
13
13
 
14
- def call(message, *_args)
14
+ def call(message, args = {})
15
15
  succeeded = false
16
16
  failed = false
17
17
  yield
@@ -20,19 +20,19 @@ module Hanami
20
20
  failed = true
21
21
  raise err
22
22
  ensure
23
- ack_or_reject(message, succeeded, failed)
23
+ ack_or_reject(message, succeeded, failed, args)
24
24
  end
25
25
 
26
26
  private
27
27
 
28
- def ack_or_reject(message, succeeded, failed)
28
+ def ack_or_reject(message, succeeded, failed, args)
29
29
  id = message.message_id
30
30
 
31
31
  if succeeded
32
32
  message.acknowledge!
33
33
  logger.debug "Message(#{id}) was acknowledged"
34
34
  elsif failed
35
- seconds = calculate_backoff_seconds(message)
35
+ seconds = calculate_backoff_seconds(message, args)
36
36
  success = message.modify_ack_deadline!(seconds)
37
37
  msg = "added #{success ? seconds : 0} seconds of delay to ack deadline"
38
38
  logger.debug "Message(#{id}) failed, #{msg}" if success
@@ -46,9 +46,16 @@ module Hanami
46
46
  @logger || CloudPubsub.logger
47
47
  end
48
48
 
49
- def calculate_backoff_seconds(_message)
50
- # Figure out a way to keep track of retries
51
- 60
49
+ def calculate_backoff_seconds(_message, args)
50
+ amt = if args.key?(:attempts)
51
+ count = args[:attempts]
52
+ # min + exponential + random smear
53
+ 15 + count**4 + (rand(30) * (count + 1))
54
+ else
55
+ 60
56
+ end
57
+
58
+ amt > 600 ? 600 : amt
52
59
  end
53
60
  end
54
61
  end
@@ -40,14 +40,14 @@ module Hanami
40
40
  @entries.shift
41
41
  end
42
42
 
43
- def invoke(*args)
43
+ def invoke(*base_args)
44
44
  stack = entries.dup
45
45
 
46
- traverse_stack = lambda do
46
+ traverse_stack = lambda do |*args|
47
47
  if stack.empty?
48
- yield(*args)
48
+ yield(*base_args, *args)
49
49
  else
50
- stack.shift.call(*args, &traverse_stack)
50
+ stack.shift.call(*base_args, *args, &traverse_stack)
51
51
  end
52
52
  end
53
53
 
@@ -3,7 +3,7 @@
3
3
  module Hanami
4
4
  module Events
5
5
  module CloudPubsub
6
- VERSION = '2.0.0'
6
+ VERSION = '2.1.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: 2.0.0
4
+ version: 2.1.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: 2019-03-10 00:00:00.000000000 Z
11
+ date: 2019-03-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dry-configurable
@@ -138,6 +138,7 @@ files:
138
138
  - lib/hanami/events/adapter/cloud_pubsub.rb
139
139
  - lib/hanami/events/cloud_pubsub.rb
140
140
  - lib/hanami/events/cloud_pubsub/cli.rb
141
+ - lib/hanami/events/cloud_pubsub/errors.rb
141
142
  - lib/hanami/events/cloud_pubsub/handler.rb
142
143
  - lib/hanami/events/cloud_pubsub/health_check_server.rb
143
144
  - lib/hanami/events/cloud_pubsub/integration.rb