google-cloud-pubsub 1.0.2 → 2.19.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (58) hide show
  1. checksums.yaml +4 -4
  2. data/AUTHENTICATION.md +16 -54
  3. data/CHANGELOG.md +464 -0
  4. data/CONTRIBUTING.md +328 -116
  5. data/EMULATOR.md +1 -1
  6. data/LOGGING.md +94 -2
  7. data/OVERVIEW.md +121 -68
  8. data/TROUBLESHOOTING.md +2 -8
  9. data/lib/google/cloud/pubsub/acknowledge_result.rb +79 -0
  10. data/lib/google/cloud/pubsub/async_publisher/batch.rb +319 -0
  11. data/lib/google/cloud/pubsub/async_publisher.rb +231 -156
  12. data/lib/google/cloud/pubsub/batch_publisher.rb +60 -30
  13. data/lib/google/cloud/pubsub/convert.rb +33 -7
  14. data/lib/google/cloud/pubsub/credentials.rb +2 -2
  15. data/lib/google/cloud/pubsub/errors.rb +93 -0
  16. data/lib/google/cloud/pubsub/flow_controller.rb +137 -0
  17. data/lib/google/cloud/pubsub/message.rb +45 -4
  18. data/lib/google/cloud/pubsub/policy.rb +3 -2
  19. data/lib/google/cloud/pubsub/project.rb +316 -49
  20. data/lib/google/cloud/pubsub/publish_result.rb +6 -1
  21. data/lib/google/cloud/pubsub/received_message.rb +171 -10
  22. data/lib/google/cloud/pubsub/retry_policy.rb +88 -0
  23. data/lib/google/cloud/pubsub/schema/list.rb +180 -0
  24. data/lib/google/cloud/pubsub/schema.rb +310 -0
  25. data/lib/google/cloud/pubsub/service.rb +285 -269
  26. data/lib/google/cloud/pubsub/snapshot/list.rb +4 -6
  27. data/lib/google/cloud/pubsub/snapshot.rb +5 -2
  28. data/lib/google/cloud/pubsub/subscriber/inventory.rb +69 -32
  29. data/lib/google/cloud/pubsub/subscriber/sequencer.rb +115 -0
  30. data/lib/google/cloud/pubsub/subscriber/stream.rb +108 -49
  31. data/lib/google/cloud/pubsub/subscriber/timed_unary_buffer.rb +191 -30
  32. data/lib/google/cloud/pubsub/subscriber.rb +155 -45
  33. data/lib/google/cloud/pubsub/subscription/list.rb +4 -6
  34. data/lib/google/cloud/pubsub/subscription/push_config.rb +55 -31
  35. data/lib/google/cloud/pubsub/subscription.rb +561 -77
  36. data/lib/google/cloud/pubsub/topic/list.rb +4 -6
  37. data/lib/google/cloud/pubsub/topic.rb +372 -52
  38. data/lib/google/cloud/pubsub/version.rb +1 -1
  39. data/lib/google/cloud/pubsub.rb +35 -46
  40. data/lib/google-cloud-pubsub.rb +21 -27
  41. metadata +26 -189
  42. data/lib/google/cloud/pubsub/v1/credentials.rb +0 -41
  43. data/lib/google/cloud/pubsub/v1/doc/google/iam/v1/iam_policy.rb +0 -21
  44. data/lib/google/cloud/pubsub/v1/doc/google/iam/v1/options.rb +0 -21
  45. data/lib/google/cloud/pubsub/v1/doc/google/iam/v1/policy.rb +0 -21
  46. data/lib/google/cloud/pubsub/v1/doc/google/protobuf/duration.rb +0 -91
  47. data/lib/google/cloud/pubsub/v1/doc/google/protobuf/empty.rb +0 -29
  48. data/lib/google/cloud/pubsub/v1/doc/google/protobuf/field_mask.rb +0 -222
  49. data/lib/google/cloud/pubsub/v1/doc/google/protobuf/timestamp.rb +0 -113
  50. data/lib/google/cloud/pubsub/v1/doc/google/pubsub/v1/pubsub.rb +0 -744
  51. data/lib/google/cloud/pubsub/v1/doc/google/type/expr.rb +0 -19
  52. data/lib/google/cloud/pubsub/v1/publisher_client.rb +0 -786
  53. data/lib/google/cloud/pubsub/v1/publisher_client_config.json +0 -105
  54. data/lib/google/cloud/pubsub/v1/subscriber_client.rb +0 -1385
  55. data/lib/google/cloud/pubsub/v1/subscriber_client_config.json +0 -138
  56. data/lib/google/cloud/pubsub/v1.rb +0 -17
  57. data/lib/google/pubsub/v1/pubsub_pb.rb +0 -249
  58. data/lib/google/pubsub/v1/pubsub_services_pb.rb +0 -211
