google-cloud-pubsub 0.20.0 → 2.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (49) hide show
  1. checksums.yaml +5 -5
  2. data/.yardopts +18 -0
  3. data/AUTHENTICATION.md +178 -0
  4. data/CHANGELOG.md +659 -0
  5. data/CODE_OF_CONDUCT.md +40 -0
  6. data/CONTRIBUTING.md +187 -0
  7. data/EMULATOR.md +37 -0
  8. data/LICENSE +201 -0
  9. data/LOGGING.md +32 -0
  10. data/OVERVIEW.md +528 -0
  11. data/TROUBLESHOOTING.md +31 -0
  12. data/lib/google/cloud/pubsub/async_publisher/batch.rb +310 -0
  13. data/lib/google/cloud/pubsub/async_publisher.rb +402 -0
  14. data/lib/google/cloud/pubsub/batch_publisher.rb +100 -0
  15. data/lib/google/cloud/pubsub/convert.rb +91 -0
  16. data/lib/google/cloud/pubsub/credentials.rb +26 -10
  17. data/lib/google/cloud/pubsub/errors.rb +85 -0
  18. data/lib/google/cloud/pubsub/message.rb +82 -20
  19. data/lib/google/cloud/pubsub/policy.rb +40 -61
  20. data/lib/google/cloud/pubsub/project.rb +405 -265
  21. data/lib/google/cloud/pubsub/publish_result.rb +103 -0
  22. data/lib/google/cloud/pubsub/received_message.rb +165 -30
  23. data/lib/google/cloud/pubsub/retry_policy.rb +88 -0
  24. data/lib/google/cloud/pubsub/schema/list.rb +180 -0
  25. data/lib/google/cloud/pubsub/schema.rb +310 -0
  26. data/lib/google/cloud/pubsub/service.rb +304 -162
  27. data/lib/google/cloud/pubsub/snapshot/list.rb +178 -0
  28. data/lib/google/cloud/pubsub/snapshot.rb +205 -0
  29. data/lib/google/cloud/pubsub/subscriber/enumerator_queue.rb +54 -0
  30. data/lib/google/cloud/pubsub/subscriber/inventory.rb +173 -0
  31. data/lib/google/cloud/pubsub/subscriber/sequencer.rb +115 -0
  32. data/lib/google/cloud/pubsub/subscriber/stream.rb +400 -0
  33. data/lib/google/cloud/pubsub/subscriber/timed_unary_buffer.rb +230 -0
  34. data/lib/google/cloud/pubsub/subscriber.rb +417 -0
  35. data/lib/google/cloud/pubsub/subscription/list.rb +38 -43
  36. data/lib/google/cloud/pubsub/subscription/push_config.rb +268 -0
  37. data/lib/google/cloud/pubsub/subscription.rb +1040 -210
  38. data/lib/google/cloud/pubsub/topic/list.rb +32 -37
  39. data/lib/google/cloud/pubsub/topic.rb +726 -177
  40. data/lib/google/cloud/pubsub/version.rb +6 -4
  41. data/lib/google/cloud/pubsub.rb +138 -413
  42. data/lib/google-cloud-pubsub.rb +60 -42
  43. metadata +88 -39
  44. data/lib/google/cloud/pubsub/topic/publisher.rb +0 -87
  45. data/lib/google/iam/v1/iam_policy.rb +0 -33
  46. data/lib/google/iam/v1/iam_policy_services.rb +0 -30
  47. data/lib/google/iam/v1/policy.rb +0 -25
  48. data/lib/google/pubsub/v1/pubsub_pb.rb +0 -129
  49. data/lib/google/pubsub/v1/pubsub_services_pb.rb +0 -117
