google-cloud-pubsub 2.23.0 → 3.0.0

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 (34) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +18 -0
  3. data/OVERVIEW.md +188 -144
  4. data/lib/google/cloud/pubsub/admin_clients.rb +116 -0
  5. data/lib/google/cloud/pubsub/async_publisher.rb +9 -9
  6. data/lib/google/cloud/pubsub/batch_publisher.rb +4 -4
  7. data/lib/google/cloud/pubsub/errors.rb +3 -3
  8. data/lib/google/cloud/pubsub/message.rb +8 -8
  9. data/lib/google/cloud/pubsub/{subscriber → message_listener}/enumerator_queue.rb +1 -1
  10. data/lib/google/cloud/pubsub/{subscriber → message_listener}/inventory.rb +2 -4
  11. data/lib/google/cloud/pubsub/{subscriber → message_listener}/sequencer.rb +1 -1
  12. data/lib/google/cloud/pubsub/{subscriber → message_listener}/stream.rb +10 -10
  13. data/lib/google/cloud/pubsub/{subscriber → message_listener}/timed_unary_buffer.rb +1 -1
  14. data/lib/google/cloud/pubsub/message_listener.rb +413 -0
  15. data/lib/google/cloud/pubsub/project.rb +98 -531
  16. data/lib/google/cloud/pubsub/publisher.rb +373 -0
  17. data/lib/google/cloud/pubsub/received_message.rb +44 -39
  18. data/lib/google/cloud/pubsub/service.rb +24 -386
  19. data/lib/google/cloud/pubsub/subscriber.rb +442 -279
  20. data/lib/google/cloud/pubsub/version.rb +1 -1
  21. data/lib/google/cloud/pubsub.rb +8 -15
  22. data/lib/google-cloud-pubsub.rb +5 -4
  23. metadata +9 -17
  24. data/lib/google/cloud/pubsub/policy.rb +0 -188
  25. data/lib/google/cloud/pubsub/retry_policy.rb +0 -88
  26. data/lib/google/cloud/pubsub/schema/list.rb +0 -180
  27. data/lib/google/cloud/pubsub/schema.rb +0 -378
  28. data/lib/google/cloud/pubsub/snapshot/list.rb +0 -178
  29. data/lib/google/cloud/pubsub/snapshot.rb +0 -205
  30. data/lib/google/cloud/pubsub/subscription/list.rb +0 -205
  31. data/lib/google/cloud/pubsub/subscription/push_config.rb +0 -268
  32. data/lib/google/cloud/pubsub/subscription.rb +0 -1467
  33. data/lib/google/cloud/pubsub/topic/list.rb +0 -171
  34. data/lib/google/cloud/pubsub/topic.rb +0 -1100