@@ -39,7 +39,7 @@ module Google
39
39
  # subscriber.start
40
40
  #
41
41
  # # Shut down the subscriber when ready to stop receiving messages.
42
- # subscriber.stop.wait!
42
+ # subscriber.stop!
43
43
  #
44
44
  class ReceivedMessage
45
45
  ##
@@ -63,6 +63,44 @@ module Google
63
63
  @grpc.ack_id
64
64
  end
65
65
 
66
+ ##
67
+ # Returns the delivery attempt counter for the message. If a dead letter policy is not set on the subscription,
68
+ # this will be `nil`. See {Topic#subscribe}, {Subscription#dead_letter_topic=} and
69
+ # {Subscription#dead_letter_max_delivery_attempts=}.
70
+ #
71
+ # The delivery attempt counter is `1 + (the sum of number of NACKs and number of ack_deadline exceeds)` for the
72
+ # message.
73
+ #
74
+ # A NACK is any call to `ModifyAckDeadline` with a `0` deadline. An `ack_deadline` exceeds event is whenever a
75
+ # message is not acknowledged within `ack_deadline`. Note that `ack_deadline` is initially
76
+ # `Subscription.ackDeadlineSeconds`, but may get extended automatically by the client library.
77
+ #
78
+ # The first delivery of a given message will have this value as `1`. The value is calculated at best effort and
79
+ # is approximate.
80
+ #
81
+ # @return [Integer, nil] A delivery attempt value of `1` or greater, or `nil` if a dead letter policy is not set
82
+ # on the subscription.
83
+ #
84
+ # @example
85
+ # require "google/cloud/pubsub"
86
+ #
87
+ # pubsub = Google::Cloud::PubSub.new
88
+ #
89
+ # topic = pubsub.topic "my-topic"
90
+ # dead_letter_topic = pubsub.topic "my-dead-letter-topic", skip_lookup: true
91
+ # sub = topic.subscribe "my-topic-sub",
92
+ # dead_letter_topic: dead_letter_topic,
93
+ # dead_letter_max_delivery_attempts: 10
94
+ #
95
+ # subscriber = sub.listen do |received_message|
96
+ # puts received_message.message.delivery_attempt
97
+ # end
98
+ #
99
+ def delivery_attempt
100
+ return nil if @grpc.delivery_attempt && @grpc.delivery_attempt < 1
101
+ @grpc.delivery_attempt
102
+ end
103
+
66
104
  ##
67
105
  # The received message.
68
106
  def message
@@ -91,6 +129,28 @@ module Google
91
129
  end
92
130
  alias msg_id message_id
93
131
 
132
+ ##
133
+ # Identifies related messages for which publish order should be
134
+ # respected.
135
+ #
136
+ # Google Cloud Pub/Sub ordering keys provide the ability to ensure
137
+ # related messages are sent to subscribers in the order in which they
138
+ # were published. Messages can be tagged with an ordering key, a string
139
+ # that identifies related messages for which publish order should be
140
+ # respected. The service guarantees that, for a given ordering key and
141
+ # publisher, messages are sent to subscribers in the order in which they
142
+ # were published. Ordering does not require sacrificing high throughput
143
+ # or scalability, as the service automatically distributes messages for
144
+ # different ordering keys across subscribers.
145
+ #
146
+ # See {Topic#publish_async} and {Subscription#listen}.
147
+ #
148
+ # @return [String]
149
+ #
150
+ def ordering_key
151
+ message.ordering_key
152
+ end
153
+
94
154
  ##