@@ -0,0 +1,103 @@
1
+ # Copyright 2017 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
+ module Google
17
+ module Cloud
18
+ module PubSub
19
+ ##
20
+ # The result of a publish operation. The message object is available on
21
+ # {#message} and will have {#message_id} assigned by the API.
22
+ #
23
+ # When the publish operation was successful the result will be marked
24
+ # {#succeeded?}. Otherwise, the result will be marked {#failed?} and the
25
+ # error raised will be availabe on {#error}.
26
+ #
27
+ class PublishResult
28
+ ##
29
+ # @private Create an PublishResult object.
30
+ def initialize message, error = nil
31
+ @message = message
32
+ @error = error
33
+ end
34
+
35
+ ##
36
+ # The message.
37
+ def message
38
+ @message
39
+ end
40
+ alias msg message
41
+
42
+ ##
43
+ # The message's data.
44
+ def data
45
+ message.data
46
+ end
47
+
48
+ ##
49
+ # The message's attributes.
50
+ def attributes
51
+ message.attributes
52
+ end
53
+
54
+ ##
55
+ # The ID of the message, assigned by the server at publication
56
+ # time. Guaranteed to be unique within the topic.
57
+ def message_id
58
+ message.message_id
59
+ end
60
+ alias msg_id message_id
61
+
62
+ ##
63
+ # The time at which the message was published.
64
+ def published_at
65
+ message.published_at
66
+ end
67
+ alias publish_time published_at
68
+
69
+ ##
70
+ # The error that was raised when published, if any.
71
+ def error
72
+ @error
73
+ end
74
+
75
+ ##
76
+ # Whether the publish request was successful.
77
+ def succeeded?
78
+ error.nil?
79
+ end
80
+
81
+ # Whether the publish request failed.
82
+ def failed?
83
+ !succeeded?
84
+ end
85
+
86
+ ##
87
+ # @private Create an PublishResult object from a message protobuf.
88
+ def self.from_grpc msg_grpc
89
+ new Message.from_grpc(msg_grpc)
90
+ end
91
+
92
+ ##
93
+ # @private Create an PublishResult object from a message protobuf and an
94
+ # error.
95
+ def self.from_error msg_grpc, error
96
+ new Message.from_grpc(msg_grpc), error
97
+ end
98
+ end
99
+ end
100
+
101
+ Pubsub = PubSub unless const_defined? :Pubsub
102
+ end
103
+ end
@@ -1,10 +1,10 @@
1
- # Copyright 2015 Google Inc. All rights reserved.
1
+ # Copyright 2015 Google LLC
2
2
  #
3
3
  # Licensed under the Apache License, Version 2.0 (the "License");
4
4
  # you may not use this file except in compliance with the License.
5
5
  # You may obtain a copy of the License at
6
6
  #
7
- # http://www.apache.org/licenses/LICENSE-2.0
7
+ # https://www.apache.org/licenses/LICENSE-2.0
8
8
  #
9
9
  # Unless required by applicable law or agreed to in writing, software
10
10
  # distributed under the License is distributed on an "AS IS" BASIS,
@@ -18,39 +18,43 @@ require "google/cloud/pubsub/message"
18
18
 
19
19
  module Google
20
20
  module Cloud
21
- module Pubsub
21
+ module PubSub
22
22
  ##
23
23
  # # ReceivedMessage
24
24
  #
25
25
  # Represents a Pub/Sub {Message} that can be acknowledged or delayed.
26
26
  #
27
27
  # @example
28
- # require "google/cloud"
28
+ # require "google/cloud/pubsub"
29
29
  #
30
- # gcloud = Google::Cloud.new
31
- # pubsub = gcloud.pubsub
30
+ # pubsub = Google::Cloud::PubSub.new
32
31
  #
33
32
  # sub = pubsub.subscription "my-topic-sub"
34
- # received_message = sub.pull.first
35
- # if received_message
33
+ # subscriber = sub.listen do |received_message|
36
34
  # puts received_message.message.data
37
35
  # received_message.acknowledge!
38
36
  # end
39
37
  #
38
+ # # Start background threads that will call the block passed to listen.
39
+ # subscriber.start
40
+ #
41
+ # # Shut down the subscriber when ready to stop receiving messages.
42
+ # subscriber.stop!
43
+ #
40
44
  class ReceivedMessage
41
45
  ##
42
46
  # @private The {Subscription} object.
43
47
  attr_accessor :subscription
44
48
 
45
49
  ##
46
- # @private The gRPC Google::Pubsub::V1::ReceivedMessage object.
50
+ # @private The gRPC Google::Cloud::PubSub::V1::ReceivedMessage object.
47
51
  attr_accessor :grpc
48
52
 
49
53
  ##
50
54
  # @private Create an empty {Subscription} object.
51
55
  def initialize
52
56
  @subscription = nil
53
- @grpc = Google::Pubsub::V1::ReceivedMessage.new
57
+ @grpc = Google::Cloud::PubSub::V1::ReceivedMessage.new
54
58
  end
55
59
 
56
60
  ##
@@ -59,21 +63,60 @@ module Google
59
63
  @grpc.ack_id
60
64
  end
61
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
+
62
104
  ##
63
105
  # The received message.
64
106
  def message
65
107
  Message.from_grpc @grpc.message
66
108
  end
67
- alias_method :msg, :message
109
+ alias msg message
68
110
 
69
111
  ##
70
- # The received message's data.
112
+ # The received message payload. This data is a list of bytes encoded as
113
+ # ASCII-8BIT.
71
114
  def data
72
115
  message.data
73
116
  end
74
117
 
75
118
  ##
76
- # The received message's attributes.
119
+ # Optional attributes for the received message.
77
120
  def attributes
78
121
  message.attributes
79
122
  end
@@ -84,29 +127,63 @@ module Google
84
127
  def message_id
85
128
  message.message_id
86
129
  end
87
- alias_method :msg_id, :message_id
130
+ alias msg_id message_id
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
+
154
+ ##
155
+ # The time at which the message was published.
156
+ def published_at
157
+ message.published_at
158
+ end
159
+ alias publish_time published_at
88
160
 
