google-cloud-pubsub 0.20.0 → 2.6.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.yardopts +18 -0
- data/AUTHENTICATION.md +178 -0
- data/CHANGELOG.md +659 -0
- data/CODE_OF_CONDUCT.md +40 -0
- data/CONTRIBUTING.md +187 -0
- data/EMULATOR.md +37 -0
- data/LICENSE +201 -0
- data/LOGGING.md +32 -0
- data/OVERVIEW.md +528 -0
- data/TROUBLESHOOTING.md +31 -0
- data/lib/google/cloud/pubsub/async_publisher/batch.rb +310 -0
- data/lib/google/cloud/pubsub/async_publisher.rb +402 -0
- data/lib/google/cloud/pubsub/batch_publisher.rb +100 -0
- data/lib/google/cloud/pubsub/convert.rb +91 -0
- data/lib/google/cloud/pubsub/credentials.rb +26 -10
- data/lib/google/cloud/pubsub/errors.rb +85 -0
- data/lib/google/cloud/pubsub/message.rb +82 -20
- data/lib/google/cloud/pubsub/policy.rb +40 -61
- data/lib/google/cloud/pubsub/project.rb +405 -265
- data/lib/google/cloud/pubsub/publish_result.rb +103 -0
- data/lib/google/cloud/pubsub/received_message.rb +165 -30
- data/lib/google/cloud/pubsub/retry_policy.rb +88 -0
- data/lib/google/cloud/pubsub/schema/list.rb +180 -0
- data/lib/google/cloud/pubsub/schema.rb +310 -0
- data/lib/google/cloud/pubsub/service.rb +304 -162
- data/lib/google/cloud/pubsub/snapshot/list.rb +178 -0
- data/lib/google/cloud/pubsub/snapshot.rb +205 -0
- data/lib/google/cloud/pubsub/subscriber/enumerator_queue.rb +54 -0
- data/lib/google/cloud/pubsub/subscriber/inventory.rb +173 -0
- data/lib/google/cloud/pubsub/subscriber/sequencer.rb +115 -0
- data/lib/google/cloud/pubsub/subscriber/stream.rb +400 -0
- data/lib/google/cloud/pubsub/subscriber/timed_unary_buffer.rb +230 -0
- data/lib/google/cloud/pubsub/subscriber.rb +417 -0
- data/lib/google/cloud/pubsub/subscription/list.rb +38 -43
- data/lib/google/cloud/pubsub/subscription/push_config.rb +268 -0
- data/lib/google/cloud/pubsub/subscription.rb +1040 -210
- data/lib/google/cloud/pubsub/topic/list.rb +32 -37
- data/lib/google/cloud/pubsub/topic.rb +726 -177
- data/lib/google/cloud/pubsub/version.rb +6 -4
- data/lib/google/cloud/pubsub.rb +138 -413
- data/lib/google-cloud-pubsub.rb +60 -42
- metadata +88 -39
- data/lib/google/cloud/pubsub/topic/publisher.rb +0 -87
- data/lib/google/iam/v1/iam_policy.rb +0 -33
- data/lib/google/iam/v1/iam_policy_services.rb +0 -30
- data/lib/google/iam/v1/policy.rb +0 -25
- data/lib/google/pubsub/v1/pubsub_pb.rb +0 -129
- data/lib/google/pubsub/v1/pubsub_services_pb.rb +0 -117
data/LOGGING.md
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
# Enabling gRPC Logging
|
2
|
+
|
3
|
+
To enable logging for this library, set the logger for the underlying
|
4
|
+
[gRPC](https://github.com/grpc/grpc/tree/master/src/ruby) library. The logger
|
5
|
+
that you set may be a Ruby stdlib
|
6
|
+
[`Logger`](https://ruby-doc.org/stdlib/libdoc/logger/rdoc/Logger.html) as
|
7
|
+
shown below, or a
|
8
|
+
[`Google::Cloud::Logging::Logger`](https://googleapis.dev/ruby/google-cloud-logging/latest)
|
9
|
+
that will write logs to [Stackdriver
|
10
|
+
Logging](https://cloud.google.com/logging/). See
|
11
|
+
[grpc/logconfig.rb](https://github.com/grpc/grpc/blob/master/src/ruby/lib/grpc/logconfig.rb)
|
12
|
+
and the gRPC
|
13
|
+
[spec_helper.rb](https://github.com/grpc/grpc/blob/master/src/ruby/spec/spec_helper.rb)
|
14
|
+
for additional information.
|
15
|
+
|
16
|
+
Configuring a Ruby stdlib logger:
|
17
|
+
|
18
|
+
```ruby
|
19
|
+
require "logger"
|
20
|
+
|
21
|
+
module MyLogger
|
22
|
+
LOGGER = Logger.new $stderr, level: Logger::WARN
|
23
|
+
def logger
|
24
|
+
LOGGER
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
# Define a gRPC module-level logger method before grpc/logconfig.rb loads.
|
29
|
+
module GRPC
|
30
|
+
extend MyLogger
|
31
|
+
end
|
32
|
+
```
|
data/OVERVIEW.md
ADDED
@@ -0,0 +1,528 @@
|
|
1
|
+
# Google Cloud Pub/Sub
|
2
|
+
|
3
|
+
Google Cloud Pub/Sub is designed to provide reliable, many-to-many, asynchronous
|
4
|
+
messaging between applications. Publisher applications can send messages to a
|
5
|
+
"topic" and other applications can subscribe to that topic to receive the
|
6
|
+
messages. By decoupling senders and receivers, Google Cloud Pub/Sub allows
|
7
|
+
developers to communicate between independently written applications.
|
8
|
+
|
9
|
+
The goal of google-cloud is to provide an API that is comfortable to Rubyists.
|
10
|
+
Your authentication credentials are detected automatically in Google Cloud
|
11
|
+
Platform (GCP), including Google Compute Engine (GCE), Google Kubernetes Engine
|
12
|
+
(GKE), Google App Engine (GAE), Google Cloud Functions (GCF) and Cloud Run. In
|
13
|
+
other environments you can configure authentication easily, either directly in
|
14
|
+
your code or via environment variables. Read more about the options for
|
15
|
+
connecting in the {file:AUTHENTICATION.md Authentication Guide}.
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
require "google/cloud/pubsub"
|
19
|
+
|
20
|
+
pubsub = Google::Cloud::PubSub.new
|
21
|
+
|
22
|
+
topic = pubsub.topic "my-topic"
|
23
|
+
topic.publish "task completed"
|
24
|
+
```
|
25
|
+
|
26
|
+
To learn more about Pub/Sub, read the [Google Cloud Pub/Sub Overview
|
27
|
+
](https://cloud.google.com/pubsub/overview).
|
28
|
+
|
29
|
+
## Retrieving Topics
|
30
|
+
|
31
|
+
A Topic is a named resource to which messages are sent by publishers. A Topic is
|
32
|
+
found by its name. (See {Google::Cloud::PubSub::Project#topic Project#topic})
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
require "google/cloud/pubsub"
|
36
|
+
|
37
|
+
pubsub = Google::Cloud::PubSub.new
|
38
|
+
topic = pubsub.topic "my-topic"
|
39
|
+
```
|
40
|
+
|
41
|
+
## Creating a Topic
|
42
|
+
|
43
|
+
A Topic is created from a Project. (See
|
44
|
+
{Google::Cloud::PubSub::Project#create_topic Project#create_topic})
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
require "google/cloud/pubsub"
|
48
|
+
|
49
|
+
pubsub = Google::Cloud::PubSub.new
|
50
|
+
topic = pubsub.create_topic "my-topic"
|
51
|
+
```
|
52
|
+
|
53
|
+
## Retrieving Subscriptions
|
54
|
+
|
55
|
+
A Subscription is a named resource representing the stream of messages from a
|
56
|
+
single, specific Topic, to be delivered to the subscribing application. A
|
57
|
+
Subscription is found by its name. (See
|
58
|
+
{Google::Cloud::PubSub::Topic#subscription Topic#subscription})
|
59
|
+
|
60
|
+
```ruby
|
61
|
+
require "google/cloud/pubsub"
|
62
|
+
|
63
|
+
pubsub = Google::Cloud::PubSub.new
|
64
|
+
|
65
|
+
topic = pubsub.topic "my-topic"
|
66
|
+
subscription = topic.subscription "my-topic-subscription"
|
67
|
+
puts subscription.name
|
68
|
+
```
|
69
|
+
|
70
|
+
## Creating a Subscription
|
71
|
+
|
72
|
+
A Subscription is created from a Topic. (See
|
73
|
+
{Google::Cloud::PubSub::Topic#subscribe Topic#subscribe})
|
74
|
+
|
75
|
+
```ruby
|
76
|
+
require "google/cloud/pubsub"
|
77
|
+
|
78
|
+
pubsub = Google::Cloud::PubSub.new
|
79
|
+
|
80
|
+
topic = pubsub.topic "my-topic"
|
81
|
+
sub = topic.subscribe "my-topic-sub"
|
82
|
+
puts sub.name # => "my-topic-sub"
|
83
|
+
```
|
84
|
+
|
85
|
+
The subscription can be created that specifies the number of seconds to wait to
|
86
|
+
be acknowledged as well as an endpoint URL to push the messages to:
|
87
|
+
|
88
|
+
```ruby
|
89
|
+
require "google/cloud/pubsub"
|
90
|
+
|
91
|
+
pubsub = Google::Cloud::PubSub.new
|
92
|
+
|
93
|
+
topic = pubsub.topic "my-topic"
|
94
|
+
sub = topic.subscribe "my-topic-sub",
|
95
|
+
deadline: 120,
|
96
|
+
endpoint: "https://example.com/push"
|
97
|
+
```
|
98
|
+
|
99
|
+
## Publishing Messages
|
100
|
+
|
101
|
+
Messages are published to a topic. Any message published to a topic without a
|
102
|
+
subscription will be lost. Ensure the topic has a subscription before
|
103
|
+
publishing. (See {Google::Cloud::PubSub::Topic#publish Topic#publish})
|
104
|
+
|
105
|
+
```ruby
|
106
|
+
require "google/cloud/pubsub"
|
107
|
+
|
108
|
+
pubsub = Google::Cloud::PubSub.new
|
109
|
+
|
110
|
+
topic = pubsub.topic "my-topic"
|
111
|
+
msg = topic.publish "task completed"
|
112
|
+
```
|
113
|
+
|
114
|
+
Messages can also be published with attributes:
|
115
|
+
|
116
|
+
```ruby
|
117
|
+
require "google/cloud/pubsub"
|
118
|
+
|
119
|
+
pubsub = Google::Cloud::PubSub.new
|
120
|
+
|
121
|
+
topic = pubsub.topic "my-topic"
|
122
|
+
msg = topic.publish "task completed",
|
123
|
+
foo: :bar,
|
124
|
+
this: :that
|
125
|
+
```
|
126
|
+
|
127
|
+
Messages can also be published in batches asynchronously using `publish_async`.
|
128
|
+
(See {Google::Cloud::PubSub::Topic#publish_async Topic#publish_async} and
|
129
|
+
{Google::Cloud::PubSub::AsyncPublisher AsyncPublisher})
|
130
|
+
|
131
|
+
```ruby
|
132
|
+
require "google/cloud/pubsub"
|
133
|
+
|
134
|
+
pubsub = Google::Cloud::PubSub.new
|
135
|
+
|
136
|
+
topic = pubsub.topic "my-topic"
|
137
|
+
topic.publish_async "task completed" do |result|
|
138
|
+
if result.succeeded?
|
139
|
+
log_publish_success result.data
|
140
|
+
else
|
141
|
+
log_publish_failure result.data, result.error
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
topic.async_publisher.stop!
|
146
|
+
```
|
147
|
+
|
148
|
+
Or multiple messages can be published in batches at the same time by passing a
|
149
|
+
block to `publish`. (See {Google::Cloud::PubSub::BatchPublisher BatchPublisher})
|
150
|
+
|
151
|
+
```ruby
|
152
|
+
require "google/cloud/pubsub"
|
153
|
+
|
154
|
+
pubsub = Google::Cloud::PubSub.new
|
155
|
+
|
156
|
+
topic = pubsub.topic "my-topic"
|
157
|
+
msgs = topic.publish do |batch|
|
158
|
+
batch.publish "task 1 completed", foo: :bar
|
159
|
+
batch.publish "task 2 completed", foo: :baz
|
160
|
+
batch.publish "task 3 completed", foo: :bif
|
161
|
+
end
|
162
|
+
```
|
163
|
+
|
164
|
+
## Receiving Messages
|
165
|
+
|
166
|
+
Messages can be streamed from a subscription with a subscriber object that is
|
167
|
+
created using `listen`. (See {Google::Cloud::PubSub::Subscription#listen
|
168
|
+
Subscription#listen} and {Google::Cloud::PubSub::Subscriber Subscriber})
|
169
|
+
|
170
|
+
```ruby
|
171
|
+
require "google/cloud/pubsub"
|
172
|
+
|
173
|
+
pubsub = Google::Cloud::PubSub.new
|
174
|
+
|
175
|
+
sub = pubsub.subscription "my-topic-sub"
|
176
|
+
|
177
|
+
# Create a subscriber to listen for available messages.
|
178
|
+
# By default, this block will be called on 8 concurrent threads
|
179
|
+
# but this can be tuned with the `threads` option.
|
180
|
+
# The `streams` and `inventory` parameters allow further tuning.
|
181
|
+
subscriber = sub.listen threads: { callback: 16 } do |received_message|
|
182
|
+
# process message
|
183
|
+
puts "Data: #{received_message.message.data}, published at #{received_message.message.published_at}"
|
184
|
+
received_message.acknowledge!
|
185
|
+
end
|
186
|
+
|
187
|
+
# Handle exceptions from listener
|
188
|
+
subscriber.on_error do |exception|
|
189
|
+
puts "Exception: #{exception.class} #{exception.message}"
|
190
|
+
end
|
191
|
+
|
192
|
+
# Gracefully shut down the subscriber on program exit, blocking until
|
193
|
+
# all received messages have been processed or 10 seconds have passed
|
194
|
+
at_exit do
|
195
|
+
subscriber.stop!(10)
|
196
|
+
end
|
197
|
+
|
198
|
+
# Start background threads that will call the block passed to listen.
|
199
|
+
subscriber.start
|
200
|
+
|
201
|
+
# Block, letting processing threads continue in the background
|
202
|
+
sleep
|
203
|
+
```
|
204
|
+
|
205
|
+
Messages also can be pulled directly in a one-time operation. (See
|
206
|
+
{Google::Cloud::PubSub::Subscription#pull Subscription#pull})
|
207
|
+
|
208
|
+
The `immediate: false` option is recommended to avoid adverse impacts on the
|
209
|
+
performance of pull operations.
|
210
|
+
|
211
|
+
```ruby
|
212
|
+
require "google/cloud/pubsub"
|
213
|
+
|
214
|
+
pubsub = Google::Cloud::PubSub.new
|
215
|
+
|
216
|
+
sub = pubsub.subscription "my-topic-sub"
|
217
|
+
received_messages = sub.pull immediate: false
|
218
|
+
```
|
219
|
+
|
220
|
+
A maximum number of messages to pull can be specified:
|
221
|
+
|
222
|
+
```ruby
|
223
|
+
require "google/cloud/pubsub"
|
224
|
+
|
225
|
+
pubsub = Google::Cloud::PubSub.new
|
226
|
+
|
227
|
+
sub = pubsub.subscription "my-topic-sub"
|
228
|
+
received_messages = sub.pull immediate: false, max: 10
|
229
|
+
```
|
230
|
+
|
231
|
+
## Acknowledging a Message
|
232
|
+
|
233
|
+
Messages that are received can be acknowledged in Pub/Sub, marking the message
|
234
|
+
to be removed so it cannot be pulled again.
|
235
|
+
|
236
|
+
A Message that can be acknowledged is called a ReceivedMessage. ReceivedMessages
|
237
|
+
can be acknowledged one at a time: (See
|
238
|
+
{Google::Cloud::PubSub::ReceivedMessage#acknowledge!
|
239
|
+
ReceivedMessage#acknowledge!})
|
240
|
+
|
241
|
+
```ruby
|
242
|
+
require "google/cloud/pubsub"
|
243
|
+
|
244
|
+
pubsub = Google::Cloud::PubSub.new
|
245
|
+
|
246
|
+
sub = pubsub.subscription "my-topic-sub"
|
247
|
+
|
248
|
+
subscriber = sub.listen do |received_message|
|
249
|
+
# process message
|
250
|
+
received_message.acknowledge!
|
251
|
+
end
|
252
|
+
|
253
|
+
# Start background threads that will call the block passed to listen.
|
254
|
+
subscriber.start
|
255
|
+
|
256
|
+
# Shut down the subscriber when ready to stop receiving messages.
|
257
|
+
subscriber.stop!
|
258
|
+
```
|
259
|
+
|
260
|
+
Or, multiple messages can be acknowledged in a single API call: (See
|
261
|
+
{Google::Cloud::PubSub::Subscription#acknowledge Subscription#acknowledge})
|
262
|
+
|
263
|
+
```ruby
|
264
|
+
require "google/cloud/pubsub"
|
265
|
+
|
266
|
+
pubsub = Google::Cloud::PubSub.new
|
267
|
+
|
268
|
+
sub = pubsub.subscription "my-topic-sub"
|
269
|
+
received_messages = sub.pull immediate: false
|
270
|
+
sub.acknowledge received_messages
|
271
|
+
```
|
272
|
+
|
273
|
+
## Modifying a Deadline
|
274
|
+
|
275
|
+
A message must be acknowledged after it is pulled, or Pub/Sub will mark the
|
276
|
+
message for redelivery. The message acknowledgement deadline can delayed if more
|
277
|
+
time is needed. This will allow more time to process the message before the
|
278
|
+
message is marked for redelivery. (See
|
279
|
+
{Google::Cloud::PubSub::ReceivedMessage#modify_ack_deadline!
|
280
|
+
ReceivedMessage#modify_ack_deadline!})
|
281
|
+
|
282
|
+
```ruby
|
283
|
+
require "google/cloud/pubsub"
|
284
|
+
|
285
|
+
pubsub = Google::Cloud::PubSub.new
|
286
|
+
|
287
|
+
sub = pubsub.subscription "my-topic-sub"
|
288
|
+
subscriber = sub.listen do |received_message|
|
289
|
+
puts received_message.message.data
|
290
|
+
|
291
|
+
# Delay for 2 minutes
|
292
|
+
received_message.modify_ack_deadline! 120
|
293
|
+
end
|
294
|
+
|
295
|
+
# Start background threads that will call the block passed to listen.
|
296
|
+
subscriber.start
|
297
|
+
|
298
|
+
# Shut down the subscriber when ready to stop receiving messages.
|
299
|
+
subscriber.stop!
|
300
|
+
```
|
301
|
+
|
302
|
+
The message can also be made available for immediate redelivery:
|
303
|
+
|
304
|
+
```ruby
|
305
|
+
require "google/cloud/pubsub"
|
306
|
+
|
307
|
+
pubsub = Google::Cloud::PubSub.new
|
308
|
+
|
309
|
+
sub = pubsub.subscription "my-topic-sub"
|
310
|
+
subscriber = sub.listen do |received_message|
|
311
|
+
puts received_message.message.data
|
312
|
+
|
313
|
+
# Mark for redelivery
|
314
|
+
received_message.reject!
|
315
|
+
end
|
316
|
+
|
317
|
+
# Start background threads that will call the block passed to listen.
|
318
|
+
subscriber.start
|
319
|
+
|
320
|
+
# Shut down the subscriber when ready to stop receiving messages.
|
321
|
+
subscriber.stop!
|
322
|
+
```
|
323
|
+
|
324
|
+
Multiple messages can be delayed or made available for immediate redelivery:
|
325
|
+
(See {Google::Cloud::PubSub::Subscription#modify_ack_deadline
|
326
|
+
Subscription#modify_ack_deadline})
|
327
|
+
|
328
|
+
```ruby
|
329
|
+
require "google/cloud/pubsub"
|
330
|
+
|
331
|
+
pubsub = Google::Cloud::PubSub.new
|
332
|
+
|
333
|
+
sub = pubsub.subscription "my-topic-sub"
|
334
|
+
received_messages = sub.pull immediate: false
|
335
|
+
sub.modify_ack_deadline 120, received_messages
|
336
|
+
```
|
337
|
+
|
338
|
+
## Using Ordering Keys
|
339
|
+
|
340
|
+
Google Cloud Pub/Sub ordering keys provide the ability to ensure related
|
341
|
+
messages are sent to subscribers in the order in which they were published.
|
342
|
+
Messages can be tagged with an ordering key, a string that identifies related
|
343
|
+
messages for which publish order should be respected. The service guarantees
|
344
|
+
that, for a given ordering key and publisher, messages are sent to subscribers
|
345
|
+
in the order in which they were published. Ordering does not require sacrificing
|
346
|
+
high throughput or scalability, as the service automatically distributes
|
347
|
+
messages for different ordering keys across subscribers.
|
348
|
+
|
349
|
+
Note: At the time of this release, ordering keys are not yet publicly enabled
|
350
|
+
and requires special project enablements.
|
351
|
+
|
352
|
+
### Publishing Ordered Messages
|
353
|
+
|
354
|
+
To use ordering keys when publishing messages, a call to
|
355
|
+
{Google::Cloud::PubSub::Topic#enable_message_ordering!
|
356
|
+
Topic#enable_message_ordering!} must be made and the `ordering_key` argument
|
357
|
+
must be provided when calling {Google::Cloud::PubSub::Topic#publish_async
|
358
|
+
Topic#publish_async}.
|
359
|
+
|
360
|
+
```ruby
|
361
|
+
require "google/cloud/pubsub"
|
362
|
+
|
363
|
+
pubsub = Google::Cloud::PubSub.new
|
364
|
+
|
365
|
+
topic = pubsub.topic "my-ordered-topic"
|
366
|
+
|
367
|
+
# Ensure that message ordering is enabled.
|
368
|
+
topic.enable_message_ordering!
|
369
|
+
|
370
|
+
# Publish an ordered message with an ordering key.
|
371
|
+
topic.publish_async "task completed",
|
372
|
+
ordering_key: "task-key"
|
373
|
+
|
374
|
+
# Shut down the publisher when ready to stop publishing messages.
|
375
|
+
topic.async_publisher.stop!
|
376
|
+
```
|
377
|
+
|
378
|
+
### Handling errors with Ordered Keys
|
379
|
+
|
380
|
+
Ordered messages that fail to publish to the Pub/Sub API due to error will put
|
381
|
+
the `ordering_key` in a failed state, and future calls to
|
382
|
+
{Google::Cloud::PubSub::Topic#publish_async Topic#publish_async} with the
|
383
|
+
`ordering_key` will raise {Google::Cloud::PubSub::OrderingKeyError
|
384
|
+
OrderingKeyError}. To allow future messages with the `ordering_key` to be
|
385
|
+
published, the `ordering_key` must be passed to
|
386
|
+
{Google::Cloud::PubSub::Topic#resume_publish Topic#resume_publish}.
|
387
|
+
|
388
|
+
### Receiving Ordered Messages
|
389
|
+
|
390
|
+
To use ordering keys when subscribing to messages, the subscription must be
|
391
|
+
created with message ordering enabled (See
|
392
|
+
{Google::Cloud::PubSub::Topic#subscribe Topic#subscribe} and
|
393
|
+
{Google::Cloud::PubSub::Subscription#message_ordering?
|
394
|
+
Subscription#message_ordering?}) before calling
|
395
|
+
{Google::Cloud::PubSub::Subscription#listen Subscription#listen}. When enabled,
|
396
|
+
the subscriber will deliver messages with the same `ordering_key` in the order
|
397
|
+
they were published.
|
398
|
+
|
399
|
+
```ruby
|
400
|
+
require "google/cloud/pubsub"
|
401
|
+
|
402
|
+
pubsub = Google::Cloud::PubSub.new
|
403
|
+
|
404
|
+
sub = pubsub.subscription "my-ordered-topic-sub"
|
405
|
+
sub.message_ordering? #=> true
|
406
|
+
|
407
|
+
subscriber = sub.listen do |received_message|
|
408
|
+
# Messsages with the same ordering_key are received
|
409
|
+
# in the order in which they were published.
|
410
|
+
received_message.acknowledge!
|
411
|
+
end
|
412
|
+
|
413
|
+
# Start background threads that will call block passed to listen.
|
414
|
+
subscriber.start
|
415
|
+
|
416
|
+
# Shut down the subscriber when ready to stop receiving messages.
|
417
|
+
subscriber.stop!
|
418
|
+
```
|
419
|
+
|
420
|
+
## Minimizing API calls before receiving and acknowledging messages
|
421
|
+
|
422
|
+
A subscription object can be created without making any API calls by providing
|
423
|
+
the `skip_lookup` argument to {Google::Cloud::PubSub::Project#subscription
|
424
|
+
Project#subscription} or {Google::Cloud::PubSub::Topic#subscription
|
425
|
+
Topic#subscription}. A subscriber object can also be created without an API call
|
426
|
+
by providing the `deadline` optional argument to
|
427
|
+
{Google::Cloud::PubSub::Subscription#listen Subscription#listen}:
|
428
|
+
|
429
|
+
```ruby
|
430
|
+
require "google/cloud/pubsub"
|
431
|
+
|
432
|
+
pubsub = Google::Cloud::PubSub.new
|
433
|
+
|
434
|
+
# No API call is made to retrieve the subscription resource.
|
435
|
+
sub = pubsub.subscription "my-topic-sub", skip_lookup: true
|
436
|
+
|
437
|
+
# No API call is made to retrieve the subscription deadline.
|
438
|
+
subscriber = sub.listen deadline: 60 do |received_message|
|
439
|
+
# process message
|
440
|
+
received_message.acknowledge!
|
441
|
+
end
|
442
|
+
|
443
|
+
# Start background threads that will call block passed to listen.
|
444
|
+
subscriber.start
|
445
|
+
|
446
|
+
# Shut down the subscriber when ready to stop receiving messages.
|
447
|
+
subscriber.stop!
|
448
|
+
```
|
449
|
+
|
450
|
+
Skipping API calls may be used to avoid `Google::Cloud::PermissionDeniedError`
|
451
|
+
if your account has limited access to the Pub/Sub API. In particular, the role
|
452
|
+
`roles/pubsub.subscriber` does not have the permission
|
453
|
+
`pubsub.subscriptions.get`, which is required to retrieve a subscription
|
454
|
+
resource. See [Access Control -
|
455
|
+
Roles](https://cloud.google.com/pubsub/docs/access-control#tbl_roles) for the
|
456
|
+
complete list of Pub/Sub roles and permissions.
|
457
|
+
|
458
|
+
## Creating a snapshot and using seek
|
459
|
+
|
460
|
+
You can create a snapshot to retain the existing backlog on a subscription. The
|
461
|
+
snapshot will hold the messages in the subscription's backlog that are
|
462
|
+
unacknowledged upon the successful completion of the `create_snapshot`
|
463
|
+
operation.
|
464
|
+
|
465
|
+
Later, you can use `seek` to reset the subscription's backlog to the snapshot.
|
466
|
+
|
467
|
+
(See {Google::Cloud::PubSub::Subscription#create_snapshot
|
468
|
+
Subscription#create_snapshot} and {Google::Cloud::PubSub::Subscription#seek
|
469
|
+
Subscription#seek})
|
470
|
+
|
471
|
+
```ruby
|
472
|
+
require "google/cloud/pubsub"
|
473
|
+
|
474
|
+
pubsub = Google::Cloud::PubSub.new
|
475
|
+
|
476
|
+
sub = pubsub.subscription "my-topic-sub"
|
477
|
+
|
478
|
+
snapshot = sub.create_snapshot
|
479
|
+
|
480
|
+
received_messages = sub.pull immediate: false
|
481
|
+
sub.acknowledge received_messages
|
482
|
+
|
483
|
+
sub.seek snapshot
|
484
|
+
```
|
485
|
+
|
486
|
+
## Working Across Projects
|
487
|
+
|
488
|
+
All calls to the Pub/Sub service use the same project and credentials provided
|
489
|
+
to the {Google::Cloud::PubSub.new PubSub.new} method. However, it is common to
|
490
|
+
reference topics or subscriptions in other projects, which can be achieved by
|
491
|
+
using the `project` option. The main credentials must have permissions to the
|
492
|
+
topics and subscriptions in other projects.
|
493
|
+
|
494
|
+
```ruby
|
495
|
+
require "google/cloud/pubsub"
|
496
|
+
|
497
|
+
pubsub = Google::Cloud::PubSub.new # my-project
|
498
|
+
|
499
|
+
# Get a topic in the current project
|
500
|
+
my_topic = pubsub.topic "my-topic"
|
501
|
+
my_topic.name #=> "projects/my-project/topics/my-topic"
|
502
|
+
# Get a topic in another project
|
503
|
+
other_topic = pubsub.topic "other-topic", project: "other-project-id"
|
504
|
+
other_topic.name #=> "projects/other-project-id/topics/other-topic"
|
505
|
+
```
|
506
|
+
|
507
|
+
It is possible to create a subscription in the current project that pulls
|
508
|
+
from a topic in another project:
|
509
|
+
|
510
|
+
```ruby
|
511
|
+
require "google/cloud/pubsub"
|
512
|
+
|
513
|
+
pubsub = Google::Cloud::PubSub.new # my-project
|
514
|
+
|
515
|
+
# Get a topic in another project
|
516
|
+
topic = pubsub.topic "other-topic", project: "other-project-id"
|
517
|
+
# Create a subscription in the current project that pulls from
|
518
|
+
# the topic in another project
|
519
|
+
sub = topic.subscribe "my-sub"
|
520
|
+
sub.name #=> "projects/my-project/subscriptions/my-sub"
|
521
|
+
sub.topic.name #=> "projects/other-project-id/topics/other-topic"
|
522
|
+
```
|
523
|
+
|
524
|
+
## Additional information
|
525
|
+
|
526
|
+
Google Cloud Pub/Sub can be configured to use an emulator or to enable gRPC's
|
527
|
+
logging. To learn more, see the {file:EMULATOR.md Emulator guide}} and
|
528
|
+
{file:LOGGING.md Logging guide}.
|