95
155
  # The time at which the message was published.
96
156
  def published_at
@@ -101,6 +161,29 @@ module Google
101
161
  ##
102
162
  # Acknowledges receipt of the message.
103
163
  #
164
+ # @yield [callback] The block to be called when reject operation is done.
165
+ # @yieldparam [Google::Cloud::PubSub::AcknowledgeResult] Result object that contains the status and error.
166
+ #
167
+ # @example
168
+ # require "google/cloud/pubsub"
169
+ #
170
+ # pubsub = Google::Cloud::PubSub.new
171
+ #
172
+ # sub = pubsub.subscription "my-topic-sub"
173
+ # subscriber = sub.listen do |received_message|
174
+ # puts received_message.message.data
175
+ #
176
+ # received_message.acknowledge! do |result|
177
+ # puts result.status
178
+ # end
179
+ # end
180
+ #
181
+ # # Start background threads that will call block passed to listen.
182
+ # subscriber.start
183
+ #
184
+ # # Shut down the subscriber when ready to stop receiving messages.
185
+ # subscriber.stop!
186
+ #
104
187
  # @example
105
188
  # require "google/cloud/pubsub"
106
189
  #
@@ -117,11 +200,16 @@ module Google
117
200
  # subscriber.start
118
201
  #
119
202
  # # Shut down the subscriber when ready to stop receiving messages.
120
- # subscriber.stop.wait!
203
+ # subscriber.stop!
121
204
  #
122
- def acknowledge!
205
+ def acknowledge! &block
123
206
  ensure_subscription!
124
- subscription.acknowledge ack_id
207
+ if subscription.respond_to?(:exactly_once_delivery_enabled) && subscription.exactly_once_delivery_enabled
208
+ subscription.acknowledge ack_id, &block
209
+ else
210
+ subscription.acknowledge ack_id
211
+ yield AcknowledgeResult.new(AcknowledgeResult::SUCCESS) if block_given?
212
+ end
125
213
  end
126
214
  alias ack! acknowledge!
127
215
 
@@ -137,6 +225,30 @@ module Google
137
225
  # seconds after the call is made. Specifying `0` may immediately make
138
226
  # the message available for another pull request.
139
227
  #
228
+ # @yield [callback] The block to be called when reject operation is done.
229
+ # @yieldparam [Google::Cloud::PubSub::AcknowledgeResult] Result object that contains the status and error.
230
+ #
231
+ # @example
232
+ # require "google/cloud/pubsub"
233
+ #
234
+ # pubsub = Google::Cloud::PubSub.new
235
+ #
236
+ # sub = pubsub.subscription "my-topic-sub"
237
+ # subscriber = sub.listen do |received_message|
238
+ # puts received_message.message.data
239
+ #
240
+ # # Delay for 2 minutes
241
+ # received_message.modify_ack_deadline! 120 do |result|
242
+ # puts result.status
243
+ # end
244
+ # end
245
+ #
246
+ # # Start background threads that will call block passed to listen.
247
+ # subscriber.start
248
+ #
249
+ # # Shut down the subscriber when ready to stop receiving messages.
250
+ # subscriber.stop!
251
+ #
140
252
  # @example
141
253
  # require "google/cloud/pubsub"
142
254
  #
@@ -154,11 +266,16 @@ module Google
154
266
  # subscriber.start
155
267
  #
156
268
  # # Shut down the subscriber when ready to stop receiving messages.
157
- # subscriber.stop.wait!
269
+ # subscriber.stop!
158
270
  #
159
- def modify_ack_deadline! new_deadline
271
+ def modify_ack_deadline! new_deadline, &block
160
272
  ensure_subscription!