@@ -1,1467 +0,0 @@
1
- # Copyright 2015 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/pubsub/convert"
17
- require "google/cloud/errors"
18
- require "google/cloud/pubsub/subscription/list"
19
- require "google/cloud/pubsub/subscription/push_config"
20
- require "google/cloud/pubsub/received_message"
21
- require "google/cloud/pubsub/retry_policy"
22
- require "google/cloud/pubsub/snapshot"
23
- require "google/cloud/pubsub/subscriber"
24
- require "google/cloud/pubsub/v1"
25
-
26
- module Google
27
- module Cloud
28
- module PubSub
29
- ##
30
- # # Subscription
31
- #
32
- # A named resource representing the stream of messages from a single,
33
- # specific {Topic}, to be delivered to the subscribing application.
34
- #
35
- # @example
36
- # require "google/cloud/pubsub"
37
- #
38
- # pubsub = Google::Cloud::PubSub.new
39
- #
40
- # sub = pubsub.subscription "my-topic-sub"
41
- # subscriber = sub.listen do |received_message|
42
- # # process message
43
- # received_message.acknowledge!
44
- # end
45
- #
46
- # # Handle exceptions from listener
47
- # subscriber.on_error do |exception|
48
- # puts "Exception: #{exception.class} #{exception.message}"
49
- # end
50
- #
51
- # # Gracefully shut down the subscriber
52
- # at_exit do
53
- # subscriber.stop!
54
- # end
55
- #
56
- # # Start background threads that will call the block passed to listen.
57
- # subscriber.start
58
- # sleep
59
- class Subscription
60
- ##
61
- # @private The Service object.
62
- attr_accessor :service
63
-
64
- ##
65
- # @private The gRPC Google::Cloud::PubSub::V1::Subscription object.
66
- attr_accessor :grpc
67
-
68
- ##
69
- # @private Create an empty {Subscription} object.
70
- def initialize
71
- @service = nil
72
- @grpc = nil
73
- @resource_name = nil
74
- @exists = nil
75
- end
76
-
77
- ##
78
- # The name of the subscription.
79
- #
80
- # @return [String] A fully-qualified subscription name in the form
81
- # `projects/{project_id}/subscriptions/{subscription_id}`.
82
- #
83
- def name
84
- return @resource_name if reference?
85
- @grpc.name
86
- end
87
-
88
- ##
89
- # The {Topic} from which this subscription receives messages.
90
- #
91
- # Makes an API call to retrieve the topic information when called on a
92
- # reference object. See {#reference?}.
93
- #
94
- # @return [Topic]
95
- #
96
- # @example
97
- # require "google/cloud/pubsub"
98
- #
99
- # pubsub = Google::Cloud::PubSub.new
100
- #
101
- # sub = pubsub.subscription "my-topic-sub"
102
- # sub.topic.name #=> "projects/my-project/topics/my-topic"
103
- #
104
- def topic
105
- ensure_grpc!
106
- Topic.from_name @grpc.topic, service
107
- end
108
-
109
- ##
110
- # This value is the maximum number of seconds after a subscriber
111
- # receives a message before the subscriber should acknowledge the
112
- # message.
113
- #
114
- # Makes an API call to retrieve the deadline value when called on a
115
- # reference object. See {#reference?}.
116
- #
117
- # @return [Integer]
118
- def deadline
119
- ensure_grpc!
120
- @grpc.ack_deadline_seconds
121
- end
122
-
123
- ##
124
- # Sets the maximum number of seconds after a subscriber
125
- # receives a message before the subscriber should acknowledge the
126
- # message.
127
- #
128
- # @param [Integer] new_deadline The new deadline value.
129
- #
130
- def deadline= new_deadline
131
- update_grpc = Google::Cloud::PubSub::V1::Subscription.new name: name, ack_deadline_seconds: new_deadline
132
- @grpc = service.update_subscription update_grpc, :ack_deadline_seconds
133
- @resource_name = nil
134
- end
135
-
136
- ##
137
- # Indicates whether to retain acknowledged messages. If `true`, then
138
- # messages are not expunged from the subscription's backlog, even if
139
- # they are acknowledged, until they fall out of the {#retention} window.
140
- # Default is `false`.
141
- #
142
- # Makes an API call to retrieve the retain_acked value when called on a
143
- # reference object. See {#reference?}.
144
- #
145
- # @return [Boolean] Returns `true` if acknowledged messages are
146
- # retained.
147
- #
148
- def retain_acked
149
- ensure_grpc!
150
- @grpc.retain_acked_messages
151
- end
152
-
153
- ##
154
- # Sets whether to retain acknowledged messages.
155
- #
156
- # @param [Boolean] new_retain_acked The new retain acknowledged messages
157
- # value.
158
- #
159
- def retain_acked= new_retain_acked
160
- update_grpc = Google::Cloud::PubSub::V1::Subscription.new name: name,
161
- retain_acked_messages: !(!new_retain_acked)
162
- @grpc = service.update_subscription update_grpc, :retain_acked_messages
163
- @resource_name = nil
164
- end
165
-
166
- ##
167
- # How long to retain unacknowledged messages in the subscription's
168
- # backlog, from the moment a message is published. If
169
- # {#retain_acked} is `true`, then this also configures the retention of
170
- # acknowledged messages, and thus configures how far back in time a
171
- # {#seek} can be done. Cannot be less than 600 (10 minutes) or more
172
- # than 604,800 (7 days). Default is 604,800 seconds (7 days).
173
- #
174
- # Makes an API call to retrieve the retention value when called on a
175
- # reference object. See {#reference?}.
176
- #
177
- # @return [Numeric] The message retention duration in seconds.
178
- #
179
- def retention
180
- ensure_grpc!
181
- Convert.duration_to_number @grpc.message_retention_duration
182
- end
183
-
184
- ##
185
- # Sets the message retention duration in seconds.
186
- #
187
- # @param [Numeric] new_retention The new retention value.
188
- #
189
- def retention= new_retention
190
- new_retention_duration = Convert.number_to_duration new_retention
191
- update_grpc = Google::Cloud::PubSub::V1::Subscription.new name: name,
192
- message_retention_duration: new_retention_duration
193
- @grpc = service.update_subscription update_grpc, :message_retention_duration
194
- @resource_name = nil
195
- end
196
-
197
- ##
198
- # Indicates the minimum duration for which a message is retained after
199
- # it is published to the subscription's topic. If this field is set,
200
- # messages published to the subscription's topic in the last
201
- # `topic_message_retention_duration` are always available to subscribers.
202
- # Output only. See {Topic#retention}.
203
- #
204
- # Makes an API call to retrieve the retention value when called on a
205
- # reference object. See {#reference?}.
206
- #
207
- # @return [Numeric, nil] The topic message retention duration in seconds,
208
- # or `nil` if not set.
209
- #
210
- def topic_retention
211
- ensure_grpc!
212
- Convert.duration_to_number @grpc.topic_message_retention_duration
213
- end
214
-
215
- ##
216
- # Returns the URL locating the endpoint to which messages should be
217
- # pushed. For example, a Webhook endpoint might use
218
- # `https://example.com/push`.
219
- #
220
- # Makes an API call to retrieve the endpoint value when called on a
221
- # reference object. See {#reference?}.
222
- #
223
- # @return [String]
224
- #
225
- def endpoint
226
- ensure_grpc!
227
- @grpc.push_config&.push_endpoint
228
- end
229
-
230
- ##
231
- # Sets the URL locating the endpoint to which messages should be pushed.
232
- # For example, a Webhook endpoint might use `https://example.com/push`.
233
- #
234
- # @param [String] new_endpoint The new endpoint value.
235
- #
236
- def endpoint= new_endpoint
237
- ensure_service!
238
- service.modify_push_config name, new_endpoint, {}
239
-
240
- return if reference?
241
-
242
- @grpc.push_config = Google::Cloud::PubSub::V1::PushConfig.new(
243
- push_endpoint: new_endpoint,
244
- attributes: {}
245
- )
246
- end
247
-
248
- ##
249
- # Inspect the Subscription's push configuration settings. The
250
- # configuration can be changed by modifying the values in the method's
251
- # block.
252
- #
253
- # Subscription objects that are reference only will return an empty
254
- # {Subscription::PushConfig} object, which can be configured and saved
255
- # using the method's block. Unlike {#endpoint}, which will retrieve the
256
- # full resource from the API before returning. To get the actual values
257
- # for a reference object, call {#reload!} before calling {#push_config}.
258
- #
259
- # @yield [push_config] a block for modifying the push configuration
260
- # @yieldparam [Subscription::PushConfig] push_config the push
261
- # configuration
262
- #
263
- # @return [Subscription::PushConfig]
264
- #
265
- # @example
266
- # require "google/cloud/pubsub"
267
- #
268
- # pubsub = Google::Cloud::PubSub.new
269
- #
270
- # sub = pubsub.subscription "my-topic-sub"
271
- # sub.push_config.endpoint #=> "http://example.com/callback"
272
- # sub.push_config.authentication.email #=> "user@example.com"
273
- # sub.push_config.authentication.audience #=> "client-12345"
274
- #
275
- # @example Update the push configuration by passing a block:
276
- # require "google/cloud/pubsub"
277
- #
278
- # pubsub = Google::Cloud::PubSub.new
279
- # sub = pubsub.subscription "my-subscription"
280
- #
281
- # sub.push_config do |pc|
282
- # pc.endpoint = "http://example.net/callback"
283
- # pc.set_oidc_token "user@example.net", "client-67890"
284
- # end
285
- #
286
- def push_config
287
- ensure_service!
288
-
289
- orig_config = reference? ? nil : @grpc.push_config
290
- config = PushConfig.from_grpc orig_config
291
-
292
- if block_given?
293
- old_config = config.to_grpc.dup
294
- yield config
295
- new_config = config.to_grpc
296
-
297
- if old_config != new_config # has the object been changed?
298
- update_grpc = Google::Cloud::PubSub::V1::Subscription.new name: name, push_config: new_config
299
- @grpc = service.update_subscription update_grpc, :push_config
300
- end
301
- end
302
-
303
- config.freeze
304
- end
305
-
306
- ##
307
- # Inspect the Subscription's bigquery configuration settings. The
308
- # configuration can be changed by modifying the values in the method's
309
- # block.
310
- #
311
- # @yield [bigquery_config] a block for modifying the bigquery configuration
312
- # @yieldparam [Google::Cloud::PubSub::V1::BigQueryConfig] bigquery_config
313
- #
314
- # @return [Google::Cloud::PubSub::V1::BigQueryConfig]
315
- #
316
- # @example
317
- # require "google/cloud/pubsub"
318
- #
319
- # pubsub = Google::Cloud::PubSub.new
320
- #
321
- # sub = pubsub.subscription "my-topic-sub"
322
- # sub.bigquery_config.table #=> "my-project:dataset-id.table-id"
323
- # sub.bigquery_config.use_topic_schema #=> true
324
- # sub.bigquery_config.write_metadata #=> false
325
- #
326
- # @example Update the bigquery configuration by passing a block:
327
- # require "google/cloud/pubsub"
328
- #
329
- # pubsub = Google::Cloud::PubSub.new
330
- # sub = pubsub.subscription "my-subscription"
331
- #
332
- # sub.bigquery_config do |bc|
333
- # bc.write_metadata = true
334
- # bc.use_topic_schema = false
335
- # end
336
- #
337
- def bigquery_config
338
- ensure_service!
339
-
340
- config = reference? ? Google::Cloud::PubSub::V1::BigQueryConfig.new : @grpc.bigquery_config
341
-
342
- if block_given?
343
- old_config = config.dup
344
- yield config
345
- new_config = config
346
-
347
- if old_config != new_config # has the object been changed?
348
- update_grpc = Google::Cloud::PubSub::V1::Subscription.new name: name, bigquery_config: new_config
349
- @grpc = service.update_subscription update_grpc, :bigquery_config
350
- end
351
- end
352
-
353
- config.freeze
354
- end
355
-
356
- ##
357
- # A hash of user-provided labels associated with this subscription.
358
- # Labels can be used to organize and group subscriptions.See [Creating
359
- # and Managing Labels](https://cloud.google.com/pubsub/docs/labels).
360
- #
361
- # The returned hash is frozen and changes are not allowed. Use
362
- # {#labels=} to update the labels for this subscription.
363
- #
364
- # Makes an API call to retrieve the labels value when called on a
365
- # reference object. See {#reference?}.
366
- #
367
- # @return [Hash] The frozen labels hash.
368
- #
369
- def labels
370
- ensure_grpc!
371
- @grpc.labels.to_h.freeze
372
- end
373
-
374
- ##
375
- # Sets the hash of user-provided labels associated with this
376
- # subscription. Labels can be used to organize and group subscriptions.
377
- # Label keys and values can be no longer than 63 characters, can only
378
- # contain lowercase letters, numeric characters, underscores and dashes.
379
- # International characters are allowed. Label values are optional. Label
380
- # keys must start with a letter and each label in the list must have a
381
- # different key. See [Creating and Managing
382
- # Labels](https://cloud.google.com/pubsub/docs/labels).
383
- #
384
- # @param [Hash] new_labels The new labels hash.
385
- #
386
- def labels= new_labels
387
- raise ArgumentError, "Value must be a Hash" if new_labels.nil?
388
- update_grpc = Google::Cloud::PubSub::V1::Subscription.new name: name, labels: new_labels
389
- @grpc = service.update_subscription update_grpc, :labels
390
- @resource_name = nil
391
- end
392
-
393
- ##
394
- # The duration (in seconds) for when a subscription expires after the
395
- # subscription goes inactive. A subscription is considered active as
396
- # long as any connected subscriber is successfully consuming messages
397
- # from the subscription or is issuing operations on the subscription.
398
- #
399
- # If {#expires_in=} is not set, a *default* value of of 31 days will be
400
- # used. The minimum allowed value is 1 day.
401
- #
402
- # Makes an API call to retrieve the expires_in value when called on a
403
- # reference object. See {#reference?}.
404
- #
405
- # @return [Numeric, nil] The expiration duration, or `nil` if unset.
406
- #
407
- def expires_in
408
- ensure_grpc!
409
-
410
- return nil if @grpc.expiration_policy.nil?
411
-
412
- Convert.duration_to_number @grpc.expiration_policy.ttl
413
- end
414
-
415
- ##
416
- # Sets the duration (in seconds) for when a subscription expires after
417
- # the subscription goes inactive.
418
- #
419
- # See also {#expires_in}.
420
- #
421
- # @param [Numeric, nil] ttl The expiration duration in seconds, or `nil`
422
- # to unset.
423
- #
424
- def expires_in= ttl
425
- new_expiration_policy = Google::Cloud::PubSub::V1::ExpirationPolicy.new ttl: Convert.number_to_duration(ttl)
426
-
427
- update_grpc = Google::Cloud::PubSub::V1::Subscription.new name: name, expiration_policy: new_expiration_policy
428
- @grpc = service.update_subscription update_grpc, :expiration_policy
429
- @resource_name = nil
430
- end
431
-
432
- ##
433
- # An expression written in the Cloud Pub/Sub filter language. If non-empty, then only {Message} instances whose
434
- # `attributes` field matches the filter are delivered on this subscription. If empty, then no messages are
435
- # filtered out.
436
- #
437
- # Makes an API call to retrieve the filter value when called on a reference
438
- # object. See {#reference?}.
439
- #
440
- # @return [String] The frozen filter string.
441
- #
442
- def filter
443
- ensure_grpc!
444
- @grpc.filter.freeze
445
- end
446
-
447
- ##
448
- # Returns the {Topic} to which dead letter messages should be published if a dead letter policy is configured,
449
- # otherwise `nil`. Dead lettering is done on a best effort basis. The same message might be dead lettered
450
- # multiple times.
451
- #
452
- # See also {#dead_letter_topic=}, {#dead_letter_max_delivery_attempts=}, {#dead_letter_max_delivery_attempts}
453
- # and {#remove_dead_letter_policy}.
454
- #
455
- # Makes an API call to retrieve the topic name when called on a reference object. See {#reference?}.
456
- #
457
- # @return [Topic, nil]
458
- #
459
- # @example
460
- # require "google/cloud/pubsub"
461
- #
462
- # pubsub = Google::Cloud::PubSub.new
463
- #
464
- # sub = pubsub.subscription "my-topic-sub"
465
- # sub.dead_letter_topic.name #=> "projects/my-project/topics/my-dead-letter-topic"
466
- # sub.dead_letter_max_delivery_attempts #=> 10
467
- #
468
- def dead_letter_topic
469
- ensure_grpc!
470
- return nil unless @grpc.dead_letter_policy
471
- Topic.from_name @grpc.dead_letter_policy.dead_letter_topic, service
472
- end
473
-
474
- ##
475
- # Sets the {Topic} to which dead letter messages for the subscription should be published. Dead lettering is
476
- # done on a best effort basis. The same message might be dead lettered multiple times.
477
- # The Cloud Pub/Sub service account associated with the enclosing subscription's parent project (i.e.,
478
- # `service-{project_number}@gcp-sa-pubsub.iam.gserviceaccount.com`) must have permission to Publish() to this
479
- # topic.
480
- #
481
- # The operation will fail if the topic does not exist. Users should ensure that there is a subscription attached
482
- # to this topic since messages published to a topic with no subscriptions are lost.
483
- #
484
- # Makes an API call to retrieve the dead_letter_policy value when called on a
485
- # reference object. See {#reference?}.
486
- #
487
- # See also {#dead_letter_topic}, {#dead_letter_max_delivery_attempts=}, {#dead_letter_max_delivery_attempts}
488
- # and {#remove_dead_letter_policy}.
489
- #
490
- # @param [Topic] new_dead_letter_topic The topic to which dead letter messages for the subscription should be
491
- # published.
492
- #
493
- # @example
494
- # require "google/cloud/pubsub"
495
- #
496
- # pubsub = Google::Cloud::PubSub.new
497
- #
498
- # sub = pubsub.subscription "my-topic-sub"
499
- # dead_letter_topic = pubsub.topic "my-dead-letter-topic", skip_lookup: true
500
- # sub.dead_letter_topic = dead_letter_topic
501
- #
502
- def dead_letter_topic= new_dead_letter_topic
503
- ensure_grpc!
504
- dead_letter_policy = @grpc.dead_letter_policy || Google::Cloud::PubSub::V1::DeadLetterPolicy.new
505
- dead_letter_policy.dead_letter_topic = new_dead_letter_topic.name
506
- update_grpc = Google::Cloud::PubSub::V1::Subscription.new name: name, dead_letter_policy: dead_letter_policy
507
- @grpc = service.update_subscription update_grpc, :dead_letter_policy
508
- @resource_name = nil
509
- end
510
-
511
- ##
512
- # Returns the maximum number of delivery attempts for any message in the subscription's dead letter policy if a
513
- # dead letter policy is configured, otherwise `nil`. Dead lettering is done on a best effort basis. The same
514
- # message might be dead lettered multiple times. The value must be between 5 and 100.
515
- #
516
- # The number of delivery attempts is defined as 1 + (the sum of number of NACKs and number of times the
517
- # acknowledgement deadline has been exceeded for the message). A NACK is any call to ModifyAckDeadline with a 0
518
- # deadline. Note that client libraries may automatically extend ack_deadlines.
519
- #
520
- # This field will be honored on a best effort basis. If this parameter is `nil` or `0`, a default value of `5`
521
- # is used.
522
- #
523
- # See also {#dead_letter_max_delivery_attempts=}, {#dead_letter_topic=}, {#dead_letter_topic}
524
- # and {#remove_dead_letter_policy}.
525
- #
526
- # Makes an API call to retrieve the dead_letter_policy when called on a reference object. See {#reference?}.
527
- #
528
- # @return [Integer, nil] A value between `5` and `100`, or `nil` if no dead letter policy is configured.
529
- #
530
- # @example
531
- # require "google/cloud/pubsub"
532
- #
533
- # pubsub = Google::Cloud::PubSub.new
534
- #
535
- # sub = pubsub.subscription "my-topic-sub"
536
- # sub.dead_letter_topic.name #=> "projects/my-project/topics/my-dead-letter-topic"
537
- # sub.dead_letter_max_delivery_attempts #=> 10
538
- #
539
- def dead_letter_max_delivery_attempts
540
- ensure_grpc!
541
- @grpc.dead_letter_policy&.max_delivery_attempts
542
- end
543
-
544
- ##
545
- # Sets the maximum number of delivery attempts for any message in the subscription's dead letter policy.
546
- # Dead lettering is done on a best effort basis. The same message might be dead lettered multiple times.
547
- # The value must be between 5 and 100.
548
- #
549
- # The number of delivery attempts is defined as 1 + (the sum of number of NACKs and number of times the
550
- # acknowledgement deadline has been exceeded for the message). A NACK is any call to ModifyAckDeadline with a 0
551
- # deadline. Note that client libraries may automatically extend ack_deadlines.
552
- #
553
- # This field will be honored on a best effort basis. If this parameter is 0, a default value of 5 is used.
554
- #
555
- # Makes an API call to retrieve the dead_letter_policy when called on a reference object. See {#reference?}.
556
- #
557
- # The dead letter topic must be set first. See {#dead_letter_topic=}, {#dead_letter_topic} and
558
- # {#remove_dead_letter_policy}.
559
- #
560
- # @param [Integer, nil] new_dead_letter_max_delivery_attempts A value between 5 and 100. If this parameter is
561
- # `nil` or `0`, a default value of 5 is used.
562
- #
563
- # @raise [ArgumentError] if the dead letter topic has not been set. See {#dead_letter_topic=}.
564
- #
565
- # @example
566
- # require "google/cloud/pubsub"
567
- #
568
- # pubsub = Google::Cloud::PubSub.new
569
- #
570
- # sub = pubsub.subscription "my-topic-sub"
571
- # sub.dead_letter_topic.name #=> "projects/my-project/topics/my-dead-letter-topic"
572
- #
573
- # sub.dead_letter_max_delivery_attempts = 20
574
- #
575
- def dead_letter_max_delivery_attempts= new_dead_letter_max_delivery_attempts
576
- ensure_grpc!
577
- unless @grpc.dead_letter_policy&.dead_letter_topic
578
- # Service error message "3:Invalid resource name given (name=)." does not identify param.
579
- raise ArgumentError, "dead_letter_topic is required with dead_letter_max_delivery_attempts"
580
- end
581
- dead_letter_policy = @grpc.dead_letter_policy || Google::Cloud::PubSub::V1::DeadLetterPolicy.new
582
- dead_letter_policy.max_delivery_attempts = new_dead_letter_max_delivery_attempts
583
- update_grpc = Google::Cloud::PubSub::V1::Subscription.new name: name, dead_letter_policy: dead_letter_policy
584
- @grpc = service.update_subscription update_grpc, :dead_letter_policy
585
- @resource_name = nil
586
- end
587
-
588
- ##
589
- # Removes an existing dead letter policy. A dead letter policy specifies the conditions for dead lettering
590
- # messages in the subscription. If a dead letter policy is not set, dead lettering is disabled.
591
- #
592
- # Makes an API call to retrieve the dead_letter_policy when called on a reference object. See {#reference?}.
593
- #
594
- # See {#dead_letter_topic}, {#dead_letter_topic=}, {#dead_letter_max_delivery_attempts} and
595
- # {#dead_letter_max_delivery_attempts=}.
596
- #
597
- # @return [Boolean] `true` if an existing dead letter policy was removed, `false` if no existing dead letter
598
- # policy was present.
599
- #
600
- # @example
601
- # require "google/cloud/pubsub"
602
- #
603
- # pubsub = Google::Cloud::PubSub.new
604
- #
605
- # sub = pubsub.subscription "my-topic-sub"
606
- #
607
- # sub.dead_letter_topic.name #=> "projects/my-project/topics/my-dead-letter-topic"
608
- # sub.dead_letter_max_delivery_attempts #=> 10
609
- #
610
- # sub.remove_dead_letter_policy
611
- #
612
- # sub.dead_letter_topic #=> nil
613
- # sub.dead_letter_max_delivery_attempts #=> nil
614
- #
615
- def remove_dead_letter_policy
616
- ensure_grpc!
617
- return false if @grpc.dead_letter_policy.nil?
618
- update_grpc = Google::Cloud::PubSub::V1::Subscription.new name: name, dead_letter_policy: nil
619
- @grpc = service.update_subscription update_grpc, :dead_letter_policy
620
- true
621
- end
622
-
623
- ##
624
- # A policy that specifies how Cloud Pub/Sub retries message delivery for this subscription. If `nil`, the
625
- # default retry policy is applied. This generally implies that messages will be retried as soon as possible
626
- # for healthy subscribers. Retry Policy will be triggered on NACKs or acknowledgement deadline exceeded events
627
- # for a given message.
628
- #
629
- # Makes an API call to retrieve the retry_policy when called on a reference object. See {#reference?}.
630
- #
631
- # @return [RetryPolicy, nil] The retry policy for the subscription, or `nil`.
632
- #
633
- # @example
634
- # require "google/cloud/pubsub"
635
- #
636
- # pubsub = Google::Cloud::PubSub.new
637
- #
638
- # sub = pubsub.subscription "my-topic-sub"
639
- #
640
- # sub.retry_policy = Google::Cloud::PubSub::RetryPolicy.new minimum_backoff: 5, maximum_backoff: 300
641
- #
642
- # sub.retry_policy.minimum_backoff #=> 5
643
- # sub.retry_policy.maximum_backoff #=> 300
644
- #
645
- def retry_policy
646
- ensure_grpc!
647
- return nil unless @grpc.retry_policy
648
- RetryPolicy.from_grpc @grpc.retry_policy
649
- end
650
-
651
- ##
652
- # Sets a policy that specifies how Cloud Pub/Sub retries message delivery for this subscription. If `nil`, the
653
- # default retry policy is applied. This generally implies that messages will be retried as soon as possible
654
- # for healthy subscribers. Retry Policy will be triggered on NACKs or acknowledgement deadline exceeded events
655
- # for a given message.
656
- #
657
- # @param [RetryPolicy, nil] new_retry_policy A new retry policy for the subscription, or `nil`.
658
- #
659
- # @example
660
- # require "google/cloud/pubsub"
661
- #
662
- # pubsub = Google::Cloud::PubSub.new
663
- #
664
- # sub = pubsub.subscription "my-topic-sub"
665
- #
666
- # sub.retry_policy = Google::Cloud::PubSub::RetryPolicy.new minimum_backoff: 5, maximum_backoff: 300
667
- #
668
- # sub.retry_policy.minimum_backoff #=> 5
669
- # sub.retry_policy.maximum_backoff #=> 300
670
- #
671
- def retry_policy= new_retry_policy
672
- ensure_service!
673
- new_retry_policy = new_retry_policy.to_grpc if new_retry_policy
674
- update_grpc = Google::Cloud::PubSub::V1::Subscription.new name: name, retry_policy: new_retry_policy
675
- @grpc = service.update_subscription update_grpc, :retry_policy
676
- @resource_name = nil
677
- end
678
-
679
- ##
680
- # Whether message ordering has been enabled. When enabled, messages
681
- # published with the same `ordering_key` will be delivered in the order
682
- # they were published. When disabled, messages may be delivered in any
683
- # order.
684
- #
685
- # @note At the time of this release, ordering keys are not yet publicly
686
- # enabled and requires special project enablements.
687
- #
688
- # See {Topic#publish_async}, {#listen}, and {Message#ordering_key}.
689
- #
690
- # Makes an API call to retrieve the enable_message_ordering value when called on a
691
- # reference object. See {#reference?}.
692
- #
693
- # @return [Boolean]
694
- #
695
- def message_ordering?
696
- ensure_grpc!
697
- @grpc.enable_message_ordering
698
- end
699
-
700
- ##
701
- # Whether the subscription is detached from its topic. Detached subscriptions don't receive messages from their
702
- # topic and don't retain any backlog. {#pull} and {#listen} (pull and streaming pull) operations will raise
703
- # `FAILED_PRECONDITION`. If the subscription is a push subscription (see {#push_config}), pushes to the endpoint
704
- # will not be made. The default value is `false`.
705
- #
706
- # See {Topic#subscribe} and {#detach}.
707
- #
708
- # Makes an API call to retrieve the detached value when called on a
709
- # reference object. See {#reference?}.
710
- #
711
- # @return [Boolean]
712
- #
713
- # @example
714
- # require "google/cloud/pubsub"
715
- #
716
- # pubsub = Google::Cloud::PubSub.new
717
- #
718
- # sub = pubsub.subscription "my-topic-sub"
719
- # sub.detach
720
- #
721
- # # sleep 120
722
- # sub.detached? #=> true
723
- #
724
- def detached?
725
- ensure_grpc!
726
- @grpc.detached
727
- end
728
-
729
- ##
730
- # Determines whether the subscription exists in the Pub/Sub service.
731
- #
732
- # Makes an API call to determine whether the subscription resource
733
- # exists when called on a reference object. See {#reference?}.
734
- #
735
- # @return [Boolean]
736
- #
737
- # @example
738
- # require "google/cloud/pubsub"
739
- #
740
- # pubsub = Google::Cloud::PubSub.new
741
- #
742
- # sub = pubsub.subscription "my-topic-sub"
743
- # sub.exists? #=> true
744
- #
745
- def exists?
746
- # Always true if the object is not set as reference
747
- return true unless reference?
748
- # If we have a value, return it
749
- return @exists unless @exists.nil?
750
- ensure_grpc!
751
- @exists = true
752
- rescue Google::Cloud::NotFoundError
753
- @exists = false
754
- end
755
-
756
- ##
757
- # Deletes an existing subscription.
758
- # All pending messages in the subscription are immediately dropped.
759
- #
760
- # @return [Boolean] Returns `true` if the subscription was deleted.
761
- #
762
- # @example
763
- # require "google/cloud/pubsub"
764
- #
765
- # pubsub = Google::Cloud::PubSub.new
766
- #
767
- # sub = pubsub.subscription "my-topic-sub"
768
- # sub.delete
769
- #
770
- def delete
771
- ensure_service!
772
- service.delete_subscription name
773
- true
774
- end
775
-
776
- ##
777
- # Detaches a subscription from its topic. All messages retained in the subscription are dropped. Detached
778
- # subscriptions don't receive messages from their topic and don't retain any backlog. Subsequent {#pull} and
779
- # {#listen} (pull and streaming pull) operations will raise `FAILED_PRECONDITION`. If the subscription is a push
780
- # subscription (see {#push_config}), pushes to the endpoint will stop. It may take a few minutes for the
781
- # subscription's detached state to be reflected in subsequent calls to {#detached?}.
782
- #
783
- # @return [Boolean] Returns `true` if the detach operation was successful.
784
- #
785
- # @example
786
- # require "google/cloud/pubsub"
787
- #
788
- # pubsub = Google::Cloud::PubSub.new
789
- #
790
- # sub = pubsub.subscription "my-topic-sub"
791
- # sub.detach
792
- #
793
- # # sleep 120
794
- # sub.detached? #=> true
795
- #
796
- def detach
797
- ensure_service!
798
- service.detach_subscription name
799
- true
800
- end
801
-
802
- ##
803
- # Pulls messages from the server, blocking until messages are available
804
- # when called with the `immediate: false` option, which is recommended
805
- # to avoid adverse impacts on the performance of pull operations.
806
- #
807
- # Raises an API error with status `UNAVAILABLE` if there are too many
808
- # concurrent pull requests pending for the given subscription.
809
- #
810
- # See also {#listen} for the preferred way to process messages as they
811
- # become available.
812
- #
813
- # @param [Boolean] immediate Whether to return immediately or block until
814
- # messages are available.
815
- #
816
- # **Warning:** The default value of this field is `true`. However, sending
817
- # `true` is discouraged because it adversely impacts the performance of
818
- # pull operations. We recommend that users always explicitly set this field
819
- # to `false`.
820
- #
821
- # If this field set to `true`, the system will respond immediately
822
- # even if it there are no messages available to return in the pull
823
- # response. Otherwise, the system may wait (for a bounded amount of time)
824
- # until at least one message is available, rather than returning no messages.
825
- #
826
- # See also {#listen} for the preferred way to process messages as they
827
- # become available.
828
- # @param [Integer] max The maximum number of messages to return for this
829
- # request. The Pub/Sub system may return fewer than the number
830
- # specified. The default value is `100`, the maximum value is `1000`.
831
- #
832
- # @return [Array<Google::Cloud::PubSub::ReceivedMessage>]
833
- #
834
- # @example The `immediate: false` option is now recommended to avoid adverse impacts on pull operations:
835
- # require "google/cloud/pubsub"
836
- #
837
- # pubsub = Google::Cloud::PubSub.new
838
- #
839
- # sub = pubsub.subscription "my-topic-sub"
840
- # received_messages = sub.pull immediate: false
841
- # received_messages.each do |received_message|
842
- # received_message.acknowledge!
843
- # end
844
- #
845
- # @example A maximum number of messages returned can also be specified:
846
- # require "google/cloud/pubsub"
847
- #
848
- # pubsub = Google::Cloud::PubSub.new
849
- #
850
- # sub = pubsub.subscription "my-topic-sub"
851
- # received_messages = sub.pull immediate: false, max: 10
852
- # received_messages.each do |received_message|
853
- # received_message.acknowledge!
854
- # end
855
- #
856
- def pull immediate: true, max: 100
857
- ensure_service!
858
- options = { immediate: immediate, max: max }
859
- list_grpc = service.pull name, options
860
- Array(list_grpc.received_messages).map do |msg_grpc|
861
- ReceivedMessage.from_grpc msg_grpc, self
862
- end
863
- rescue Google::Cloud::DeadlineExceededError
864
- []
865
- end
866
-
867
- ##
868
- # Pulls from the server while waiting for messages to become available.
869
- # This is the same as:
870
- #
871
- # subscription.pull immediate: false
872
- #
873
- # See also {#listen} for the preferred way to process messages as they
874
- # become available.
875
- #
876
- # @param [Integer] max The maximum number of messages to return for this
877
- # request. The Pub/Sub system may return fewer than the number
878
- # specified. The default value is `100`, the maximum value is `1000`.
879
- #
880
- # @return [Array<Google::Cloud::PubSub::ReceivedMessage>]
881
- #
882
- # @example
883
- # require "google/cloud/pubsub"
884
- #
885
- # pubsub = Google::Cloud::PubSub.new
886
- #
887
- # sub = pubsub.subscription "my-topic-sub"
888
- # received_messages = sub.wait_for_messages
889
- # received_messages.each do |received_message|
890
- # received_message.acknowledge!
891
- # end
892
- #
893
- def wait_for_messages max: 100
894
- pull immediate: false, max: max
895
- end
896
-
897
- ##
898
- # Create a {Subscriber} object that receives and processes messages
899
- # using the code provided in the callback. Messages passed to the
900
- # callback should acknowledge ({ReceivedMessage#acknowledge!}) or reject
901
- # ({ReceivedMessage#reject!}) the message. If no action is taken, the
902
- # message will be removed from the subscriber and made available for
903
- # redelivery after the callback is completed.
904
- #
905
- # Google Cloud Pub/Sub ordering keys provide the ability to ensure
906
- # related messages are sent to subscribers in the order in which they
907
- # were published. Messages can be tagged with an ordering key, a string
908
- # that identifies related messages for which publish order should be
909
- # respected. The service guarantees that, for a given ordering key and
910
- # publisher, messages are sent to subscribers in the order in which they
911
- # were published. Ordering does not require sacrificing high throughput
912
- # or scalability, as the service automatically distributes messages for
913
- # different ordering keys across subscribers.
914
- #
915
- # To use ordering keys, the subscription must be created with message
916
- # ordering enabled (See {Topic#subscribe} and {#message_ordering?})
917
- # before calling {#listen}. When enabled, the subscriber will deliver
918
- # messages with the same `ordering_key` in the order they were
919
- # published.
920
- #
921
- # @note At the time of this release, ordering keys are not yet publicly
922
- # enabled and requires special project enablements.
923
- #
924
- # @param [Numeric] deadline The default number of seconds the stream
925
- # will hold received messages before modifying the message's ack
926
- # deadline. The minimum is 10, the maximum is 600. Default is
927
- # {#deadline}. Optional.
928
- #
929
- # When using a reference object an API call will be made to retrieve
930
- # the default deadline value for the subscription when this argument
931
- # is not provided. See {#reference?}.
932
- # @param [Boolean] message_ordering Whether message ordering has been
933
- # enabled. The value provided must match the value set on the Pub/Sub
934
- # service. See {#message_ordering?}. Optional.
935
- #
936
- # When using a reference object an API call will be made to retrieve
937
- # the default message_ordering value for the subscription when this
938
- # argument is not provided. See {#reference?}.
939
- # @param [Integer] streams The number of concurrent streams to open to
940
- # pull messages from the subscription. Default is 2. Optional.
941
- # @param [Hash, Integer] inventory The settings to control how received messages are to be handled by the
942
- # subscriber. When provided as an Integer instead of a Hash only `max_outstanding_messages` will be set.
943
- # Optional.
944
- #
945
- # Hash keys and values may include the following:
946
- #
947
- # * `:max_outstanding_messages` [Integer] The number of received messages to be collected by subscriber.
948
- # Default is 1,000. (Note: replaces `:limit`, which is deprecated.)
949
- # * `:max_outstanding_bytes` [Integer] The total byte size of received messages to be collected by
950
- # subscriber. Default is 100,000,000 (100MB). (Note: replaces `:bytesize`, which is deprecated.)
951
- # * `:use_legacy_flow_control` [Boolean] Disables enforcing flow control settings at the Cloud PubSub
952
- # server and the less accurate method of only enforcing flow control at the client side is used instead.
953
- # Default is false.
954
- # * `:max_total_lease_duration` [Integer] The number of seconds that received messages can be held awaiting
955
- # processing. Default is 3,600 (1 hour). (Note: replaces `:extension`, which is deprecated.)
956
- # * `:max_duration_per_lease_extension` [Integer] The maximum amount of time in seconds for a single lease
957
- # extension attempt. Bounds the delay before a message redelivery if the subscriber fails to extend the
958
- # deadline. Default is 0 (disabled).
959
- # @param [Hash] threads The number of threads to create to handle
960
- # concurrent calls by each stream opened by the subscriber. Optional.
961
- #
962
- # Hash keys and values may include the following:
963
- #
964
- # * `:callback` (Integer) The number of threads used to handle the
965
- # received messages. Default is 8.
966
- # * `:push` (Integer) The number of threads to handle
967
- # acknowledgement ({ReceivedMessage#ack!}) and modify ack deadline
968
- # messages ({ReceivedMessage#nack!},
969
- # {ReceivedMessage#modify_ack_deadline!}). Default is 4.
970
- #
971
- # @yield [received_message] a block for processing new messages
972
- # @yieldparam [ReceivedMessage] received_message the newly received
973
- # message
974
- #
975
- # @return [Subscriber]
976
- #
977
- # @example
978
- # require "google/cloud/pubsub"
979
- #
980
- # pubsub = Google::Cloud::PubSub.new
981
- #
982
- # sub = pubsub.subscription "my-topic-sub"
983
- #
984
- # subscriber = sub.listen do |received_message|
985
- # # process message
986
- # puts "Data: #{received_message.message.data}, published at #{received_message.message.published_at}"
987
- # received_message.acknowledge!
988
- # end
989
- #
990
- # # Start background threads that will call block passed to listen.
991
- # subscriber.start
992
- #
993
- # # Shut down the subscriber when ready to stop receiving messages.
994
- # subscriber.stop!
995
- #
996
- # @example Configuring to increase concurrent callbacks:
997
- # require "google/cloud/pubsub"
998
- #
999
- # pubsub = Google::Cloud::PubSub.new
1000
- #
1001
- # sub = pubsub.subscription "my-topic-sub"
1002
- #
1003
- # subscriber = sub.listen threads: { callback: 16 } do |rec_message|
1004
- # # store the message somewhere before acknowledging
1005
- # store_in_backend rec_message.data # takes a few seconds
1006
- # rec_message.acknowledge!
1007
- # end
1008
- #
1009
- # # Start background threads that will call block passed to listen.
1010
- # subscriber.start
1011
- #
1012
- # # Shut down the subscriber when ready to stop receiving messages.
1013
- # subscriber.stop!
1014
- #
1015
- # @example Ordered messages are supported using ordering_key:
1016
- # require "google/cloud/pubsub"
1017
- #
1018
- # pubsub = Google::Cloud::PubSub.new
1019
- #
1020
- # sub = pubsub.subscription "my-ordered-topic-sub"
1021
- # sub.message_ordering? #=> true
1022
- #
1023
- # subscriber = sub.listen do |received_message|
1024
- # # messsages with the same ordering_key are received
1025
- # # in the order in which they were published.
1026
- # received_message.acknowledge!
1027
- # end
1028
- #
1029
- # # Start background threads that will call block passed to listen.
1030
- # subscriber.start
1031
- #
1032
- # # Shut down the subscriber when ready to stop receiving messages.
1033
- # subscriber.stop!
1034
- #
1035
- # @example Set the maximum amount of time before redelivery if the subscriber fails to extend the deadline:
1036
- # require "google/cloud/pubsub"
1037
- #
1038
- # pubsub = Google::Cloud::PubSub.new
1039
- #
1040
- # sub = pubsub.subscription "my-topic-sub"
1041
- #
1042
- # subscriber = sub.listen inventory: { max_duration_per_lease_extension: 20 } do |received_message|
1043
- # # Process message very slowly with possibility of failure.
1044
- # process rec_message.data # takes minutes
1045
- # rec_message.acknowledge!
1046
- # end
1047
- #
1048
- # # Start background threads that will call block passed to listen.
1049
- # subscriber.start
1050
- #
1051
- # # Shut down the subscriber when ready to stop receiving messages.
1052
- # subscriber.stop!
1053
- #
1054
- def listen deadline: nil, message_ordering: nil, streams: nil, inventory: nil, threads: {}, &block
1055
- ensure_service!
1056
- deadline ||= self.deadline
1057
- message_ordering = message_ordering? if message_ordering.nil?
1058
-
1059
- Subscriber.new name, block, deadline: deadline, streams: streams, inventory: inventory,
1060
- message_ordering: message_ordering, threads: threads, service: service
1061
- end
1062
-
1063
- ##
1064
- # Acknowledges receipt of a message. After an ack,
1065
- # the Pub/Sub system can remove the message from the subscription.
1066
- # Acknowledging a message whose ack deadline has expired may succeed,
1067
- # although the message may have been sent again.
1068
- # Acknowledging a message more than once will not result in an error.
1069
- # This is only used for messages received via pull.
1070
- #
1071
- # See also {ReceivedMessage#acknowledge!}.
1072
- #
1073
- # @param [ReceivedMessage, String] messages One or more
1074
- # {ReceivedMessage} objects or ack_id values.
1075
- #
1076
- # @example
1077
- # require "google/cloud/pubsub"
1078
- #
1079
- # pubsub = Google::Cloud::PubSub.new
1080
- #
1081
- # sub = pubsub.subscription "my-topic-sub"
1082
- # received_messages = sub.pull immediate: false
1083
- # sub.acknowledge received_messages
1084
- #
1085
- def acknowledge *messages
1086
- ack_ids = coerce_ack_ids messages
1087
- return true if ack_ids.empty?
1088
- ensure_service!
1089
- service.acknowledge name, *ack_ids
1090
- true
1091
- end
1092
- alias ack acknowledge
1093
-
1094
- ##
1095
- # Modifies the acknowledge deadline for messages.
1096
- #
1097
- # This indicates that more time is needed to process the messages, or to
1098
- # make the messages available for redelivery if the processing was
1099
- # interrupted.
1100
- #
1101
- # See also {ReceivedMessage#modify_ack_deadline!}.
1102
- #
1103
- # @param [Integer] new_deadline The new ack deadline in seconds from the
1104
- # time this request is sent to the Pub/Sub system. Must be >= 0. For
1105
- # example, if the value is `10`, the new ack deadline will expire 10
1106
- # seconds after the call is made. Specifying `0` may immediately make
1107
- # the message available for another pull request.
1108
- # @param [ReceivedMessage, String] messages One or more
1109
- # {ReceivedMessage} objects or ack_id values.
1110
- #
1111
- # @example
1112
- # require "google/cloud/pubsub"
1113
- #
1114
- # pubsub = Google::Cloud::PubSub.new
1115
- #
1116
- # sub = pubsub.subscription "my-topic-sub"
1117
- # received_messages = sub.pull immediate: false
1118
- # sub.modify_ack_deadline 120, received_messages
1119
- #
1120
- def modify_ack_deadline new_deadline, *messages
1121
- ack_ids = coerce_ack_ids messages
1122
- ensure_service!
1123
- service.modify_ack_deadline name, ack_ids, new_deadline
1124
- true
1125
- end
1126
-
1127
- ##
1128
- # Creates a new {Snapshot} from the subscription. The created snapshot
1129
- # is guaranteed to retain:
1130
- #
1131
- # * The existing backlog on the subscription. More precisely, this is
1132
- # defined as the messages in the subscription's backlog that are
1133
- # unacknowledged upon the successful completion of the
1134
- # `create_snapshot` operation; as well as:
1135
- # * Any messages published to the subscription's topic following the
1136
- # successful completion of the `create_snapshot` operation.
1137
- #
1138
- # @param [String, nil] snapshot_name Name of the new snapshot. Optional.
1139
- # If the name is not provided, the server will assign a random name
1140
- # for this snapshot on the same project as the subscription.
1141
- # The value can be a simple snapshot ID (relative name), in which
1142
- # case the current project ID will be supplied, or a fully-qualified
1143
- # snapshot name in the form
1144
- # `projects/{project_id}/snapshots/{snapshot_id}`.
1145
- #
1146
- # The snapshot ID (relative name) must start with a letter, and
1147
- # contain only letters (`[A-Za-z]`), numbers (`[0-9]`), dashes (`-`),
1148
- # underscores (`_`), periods (`.`), tildes (`~`), plus (`+`) or percent
1149
- # signs (`%`). It must be between 3 and 255 characters in length, and
1150
- # it must not start with `goog`.
1151
- # @param [Hash] labels A hash of user-provided labels associated with
1152
- # the snapshot. You can use these to organize and group your
1153
- # snapshots. Label keys and values can be no longer than 63
1154
- # characters, can only contain lowercase letters, numeric characters,
1155
- # underscores and dashes. International characters are allowed. Label
1156
- # values are optional. Label keys must start with a letter and each
1157
- # label in the list must have a different key. See [Creating and
1158
- # Managing Labels](https://cloud.google.com/pubsub/docs/labels).
1159
- #
1160
- # @return [Google::Cloud::PubSub::Snapshot]
1161
- #
1162
- # @example
1163
- # require "google/cloud/pubsub"
1164
- #
1165
- # pubsub = Google::Cloud::PubSub.new
1166
- # sub = pubsub.subscription "my-sub"
1167
- #
1168
- # snapshot = sub.create_snapshot "my-snapshot"
1169
- # snapshot.name #=> "projects/my-project/snapshots/my-snapshot"
1170
- #
1171
- # @example Without providing a name:
1172
- # require "google/cloud/pubsub"
1173
- #
1174
- # pubsub = Google::Cloud::PubSub.new
1175
- # sub = pubsub.subscription "my-sub"
1176
- #
1177
- # snapshot = sub.create_snapshot
1178
- # snapshot.name #=> "projects/my-project/snapshots/gcr-analysis-..."
1179
- #
1180
- def create_snapshot snapshot_name = nil, labels: nil
1181
- ensure_service!
1182
- grpc = service.create_snapshot name, snapshot_name, labels: labels
1183
- Snapshot.from_grpc grpc, service
1184
- end
1185
- alias new_snapshot create_snapshot
1186
-
1187
- ##
1188
- # Resets the subscription's backlog to a given {Snapshot} or to a point
1189
- # in time, whichever is provided in the request.
1190
- #
1191
- # @param [Snapshot, String, Time] snapshot The `Snapshot` instance,
1192
- # snapshot name, or time to which to perform the seek.
1193
- # If the argument is a snapshot, the snapshot's topic must be the
1194
- # same as that of the subscription. If it is a time, messages retained
1195
- # in the subscription that were published before this time are marked
1196
- # as acknowledged, and messages retained in the subscription that were
1197
- # published after this time are marked as unacknowledged. Note that
1198
- # this operation affects only those messages retained in the
1199
- # subscription. For example, if the time corresponds to a point before
1200
- # the message retention window (or to a point before the system's
1201
- # notion of the subscription creation time), only retained messages
1202
- # will be marked as unacknowledged, and already-expunged messages will
1203
- # not be restored.
1204
- #
1205
- # @return [Boolean] Returns `true` if the seek was successful.
1206
- #
1207
- # @example Using a snapshot
1208
- # require "google/cloud/pubsub"
1209
- #
1210
- # pubsub = Google::Cloud::PubSub.new
1211
- # sub = pubsub.subscription "my-sub"
1212
- #
1213
- # snapshot = sub.create_snapshot
1214
- #
1215
- # received_messages = sub.pull immediate: false
1216
- # sub.acknowledge received_messages
1217
- #
1218
- # sub.seek snapshot
1219
- #
1220
- # @example Using a time:
1221
- # require "google/cloud/pubsub"
1222
- #
1223
- # pubsub = Google::Cloud::PubSub.new
1224
- # sub = pubsub.subscription "my-sub"
1225
- #
1226
- # time = Time.now
1227
- #
1228
- # received_messages = sub.pull immediate: false
1229
- # sub.acknowledge received_messages
1230
- #
1231
- # sub.seek time
1232
- #
1233
- def seek snapshot
1234
- ensure_service!
1235
- service.seek name, snapshot
1236
- true
1237
- end
1238
-
1239
- ##
1240
- # Determines whether the subscription object was created without
1241
- # retrieving the resource representation from the Pub/Sub service.
1242
- #
1243
- # @return [Boolean] `true` when the subscription was created without a
1244
- # resource representation, `false` otherwise.
1245
- #
1246
- # @example
1247
- # require "google/cloud/pubsub"
1248
- #
1249
- # pubsub = Google::Cloud::PubSub.new
1250
- #
1251
- # sub = pubsub.get_subscription "my-topic-sub", skip_lookup: true
1252
- # sub.reference? #=> true
1253
- #
1254
- def reference?
1255
- @grpc.nil?
1256
- end
1257
-
1258
- ##
1259
- # Determines whether the subscription object was created with a resource
1260
- # representation from the Pub/Sub service.
1261
- #
1262
- # @return [Boolean] `true` when the subscription was created with a
1263
- # resource representation, `false` otherwise.
1264
- #
1265
- # @example
1266
- # require "google/cloud/pubsub"
1267
- #
1268
- # pubsub = Google::Cloud::PubSub.new
1269
- #
1270
- # sub = pubsub.get_subscription "my-topic-sub"
1271
- # sub.resource? #=> true
1272
- #
1273
- def resource?
1274
- !@grpc.nil?
1275
- end
1276
-
1277
- ##
1278
- # Reloads the subscription with current data from the Pub/Sub service.
1279
- #
1280
- # @return [Google::Cloud::PubSub::Subscription] Returns the reloaded
1281
- # subscription
1282
- #
1283
- # @example
1284
- # require "google/cloud/pubsub"
1285
- #
1286
- # pubsub = Google::Cloud::PubSub.new
1287
- #
1288
- # sub = pubsub.get_subscription "my-topic-sub"
1289
- # sub.reload!
1290
- #
1291
- def reload!
1292
- ensure_service!
1293
- @grpc = service.get_subscription name
1294
- @resource_name = nil
1295
- self
1296
- end
1297
- alias refresh! reload!
1298
-
1299
- ##
1300
- # Gets the [Cloud IAM](https://cloud.google.com/iam/) access control
1301
- # policy for this subscription.
1302
- #
1303
- # @see https://cloud.google.com/pubsub/docs/reference/rpc/google.iam.v1#iampolicy
1304
- # google.iam.v1.IAMPolicy
1305
- #
1306
- # @yield [policy] A block for updating the policy. The latest policy
1307
- # will be read from the Pub/Sub service and passed to the block. After
1308
- # the block completes, the modified policy will be written to the
1309
- # service.
1310
- # @yieldparam [Policy] policy the current Cloud IAM Policy for this
1311
- # subscription
1312
- #
1313
- # @return [Policy] the current Cloud IAM Policy for this subscription
1314
- #
1315
- # @example
1316
- # require "google/cloud/pubsub"
1317
- #
1318
- # pubsub = Google::Cloud::PubSub.new
1319
- # sub = pubsub.subscription "my-subscription"
1320
- #
1321
- # policy = sub.policy
1322
- #
1323
- # @example Update the policy by passing a block:
1324
- # require "google/cloud/pubsub"
1325
- #
1326
- # pubsub = Google::Cloud::PubSub.new
1327
- # sub = pubsub.subscription "my-subscription"
1328
- #
1329
- # sub.policy do |p|
1330
- # p.add "roles/owner", "user:owner@example.com"
1331
- # end
1332
- #
1333
- def policy
1334
- ensure_service!
1335
- grpc = service.get_subscription_policy name
1336
- policy = Policy.from_grpc grpc
1337
- return policy unless block_given?
1338
- yield policy
1339
- update_policy policy
1340
- end
1341
-
1342
- ##
1343
- # Updates the [Cloud IAM](https://cloud.google.com/iam/) access control
1344
- # policy for this subscription. The policy should be read from
1345
- # {#policy}. See {Google::Cloud::PubSub::Policy} for an explanation of
1346
- # the policy `etag` property and how to modify policies.
1347
- #
1348
- # You can also update the policy by passing a block to {#policy}, which
1349
- # will call this method internally after the block completes.
1350
- #
1351
- # @see https://cloud.google.com/pubsub/docs/reference/rpc/google.iam.v1#iampolicy
1352
- # google.iam.v1.IAMPolicy
1353
- #
1354
- # @param [Policy] new_policy a new or modified Cloud IAM Policy for this
1355
- # subscription
1356
- #
1357
- # @return [Policy] the policy returned by the API update operation
1358
- #
1359
- # @example
1360
- # require "google/cloud/pubsub"
1361
- #
1362
- # pubsub = Google::Cloud::PubSub.new
1363
- # sub = pubsub.subscription "my-subscription"
1364
- #
1365
- # policy = sub.policy # API call
1366
- #
1367
- # policy.add "roles/owner", "user:owner@example.com"
1368
- #
1369
- # sub.update_policy policy # API call
1370
- #
1371
- def update_policy new_policy
1372
- ensure_service!
1373
- grpc = service.set_subscription_policy name, new_policy.to_grpc
1374
- Policy.from_grpc grpc
1375
- end
1376
- alias policy= update_policy
1377
-
1378
- ##
1379
- # Tests the specified permissions against the [Cloud
1380
- # IAM](https://cloud.google.com/iam/) access control policy.
1381
- #
1382
- # @see https://cloud.google.com/iam/docs/managing-policies Managing
1383
- # Policies
1384
- #
1385
- # @param [String, Array<String>] permissions The set of permissions to
1386
- # check access for. Permissions with wildcards (such as `*` or
1387
- # `storage.*`) are not allowed.
1388
- #
1389
- # The permissions that can be checked on a subscription are:
1390
- #
1391
- # * pubsub.subscriptions.consume
1392
- # * pubsub.subscriptions.get
1393
- # * pubsub.subscriptions.delete
1394
- # * pubsub.subscriptions.update
1395
- # * pubsub.subscriptions.getIamPolicy
1396
- # * pubsub.subscriptions.setIamPolicy
1397
- #
1398
- # @return [Array<String>] The permissions that have access.
1399
- #
1400
- # @example
1401
- # require "google/cloud/pubsub"
1402
- #
1403
- # pubsub = Google::Cloud::PubSub.new
1404
- # sub = pubsub.subscription "my-subscription"
1405
- # perms = sub.test_permissions "pubsub.subscriptions.get",
1406
- # "pubsub.subscriptions.consume"
1407
- # perms.include? "pubsub.subscriptions.get" #=> true
1408
- # perms.include? "pubsub.subscriptions.consume" #=> false
1409
- #
1410
- def test_permissions *permissions
1411
- permissions = Array(permissions).flatten
1412
- ensure_service!
1413
- grpc = service.test_subscription_permissions name, permissions
1414
- grpc.permissions
1415
- end
1416
-
1417
- ##
1418
- # @private
1419
- # New Subscription from a Google::Cloud::PubSub::V1::Subscription
1420
- # object.
1421
- def self.from_grpc grpc, service
1422
- new.tap do |f|
1423
- f.grpc = grpc
1424
- f.service = service
1425
- end
1426
- end
1427
-
1428
- ##
1429
- # @private New reference {Subscription} object without making an HTTP
1430
- # request.
1431
- def self.from_name name, service, options = {}
1432
- name = service.subscription_path name, options
1433
- from_grpc(nil, service).tap do |s|
1434
- s.instance_variable_set :@resource_name, name
1435
- end
1436
- end
1437
-
1438
- protected
1439
-
1440
- ##
1441
- # @private Raise an error unless an active connection to the service is
1442
- # available.
1443
- def ensure_service!
1444
- raise "Must have active connection to service" unless service
1445
- end
1446
-
1447
- ##
1448
- # Ensures a Google::Cloud::PubSub::V1::Subscription object exists.
1449
- def ensure_grpc!
1450
- ensure_service!
1451
- reload! if reference?
1452
- end
1453
-
1454
- ##
1455
- # Makes sure the values are the `ack_id`. If given several
1456
- # {ReceivedMessage} objects extract the `ack_id` values.
1457
- def coerce_ack_ids messages
1458
- Array(messages).flatten.map do |msg|
1459
- msg.respond_to?(:ack_id) ? msg.ack_id : msg.to_s
1460
- end
1461
- end
1462
- end
1463
- end
1464
-
1465
- Pubsub = PubSub unless const_defined? :Pubsub
1466
- end
1467
- end