89
161
  ##
90
162
  # Acknowledges receipt of the message.
91
163
  #
92
164
  # @example
93
- # require "google/cloud"
165
+ # require "google/cloud/pubsub"
94
166
  #
95
- # gcloud = Google::Cloud.new
96
- # pubsub = gcloud.pubsub
167
+ # pubsub = Google::Cloud::PubSub.new
97
168
  #
98
169
  # sub = pubsub.subscription "my-topic-sub"
99
- # received_message = sub.pull.first
100
- # if received_message
170
+ # subscriber = sub.listen do |received_message|
101
171
  # puts received_message.message.data
172
+ #
102
173
  # received_message.acknowledge!
103
174
  # end
104
175
  #
176
+ # # Start background threads that will call block passed to listen.
177
+ # subscriber.start
178
+ #
179
+ # # Shut down the subscriber when ready to stop receiving messages.
180
+ # subscriber.stop!
181
+ #
105
182
  def acknowledge!
106
183
  ensure_subscription!
107
184
  subscription.acknowledge ack_id
108
185
  end
109
- alias_method :ack!, :acknowledge!
186
+ alias ack! acknowledge!
110
187
 
111
188
  ##
112
189
  # Modifies the acknowledge deadline for the message.
@@ -121,27 +198,83 @@ module Google
121
198
  # the message available for another pull request.
122
199
  #
123
200
  # @example
124
- # require "google/cloud"
201
+ # require "google/cloud/pubsub"
125
202
  #
126
- # gcloud = Google::Cloud.new
127
- # pubsub = gcloud.pubsub
203
+ # pubsub = Google::Cloud::PubSub.new
128
204
  #
129
205
  # sub = pubsub.subscription "my-topic-sub"
130
- # received_message = sub.pull.first
131
- # if received_message
206
+ # subscriber = sub.listen do |received_message|
132
207
  # puts received_message.message.data
208
+ #
133
209
  # # Delay for 2 minutes
134
- # received_message.delay! 120
210
+ # received_message.modify_ack_deadline! 120
135
211
  # end
136
212
  #
137
- def delay! new_deadline
213
+ # # Start background threads that will call block passed to listen.
214
+ # subscriber.start
215
+ #
216
+ # # Shut down the subscriber when ready to stop receiving messages.
217
+ # subscriber.stop!
218
+ #
219
+ def modify_ack_deadline! new_deadline
138
220
  ensure_subscription!
139
- subscription.delay new_deadline, ack_id
221
+ subscription.modify_ack_deadline new_deadline, ack_id
222
+ end
223
+
224
+ ##
225
+ # Resets the acknowledge deadline for the message without acknowledging
226
+ # it.
227
+ #
228
+ # This will make the message available for redelivery.
229
+ #
230
+ # @example
231
+ # require "google/cloud/pubsub"
232
+ #
233
+ # pubsub = Google::Cloud::PubSub.new
234
+ #
235
+ # sub = pubsub.subscription "my-topic-sub"
236
+ # subscriber = sub.listen do |received_message|
237
+ # puts received_message.message.data
238
+ #
239
+ # # Release message back to the API.
240
+ # received_message.reject!
241
+ # end
242
+ #
243
+ # # Start background threads that will call block passed to listen.
244
+ # subscriber.start
245
+ #
246
+ # # Shut down the subscriber when ready to stop receiving messages.
247
+ # subscriber.stop!
248
+ #
249
+ def reject!
250
+ modify_ack_deadline! 0
251
+ end
252
+ alias nack! reject!
253
+ alias ignore! reject!
254
+
255
+ # @private
256
+ def hash
257
+ @grpc.hash
258
+ end
259
+
260
+ # @private
261
+ def eql? other
262
+ return false unless other.is_a? self.class
263
+ @grpc.hash == other.hash
264
+ end
265
+ # @private
266
+ alias == eql?
267
+
268
+ # @private
269
+ def <=> other
270
+ return nil unless other.is_a? self.class
271
+ other_grpc = other.instance_variable_get :@grpc
272
+ @grpc <=> other_grpc
140
273
  end
141
274
 
142
275
  ##
143
276
  # @private New ReceivedMessage from a
144
- # Google::Pubsub::V1::ReceivedMessage object.
277
+ # Google::Cloud::PubSub::V1::ReceivedMessage object.
145
278
  def self.from_grpc grpc, subscription
146
279
  new.tap do |rm|
147
280
  rm.grpc = grpc
@@ -154,9 +287,11 @@ module Google
154
287
  ##
155
288
  # Raise an error unless an active subscription is available.
156
289
  def ensure_subscription!
157
- fail "Must have active subscription" unless subscription
290
+ raise "Must have active subscription" unless subscription
158
291
  end
159
292
  end
160
293
  end
294
+
295
+ Pubsub = PubSub unless const_defined? :Pubsub
161
296
  end
162
297
  end
@@ -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