161
- subscription.modify_ack_deadline new_deadline, ack_id
273
+ if subscription.respond_to?(:exactly_once_delivery_enabled) && subscription.exactly_once_delivery_enabled
274
+ subscription.modify_ack_deadline new_deadline, ack_id, &block
275
+ else
276
+ subscription.modify_ack_deadline new_deadline, ack_id
277
+ yield AcknowledgeResult.new(AcknowledgeResult::SUCCESS) if block_given?
278
+ end
162
279
  end
163
280
 
164
281
  ##
@@ -167,6 +284,30 @@ module Google
167
284
  #
168
285
  # This will make the message available for redelivery.
169
286
  #
287
+ # @yield [callback] The block to be called when reject operation is done.
288
+ # @yieldparam [Google::Cloud::PubSub::AcknowledgeResult] Result object that contains the status and error.
289
+ #
290
+ # @example
291
+ # require "google/cloud/pubsub"
292
+ #
293
+ # pubsub = Google::Cloud::PubSub.new
294
+ #
295
+ # sub = pubsub.subscription "my-topic-sub"
296
+ # subscriber = sub.listen do |received_message|
297
+ # puts received_message.message.data
298
+ #
299
+ # # Release message back to the API.
300
+ # received_message.reject! do |result|
301
+ # puts result.status
302
+ # end
303
+ # end
304
+ #
305
+ # # Start background threads that will call block passed to listen.
306
+ # subscriber.start
307
+ #
308
+ # # Shut down the subscriber when ready to stop receiving messages.
309
+ # subscriber.stop!
310
+ #
170
311
  # @example
171
312
  # require "google/cloud/pubsub"
172
313
  #
@@ -184,14 +325,34 @@ module Google
184
325
  # subscriber.start
185
326
  #
186
327
  # # Shut down the subscriber when ready to stop receiving messages.
187
- # subscriber.stop.wait!
328
+ # subscriber.stop!
188
329
  #
189
- def reject!
190
- modify_ack_deadline! 0
330
+ def reject! &block
331
+ modify_ack_deadline! 0, &block
191
332
  end
192
333
  alias nack! reject!
193
334
  alias ignore! reject!
194
335
 
336
+ # @private
337
+ def hash
338
+ @grpc.hash
339
+ end
340
+
341
+ # @private
342
+ def eql? other
343
+ return false unless other.is_a? self.class
344
+ @grpc.hash == other.hash
345
+ end
346
+ # @private
347
+ alias == eql?
348
+
349
+ # @private
350
+ def <=> other
351
+ return nil unless other.is_a? self.class
352
+ other_grpc = other.instance_variable_get :@grpc
353
+ @grpc <=> other_grpc
354
+ end
355
+
195
356
  ##
196
357
  # @private New ReceivedMessage from a
197
358
  # Google::Cloud::PubSub::V1::ReceivedMessage object.
@@ -0,0 +1,88 @@
1
+ # Copyright 2016 Google LLC
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # https://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+
16
+ require "google/cloud/errors"
17
+
18
+ module Google
19
+ module Cloud
20
+ module PubSub
21
+ ##
22
+ # # RetryPolicy
23
+ #
24
+ # An immutable Retry Policy value object that specifies how Cloud Pub/Sub retries message delivery.
25
+ #
26
+ # Retry delay will be exponential based on provided minimum and maximum backoffs. (See [Exponential
27
+ # backoff](https://en.wikipedia.org/wiki/Exponential_backoff).)
28
+ #
29
+ # Retry Policy will be triggered on NACKs or acknowledgement deadline exceeded events for a given message.
30
+ #
31
+ # Retry Policy is implemented on a best effort basis. At times, the delay between consecutive deliveries may not
32
+ # match the configuration. That is, delay can be more or less than configured backoff.
33
+ #
34
+ # @attr [Numeric] minimum_backoff The minimum delay between consecutive deliveries of a given message. Value
35
+ # should be between 0 and 600 seconds. The default value is 10 seconds.
36
+ # @attr [Numeric] maximum_backoff The maximum delay between consecutive deliveries of a given message. Value
37
+ # should be between 0 and 600 seconds. The default value is 600 seconds.
38
+ #
39
+ # @example
40
+ # require "google/cloud/pubsub"
41
+ #
42
+ # pubsub = Google::Cloud::PubSub.new
43
+ #
44
+ # sub = pubsub.subscription "my-topic-sub"
45
+ #
46
+ # sub.retry_policy = Google::Cloud::PubSub::RetryPolicy.new minimum_backoff: 5, maximum_backoff: 300
47
+ #
48
+ # sub.retry_policy.minimum_backoff #=> 5
49
+ # sub.retry_policy.maximum_backoff #=> 300
50
+ #
51
+ class RetryPolicy
52
+ attr_reader :minimum_backoff
53
+ attr_reader :maximum_backoff
54
+
55
+ ##
56
+ # Creates a new, immutable RetryPolicy value object.
57
+ #
58
+ # @attr [Numeric, nil] minimum_backoff The minimum delay between consecutive deliveries of a given message.
59
+ # Value should be between 0 and 600 seconds. If `nil` is provided, the default value is 10 seconds.
60
+ # @attr [Numeric, nil] maximum_backoff The maximum delay between consecutive deliveries of a given message.
61
+ # Value should be between 0 and 600 seconds. If `nil` is provided, the default value is 600 seconds.
62
+ #
63
+ def initialize minimum_backoff: nil, maximum_backoff: nil
64
+ @minimum_backoff = minimum_backoff
65
+ @maximum_backoff = maximum_backoff
66
+ end
67
+
68
+ ##
69
+ # @private Convert the RetryPolicy to a Google::Cloud::PubSub::V1::RetryPolicy object.
70
+ def to_grpc
71
+ Google::Cloud::PubSub::V1::RetryPolicy.new(
72
+ minimum_backoff: Convert.number_to_duration(minimum_backoff),
73
+ maximum_backoff: Convert.number_to_duration(maximum_backoff)
74
+ )
75
+ end
76
+
77
+ ##
78
+ # @private New RetryPolicy from a Google::Cloud::PubSub::V1::RetryPolicy object.
79
+ def self.from_grpc grpc
80
+ new(
81
+ minimum_backoff: Convert.duration_to_number(grpc.minimum_backoff),
82
+ maximum_backoff: Convert.duration_to_number(grpc.maximum_backoff)
83
+ )
84
+ end
85
+ end
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,180 @@
1
+ # Copyright 2021 Google LLC
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # https://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+
16
+ require "delegate"
17
+
18
+ module Google
19
+ module Cloud
20
+ module PubSub
21
+ class Schema
22
+ ##
23
+ # Schema::List is a special case Array with additional values.
24
+ class List < DelegateClass(::Array)
25
+ ##
26
+ # If not empty, indicates that there are more schemas
27
+ # that match the request and this value should be passed to
28
+ # the next {Google::Cloud::PubSub::Project#schemas} to continue.
29
+ attr_accessor :token
30
+
31
+ ##
32
+ # @private Create a new Schema::List with an array of values.
33
+ def initialize arr = []
34
+ @prefix = nil
35
+ @token = nil
36
+ @view = nil
37
+ @max = nil
38
+ super arr
39
+ end
40
+
41
+ ##
42
+ # Whether there a next page of schemas.
43
+ #
44
+ # @return [Boolean]
45
+ #
46
+ # @example
47
+ # require "google/cloud/pubsub"
48
+ #
49
+ # pubsub = Google::Cloud::PubSub.new
50
+ #
51
+ # schemas = pubsub.schemas
52
+ # if schemas.next?
53
+ # next_schemas = schemas.next
54
+ # end
55
+ #
56
+ def next?
57
+ !token.nil?
58
+ end
59
+
60
+ ##
61
+ # Retrieve the next page of schemas.
62
+ #
63
+ # @return [Schema::List]
64
+ #
65
+ # @example
66
+ # require "google/cloud/pubsub"
67
+ #
68
+ # pubsub = Google::Cloud::PubSub.new
69
+ #
70
+ # schemas = pubsub.schemas
71
+ # if schemas.next?
72
+ # next_schemas = schemas.next
73
+ # end
74
+ #
75
+ def next
76
+ return nil unless next?
77
+ ensure_service!
78
+ next_schemas
79
+ end
80
+
81
+ ##
82
+ # Retrieves remaining results by repeatedly invoking {#next} until
83
+ # {#next?} returns `false`. Calls the given block once for each
84
+ # result, which is passed as the argument to the block.
85
+ #
86
+ # An Enumerator is returned if no block is given.
87
+ #
88
+ # This method will make repeated API calls until all remaining results
89
+ # are retrieved. (Unlike `#each`, for example, which merely iterates
90
+ # over the results returned by a single API call.) Use with caution.
91
+ #
92
+ # @param [Integer] request_limit The upper limit of API requests to
93
+ # make to load all schemas. Default is no limit.
94
+ # @yield [schema] The block for accessing each schema.
95
+ # @yieldparam [Schema] schema The schema object.
96
+ #
97
+ # @return [Enumerator]
98
+ #
99
+ # @example Iterating each schema by passing a block:
100
+ # require "google/cloud/pubsub"
101
+ #
102
+ # pubsub = Google::Cloud::PubSub.new
103
+ #
104
+ # schemas = pubsub.schemas
105
+ # schemas.all do |schema|
106
+ # puts schema.name
107
+ # end
108
+ #
109
+ # @example Using the enumerator by not passing a block:
110
+ # require "google/cloud/pubsub"
111
+ #
112
+ # pubsub = Google::Cloud::PubSub.new
113
+ #
114
+ # schemas = pubsub.schemas
115
+ # all_names = schemas.all.map do |schema|
116
+ # schema.name
117
+ # end
118
+ #
119
+ # @example Limit the number of API calls made:
120
+ # require "google/cloud/pubsub"
121
+ #
122
+ # pubsub = Google::Cloud::PubSub.new
123
+ #
124
+ # schemas = pubsub.schemas
125
+ # schemas.all(request_limit: 10) do |schema|
126
+ # puts schema.name
127
+ # end
128
+ #
129
+ def all request_limit: nil, &block
130
+ request_limit = request_limit.to_i if request_limit
131
+ return enum_for :all, request_limit: request_limit unless block_given?
132
+ results = self
133
+ loop do
134
+ results.each(&block)
135
+ if request_limit
136
+ request_limit -= 1
137
+ break if request_limit.negative?
138
+ end
139
+ break unless results.next?
140
+ results = results.next
141
+ end
142
+ end
143
+
144
+ ##
145
+ # @private New Schemas::List from a
146
+ # Google::Cloud::PubSub::V1::ListSchemasRequest object.
147
+ def self.from_grpc grpc_list, service, view, max = nil
148
+ subs = new(Array(grpc_list.schemas).map do |grpc|
149
+ Schema.from_grpc grpc, service
150
+ end)
151
+ token = grpc_list.next_page_token
152
+ token = nil if token == "".freeze
153
+ subs.instance_variable_set :@token, token
154
+ subs.instance_variable_set :@service, service
155
+ subs.instance_variable_set :@view, view
156
+ subs.instance_variable_set :@max, max
157
+ subs
158
+ end
159
+
160
+ protected
161
+
162
+ ##
163
+ # @private Raise an error unless an active connection to the service
164
+ # is available.
165
+ def ensure_service!
166
+ raise "Must have active connection to service" unless @service
167
+ end
168
+
169
+ def next_schemas
170
+ options = { prefix: @prefix, token: @token, max: @max }
171
+ grpc = @service.list_schemas @view, options
172
+ self.class.from_grpc grpc, @service, @view, @max
173
+ end
174
+ end
175
+ end
176
+ end
177
+
178
+ Pubsub = PubSub unless const_defined? :Pubsub
179
+ end
180
+ end