google-cloud-pubsub 2.22.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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +27 -0
- data/OVERVIEW.md +188 -144
- data/lib/google/cloud/pubsub/admin_clients.rb +116 -0
- data/lib/google/cloud/pubsub/async_publisher.rb +9 -9
- data/lib/google/cloud/pubsub/batch_publisher.rb +4 -4
- data/lib/google/cloud/pubsub/errors.rb +3 -3
- data/lib/google/cloud/pubsub/message.rb +8 -8
- data/lib/google/cloud/pubsub/{subscriber → message_listener}/enumerator_queue.rb +1 -1
- data/lib/google/cloud/pubsub/{subscriber → message_listener}/inventory.rb +2 -4
- data/lib/google/cloud/pubsub/{subscriber → message_listener}/sequencer.rb +1 -1
- data/lib/google/cloud/pubsub/{subscriber → message_listener}/stream.rb +10 -10
- data/lib/google/cloud/pubsub/{subscriber → message_listener}/timed_unary_buffer.rb +1 -1
- data/lib/google/cloud/pubsub/message_listener.rb +413 -0
- data/lib/google/cloud/pubsub/project.rb +98 -531
- data/lib/google/cloud/pubsub/publisher.rb +373 -0
- data/lib/google/cloud/pubsub/received_message.rb +50 -45
- data/lib/google/cloud/pubsub/service.rb +24 -386
- data/lib/google/cloud/pubsub/subscriber.rb +442 -279
- data/lib/google/cloud/pubsub/version.rb +1 -1
- data/lib/google/cloud/pubsub.rb +8 -15
- data/lib/google-cloud-pubsub.rb +5 -4
- metadata +18 -26
- data/lib/google/cloud/pubsub/policy.rb +0 -188
- data/lib/google/cloud/pubsub/retry_policy.rb +0 -88
- data/lib/google/cloud/pubsub/schema/list.rb +0 -180
- data/lib/google/cloud/pubsub/schema.rb +0 -378
- data/lib/google/cloud/pubsub/snapshot/list.rb +0 -178
- data/lib/google/cloud/pubsub/snapshot.rb +0 -205
- data/lib/google/cloud/pubsub/subscription/list.rb +0 -205
- data/lib/google/cloud/pubsub/subscription/push_config.rb +0 -268
- data/lib/google/cloud/pubsub/subscription.rb +0 -1467
- data/lib/google/cloud/pubsub/topic/list.rb +0 -171
- data/lib/google/cloud/pubsub/topic.rb +0 -1100
data/OVERVIEW.md
CHANGED
@@ -19,96 +19,130 @@ require "google/cloud/pubsub"
|
|
19
19
|
|
20
20
|
pubsub = Google::Cloud::PubSub.new
|
21
21
|
|
22
|
-
|
23
|
-
|
22
|
+
publisher = pubsub.publisher "my-topic"
|
23
|
+
publisher.publish "topic-message"
|
24
|
+
|
25
|
+
subscriber = pubsub.subscriber "my-topic-sub"
|
26
|
+
subscriber.listen do |received_message|
|
27
|
+
puts "Message: #{received_message.message.data}"
|
28
|
+
received_message.acknowledge!
|
29
|
+
end
|
24
30
|
```
|
25
31
|
|
32
|
+
This guide provides an overview of the client library's operations, which are categorized
|
33
|
+
into Admin Operations and Data Plane Operations.
|
34
|
+
|
35
|
+
* **Admin Operations**: Used for creating, configuring, and managing Pub/Sub resources (topics, subscriptions, schemas).
|
36
|
+
* **Data Plane Operations**: For the core functionality of publishing and receiving messages.
|
37
|
+
|
26
38
|
To learn more about Pub/Sub, read the [Google Cloud Pub/Sub Overview
|
27
39
|
](https://cloud.google.com/pubsub/overview).
|
28
40
|
|
29
|
-
##
|
41
|
+
## Admin Operations
|
42
|
+
|
43
|
+
### Topic Admin Client
|
30
44
|
|
31
|
-
|
32
|
-
found by its name. (See {Google::Cloud::PubSub::Project#topic Project#topic})
|
45
|
+
Manages topic resources.
|
33
46
|
|
34
47
|
```ruby
|
35
48
|
require "google/cloud/pubsub"
|
36
49
|
|
37
|
-
pubsub = Google::Cloud::PubSub.new
|
38
|
-
|
50
|
+
pubsub = Google::Cloud::PubSub.new project_id: "my-project-id"
|
51
|
+
topic_admin = pubsub.topic_admin
|
39
52
|
```
|
40
53
|
|
41
|
-
|
54
|
+
#### Creating a Topic
|
42
55
|
|
43
|
-
A Topic is
|
44
|
-
|
56
|
+
A Topic is a named resource to which messages are sent by publishers. The resource must be created using
|
57
|
+
a topic admin client before it can be used.
|
45
58
|
|
46
59
|
```ruby
|
47
|
-
|
60
|
+
topic_path = pubsub.topic_path "my-topic"
|
61
|
+
topic = topic_admin.create_topic name: topic_path
|
48
62
|
|
49
|
-
|
50
|
-
topic = pubsub.create_topic "my-topic"
|
63
|
+
puts "Topic #{topic.name} created."
|
51
64
|
```
|
52
65
|
|
53
|
-
|
66
|
+
#### Retrieving a Topic
|
54
67
|
|
55
|
-
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})
|
68
|
+
A Topic is found by its full name.
|
59
69
|
|
60
70
|
```ruby
|
61
|
-
|
62
|
-
|
63
|
-
|
71
|
+
topic_name = "my-topic"
|
72
|
+
topic_path = pubsub.topic_path topic_name # Format is `projects/#{project_id}/topics/#{topic_name}`
|
73
|
+
topic = topic_admin.get_topic topic: topic_path
|
64
74
|
|
65
|
-
|
66
|
-
subscription = topic.subscription "my-topic-subscription"
|
67
|
-
puts subscription.name
|
75
|
+
puts "Topic: #{topic.name}."
|
68
76
|
```
|
69
77
|
|
70
|
-
|
78
|
+
### Subscription Admin Client
|
71
79
|
|
72
|
-
|
73
|
-
{Google::Cloud::PubSub::Topic#subscribe Topic#subscribe})
|
80
|
+
Manages subscription and snapshot resources.
|
74
81
|
|
75
82
|
```ruby
|
76
83
|
require "google/cloud/pubsub"
|
77
84
|
|
78
|
-
pubsub = Google::Cloud::PubSub.new
|
85
|
+
pubsub = Google::Cloud::PubSub.new project_id: "my-project-id"
|
86
|
+
subscription_admin = pubsub.subscription_admin
|
87
|
+
```
|
88
|
+
|
89
|
+
|
90
|
+
#### Creating a Subscription
|
91
|
+
|
92
|
+
A Subscription is a named resource representing the stream of messages from a
|
93
|
+
single, specific Topic, to be delivered to the subscribing application.
|
79
94
|
|
80
|
-
|
81
|
-
|
82
|
-
|
95
|
+
```ruby
|
96
|
+
topic_path = pubsub.topic_path "my-topic" # Already created Topic resource
|
97
|
+
subscription_path = pubsub.subscription_path "my-topic-subscription"
|
98
|
+
subscription = subscription_admin.create_subscription name: subscription_path, topic: topic_path
|
83
99
|
```
|
84
100
|
|
85
101
|
The subscription can be created that specifies the number of seconds to wait to
|
86
102
|
be acknowledged as well as an endpoint URL to push the messages to:
|
87
103
|
|
88
104
|
```ruby
|
89
|
-
|
105
|
+
topic_path = pubsub.topic_path "my-topic" # Already created Topic resource
|
106
|
+
subscription_path = pubsub.subscription_path "my-topic-subscription"
|
107
|
+
push_config = Google::Cloud::PubSub::V1::PushConfig.new push_endpoint: "https://example.com/push"
|
108
|
+
subscription = subscription_admin.create_subscription name: subscription_path, topic: topic_path,
|
109
|
+
push_config: push_config,
|
110
|
+
ack_deadline_seconds: 120
|
111
|
+
```
|
90
112
|
|
91
|
-
|
113
|
+
#### Retrieving Subscriptions
|
114
|
+
|
115
|
+
A Subscription is found by its name.
|
92
116
|
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
117
|
+
```ruby
|
118
|
+
subscription_path = pubsub.subscription_path "my-topic-subscription"
|
119
|
+
subscription = subscription_admin.get_subscription subscription: subscription_path
|
120
|
+
```
|
121
|
+
|
122
|
+
## Data Plane Operations
|
123
|
+
|
124
|
+
### Publisher Client
|
125
|
+
|
126
|
+
```ruby
|
127
|
+
require "google/cloud/pubsub"
|
128
|
+
|
129
|
+
pubsub = Google::Cloud::PubSub.new project_id: "my-project-id"
|
130
|
+
publisher = pubsub.publisher "my-topic"
|
97
131
|
```
|
98
132
|
|
99
|
-
|
133
|
+
#### Publishing Messages
|
100
134
|
|
101
135
|
Messages are published to a topic. Any message published to a topic without a
|
102
136
|
subscription will be lost. Ensure the topic has a subscription before
|
103
|
-
publishing.
|
137
|
+
publishing.
|
104
138
|
|
105
139
|
```ruby
|
106
140
|
require "google/cloud/pubsub"
|
107
141
|
|
108
142
|
pubsub = Google::Cloud::PubSub.new
|
109
143
|
|
110
|
-
|
111
|
-
msg =
|
144
|
+
publisher = pubsub.publisher "my-topic"
|
145
|
+
msg = publisher.publish "task completed"
|
112
146
|
```
|
113
147
|
|
114
148
|
Messages can also be published with attributes:
|
@@ -118,14 +152,14 @@ require "google/cloud/pubsub"
|
|
118
152
|
|
119
153
|
pubsub = Google::Cloud::PubSub.new
|
120
154
|
|
121
|
-
|
122
|
-
msg =
|
155
|
+
publisher = pubsub.publisher "my-topic"
|
156
|
+
msg = publisher.publish "task completed",
|
123
157
|
foo: :bar,
|
124
158
|
this: :that
|
125
159
|
```
|
126
160
|
|
127
161
|
Messages can also be published in batches asynchronously using `publish_async`.
|
128
|
-
(See {Google::Cloud::PubSub::
|
162
|
+
(See {Google::Cloud::PubSub::Publisher#publish_async Publisher#publish_async} and
|
129
163
|
{Google::Cloud::PubSub::AsyncPublisher AsyncPublisher})
|
130
164
|
|
131
165
|
```ruby
|
@@ -133,8 +167,8 @@ require "google/cloud/pubsub"
|
|
133
167
|
|
134
168
|
pubsub = Google::Cloud::PubSub.new
|
135
169
|
|
136
|
-
|
137
|
-
|
170
|
+
publisher = pubsub.publisher "my-topic"
|
171
|
+
publisher.publish_async "task completed" do |result|
|
138
172
|
if result.succeeded?
|
139
173
|
log_publish_success result.data
|
140
174
|
else
|
@@ -142,7 +176,7 @@ topic.publish_async "task completed" do |result|
|
|
142
176
|
end
|
143
177
|
end
|
144
178
|
|
145
|
-
|
179
|
+
publisher.async_publisher.stop!
|
146
180
|
```
|
147
181
|
|
148
182
|
Or multiple messages can be published in batches at the same time by passing a
|
@@ -153,57 +187,67 @@ require "google/cloud/pubsub"
|
|
153
187
|
|
154
188
|
pubsub = Google::Cloud::PubSub.new
|
155
189
|
|
156
|
-
|
157
|
-
msgs =
|
190
|
+
publisher = pubsub.publisher "my-topic"
|
191
|
+
msgs = publisher.publish do |batch|
|
158
192
|
batch.publish "task 1 completed", foo: :bar
|
159
193
|
batch.publish "task 2 completed", foo: :baz
|
160
194
|
batch.publish "task 3 completed", foo: :bif
|
161
195
|
end
|
162
196
|
```
|
163
197
|
|
164
|
-
|
198
|
+
### Subscriber Client
|
199
|
+
|
200
|
+
```ruby
|
201
|
+
require "google/cloud/pubsub"
|
202
|
+
|
203
|
+
pubsub = Google::Cloud::PubSub.new project_id: "my-project-id"
|
204
|
+
publisher = pubsub.subscriber "my-topic-subscription"
|
205
|
+
|
206
|
+
```
|
207
|
+
|
208
|
+
#### Receiving Messages
|
165
209
|
|
166
210
|
Messages can be streamed from a subscription with a subscriber object that is
|
167
|
-
created using `listen`. (See {Google::Cloud::PubSub::
|
168
|
-
|
211
|
+
created using `listen`. (See {Google::Cloud::PubSub::Subscriber#listen
|
212
|
+
Subscriber#listen} and {Google::Cloud::PubSub::MessageListener MessageListener})
|
169
213
|
|
170
214
|
```ruby
|
171
215
|
require "google/cloud/pubsub"
|
172
216
|
|
173
217
|
pubsub = Google::Cloud::PubSub.new
|
174
218
|
|
175
|
-
|
219
|
+
subscriber = pubsub.subscriber "my-topic-sub"
|
176
220
|
|
177
|
-
# Create a
|
221
|
+
# Create a MessageListener to listen for available messages.
|
178
222
|
# By default, this block will be called on 8 concurrent threads
|
179
223
|
# but this can be tuned with the `threads` option.
|
180
224
|
# The `streams` and `inventory` parameters allow further tuning.
|
181
|
-
|
225
|
+
listener = subscriber.listen threads: { callback: 16 } do |received_message|
|
182
226
|
# process message
|
183
227
|
puts "Data: #{received_message.message.data}, published at #{received_message.message.published_at}"
|
184
228
|
received_message.acknowledge!
|
185
229
|
end
|
186
230
|
|
187
231
|
# Handle exceptions from listener
|
188
|
-
|
232
|
+
listener.on_error do |exception|
|
189
233
|
puts "Exception: #{exception.class} #{exception.message}"
|
190
234
|
end
|
191
235
|
|
192
236
|
# Gracefully shut down the subscriber on program exit, blocking until
|
193
237
|
# all received messages have been processed or 10 seconds have passed
|
194
238
|
at_exit do
|
195
|
-
|
239
|
+
listener.stop!(10)
|
196
240
|
end
|
197
241
|
|
198
242
|
# Start background threads that will call the block passed to listen.
|
199
|
-
|
243
|
+
listener.start
|
200
244
|
|
201
245
|
# Block, letting processing threads continue in the background
|
202
246
|
sleep
|
203
247
|
```
|
204
248
|
|
205
249
|
Messages also can be pulled directly in a one-time operation. (See
|
206
|
-
{Google::Cloud::PubSub::
|
250
|
+
{Google::Cloud::PubSub::Subscriber#pull Subscriber#pull})
|
207
251
|
|
208
252
|
The `immediate: false` option is recommended to avoid adverse impacts on the
|
209
253
|
performance of pull operations.
|
@@ -213,8 +257,8 @@ require "google/cloud/pubsub"
|
|
213
257
|
|
214
258
|
pubsub = Google::Cloud::PubSub.new
|
215
259
|
|
216
|
-
|
217
|
-
received_messages =
|
260
|
+
subscriber = pubsub.subscriber "my-topic-sub"
|
261
|
+
received_messages = subscriber.pull immediate: false
|
218
262
|
```
|
219
263
|
|
220
264
|
A maximum number of messages to pull can be specified:
|
@@ -224,14 +268,14 @@ require "google/cloud/pubsub"
|
|
224
268
|
|
225
269
|
pubsub = Google::Cloud::PubSub.new
|
226
270
|
|
227
|
-
|
228
|
-
received_messages =
|
271
|
+
subscriber = pubsub.subscriber "my-topic-sub"
|
272
|
+
received_messages = subscriber.pull immediate: false, max: 10
|
229
273
|
```
|
230
274
|
|
231
|
-
|
275
|
+
#### Acknowledging a Message
|
232
276
|
|
233
|
-
Messages that are received can be acknowledged in Pub/Sub,
|
234
|
-
to
|
277
|
+
Messages that are received can be acknowledged in Pub/Sub, signaling the server
|
278
|
+
not to deliver them again.
|
235
279
|
|
236
280
|
A Message that can be acknowledged is called a ReceivedMessage. ReceivedMessages
|
237
281
|
can be acknowledged one at a time: (See
|
@@ -243,34 +287,34 @@ require "google/cloud/pubsub"
|
|
243
287
|
|
244
288
|
pubsub = Google::Cloud::PubSub.new
|
245
289
|
|
246
|
-
|
290
|
+
subscriber = pubsub.subscriber "my-topic-sub"
|
247
291
|
|
248
|
-
|
292
|
+
listener = subscriber.listen do |received_message|
|
249
293
|
# process message
|
250
294
|
received_message.acknowledge!
|
251
295
|
end
|
252
296
|
|
253
297
|
# Start background threads that will call the block passed to listen.
|
254
|
-
|
298
|
+
listener.start
|
255
299
|
|
256
300
|
# Shut down the subscriber when ready to stop receiving messages.
|
257
|
-
|
301
|
+
listener.stop!
|
258
302
|
```
|
259
303
|
|
260
304
|
Or, multiple messages can be acknowledged in a single API call: (See
|
261
|
-
{Google::Cloud::PubSub::
|
305
|
+
{Google::Cloud::PubSub::Subscriber#acknowledge Subscriber#acknowledge})
|
262
306
|
|
263
307
|
```ruby
|
264
308
|
require "google/cloud/pubsub"
|
265
309
|
|
266
310
|
pubsub = Google::Cloud::PubSub.new
|
267
311
|
|
268
|
-
|
269
|
-
received_messages =
|
270
|
-
|
312
|
+
subscriber = pubsub.subscriber "my-topic-sub"
|
313
|
+
received_messages = subscriber.pull immediate: false
|
314
|
+
subscriber.acknowledge received_messages
|
271
315
|
```
|
272
316
|
|
273
|
-
|
317
|
+
#### Modifying a Deadline
|
274
318
|
|
275
319
|
A message must be acknowledged after it is pulled, or Pub/Sub will mark the
|
276
320
|
message for redelivery. The message acknowledgement deadline can delayed if more
|
@@ -284,8 +328,8 @@ require "google/cloud/pubsub"
|
|
284
328
|
|
285
329
|
pubsub = Google::Cloud::PubSub.new
|
286
330
|
|
287
|
-
|
288
|
-
|
331
|
+
subscriber = pubsub.subscriber "my-topic-sub"
|
332
|
+
listener = subscriber.listen do |received_message|
|
289
333
|
puts received_message.message.data
|
290
334
|
|
291
335
|
# Delay for 2 minutes
|
@@ -293,10 +337,10 @@ subscriber = sub.listen do |received_message|
|
|
293
337
|
end
|
294
338
|
|
295
339
|
# Start background threads that will call the block passed to listen.
|
296
|
-
|
340
|
+
listener.start
|
297
341
|
|
298
342
|
# Shut down the subscriber when ready to stop receiving messages.
|
299
|
-
|
343
|
+
listener.stop!
|
300
344
|
```
|
301
345
|
|
302
346
|
The message can also be made available for immediate redelivery:
|
@@ -306,8 +350,8 @@ require "google/cloud/pubsub"
|
|
306
350
|
|
307
351
|
pubsub = Google::Cloud::PubSub.new
|
308
352
|
|
309
|
-
|
310
|
-
|
353
|
+
subscriber = pubsub.subscriber "my-topic-sub"
|
354
|
+
listener = subscriber.listen do |received_message|
|
311
355
|
puts received_message.message.data
|
312
356
|
|
313
357
|
# Mark for redelivery
|
@@ -315,27 +359,27 @@ subscriber = sub.listen do |received_message|
|
|
315
359
|
end
|
316
360
|
|
317
361
|
# Start background threads that will call the block passed to listen.
|
318
|
-
|
362
|
+
listener.start
|
319
363
|
|
320
364
|
# Shut down the subscriber when ready to stop receiving messages.
|
321
|
-
|
365
|
+
listener.stop!
|
322
366
|
```
|
323
367
|
|
324
368
|
Multiple messages can be delayed or made available for immediate redelivery:
|
325
|
-
(See {Google::Cloud::PubSub::
|
326
|
-
|
369
|
+
(See {Google::Cloud::PubSub::Subscriber#modify_ack_deadline
|
370
|
+
Subscriber#modify_ack_deadline})
|
327
371
|
|
328
372
|
```ruby
|
329
373
|
require "google/cloud/pubsub"
|
330
374
|
|
331
375
|
pubsub = Google::Cloud::PubSub.new
|
332
376
|
|
333
|
-
|
334
|
-
received_messages =
|
335
|
-
|
377
|
+
subscriber = pubsub.subscriber "my-topic-sub"
|
378
|
+
received_messages = subscriber.pull immediate: false
|
379
|
+
subscriber.modify_ack_deadline 120, received_messages
|
336
380
|
```
|
337
381
|
|
338
|
-
|
382
|
+
### Using Ordering Keys
|
339
383
|
|
340
384
|
Google Cloud Pub/Sub ordering keys provide the ability to ensure related
|
341
385
|
messages are sent to subscribers in the order in which they were published.
|
@@ -349,50 +393,47 @@ messages for different ordering keys across subscribers.
|
|
349
393
|
Note: At the time of this release, ordering keys are not yet publicly enabled
|
350
394
|
and requires special project enablements.
|
351
395
|
|
352
|
-
|
396
|
+
#### Publishing Ordered Messages
|
353
397
|
|
354
398
|
To use ordering keys when publishing messages, a call to
|
355
|
-
{Google::Cloud::PubSub::
|
356
|
-
|
357
|
-
must be provided when calling {Google::Cloud::PubSub::
|
358
|
-
|
399
|
+
{Google::Cloud::PubSub::Publisher#enable_message_ordering!
|
400
|
+
Publisher#enable_message_ordering!} must be made and the `ordering_key` argument
|
401
|
+
must be provided when calling {Google::Cloud::PubSub::Publisher#publish_async
|
402
|
+
Publisher#publish_async}.
|
359
403
|
|
360
404
|
```ruby
|
361
405
|
require "google/cloud/pubsub"
|
362
406
|
|
363
407
|
pubsub = Google::Cloud::PubSub.new
|
364
408
|
|
365
|
-
|
409
|
+
publisher = pubsub.publisher "my-ordered-topic"
|
366
410
|
|
367
411
|
# Ensure that message ordering is enabled.
|
368
|
-
|
412
|
+
publisher.enable_message_ordering!
|
369
413
|
|
370
414
|
# Publish an ordered message with an ordering key.
|
371
|
-
|
415
|
+
publisher.publish_async "task completed",
|
372
416
|
ordering_key: "task-key"
|
373
417
|
|
374
418
|
# Shut down the publisher when ready to stop publishing messages.
|
375
|
-
|
419
|
+
publisher.async_publisher.stop!
|
376
420
|
```
|
377
421
|
|
378
|
-
|
422
|
+
#### Handling errors with Ordered Keys
|
379
423
|
|
380
424
|
Ordered messages that fail to publish to the Pub/Sub API due to error will put
|
381
425
|
the `ordering_key` in a failed state, and future calls to
|
382
|
-
{Google::Cloud::PubSub::
|
426
|
+
{Google::Cloud::PubSub::Publisher#publish_async Publisher#publish_async} with the
|
383
427
|
`ordering_key` will raise {Google::Cloud::PubSub::OrderingKeyError
|
384
428
|
OrderingKeyError}. To allow future messages with the `ordering_key` to be
|
385
429
|
published, the `ordering_key` must be passed to
|
386
|
-
{Google::Cloud::PubSub::
|
430
|
+
{Google::Cloud::PubSub::Publisher#resume_publish Publisher#resume_publish}.
|
387
431
|
|
388
|
-
|
432
|
+
#### Receiving Ordered Messages
|
389
433
|
|
390
434
|
To use ordering keys when subscribing to messages, the subscription must be
|
391
|
-
created with message ordering enabled
|
392
|
-
{Google::Cloud::PubSub::
|
393
|
-
{Google::Cloud::PubSub::Subscription#message_ordering?
|
394
|
-
Subscription#message_ordering?}) before calling
|
395
|
-
{Google::Cloud::PubSub::Subscription#listen Subscription#listen}. When enabled,
|
435
|
+
created with message ordering enabled before calling
|
436
|
+
{Google::Cloud::PubSub::Subscriber#listen Subscriber#listen}. When enabled,
|
396
437
|
the subscriber will deliver messages with the same `ordering_key` in the order
|
397
438
|
they were published.
|
398
439
|
|
@@ -401,30 +442,31 @@ require "google/cloud/pubsub"
|
|
401
442
|
|
402
443
|
pubsub = Google::Cloud::PubSub.new
|
403
444
|
|
404
|
-
|
405
|
-
|
445
|
+
subscription = ... # "my-ordered-topic-sub" subscription with message ordering enabled
|
446
|
+
puts subscription.enable_message_ordering #=> true
|
406
447
|
|
407
|
-
subscriber =
|
448
|
+
subscriber = pubsub.subscriber "my-ordered-topic-sub"
|
449
|
+
|
450
|
+
listener = subscriber.listen do |received_message|
|
408
451
|
# Messsages with the same ordering_key are received
|
409
452
|
# in the order in which they were published.
|
410
453
|
received_message.acknowledge!
|
411
454
|
end
|
412
455
|
|
413
456
|
# Start background threads that will call block passed to listen.
|
414
|
-
|
457
|
+
listener.start
|
415
458
|
|
416
459
|
# Shut down the subscriber when ready to stop receiving messages.
|
417
|
-
|
460
|
+
listener.stop!
|
418
461
|
```
|
419
462
|
|
420
|
-
|
463
|
+
### Minimizing API calls before receiving and acknowledging messages
|
421
464
|
|
422
|
-
A
|
423
|
-
the `skip_lookup` argument to {Google::Cloud::PubSub::Project#
|
424
|
-
Project#
|
425
|
-
|
426
|
-
|
427
|
-
{Google::Cloud::PubSub::Subscription#listen Subscription#listen}:
|
465
|
+
A subscriber object can be created without making any API calls by providing
|
466
|
+
the `skip_lookup` argument to {Google::Cloud::PubSub::Project#subscriber
|
467
|
+
Project#subscriber}. A MessageListener object can also be created without an API
|
468
|
+
call by providing the `deadline` optional argument to
|
469
|
+
{Google::Cloud::PubSub::Subscriber#listen Subscriber#listen}
|
428
470
|
|
429
471
|
```ruby
|
430
472
|
require "google/cloud/pubsub"
|
@@ -432,19 +474,19 @@ require "google/cloud/pubsub"
|
|
432
474
|
pubsub = Google::Cloud::PubSub.new
|
433
475
|
|
434
476
|
# No API call is made to retrieve the subscription resource.
|
435
|
-
|
477
|
+
subscriber = pubsub.subscriber "my-topic-sub", skip_lookup: true
|
436
478
|
|
437
479
|
# No API call is made to retrieve the subscription deadline.
|
438
|
-
|
480
|
+
listener = subscriber.listen deadline: 60 do |received_message|
|
439
481
|
# process message
|
440
482
|
received_message.acknowledge!
|
441
483
|
end
|
442
484
|
|
443
485
|
# Start background threads that will call block passed to listen.
|
444
|
-
|
486
|
+
listener.start
|
445
487
|
|
446
488
|
# Shut down the subscriber when ready to stop receiving messages.
|
447
|
-
|
489
|
+
listener.stop!
|
448
490
|
```
|
449
491
|
|
450
492
|
Skipping API calls may be used to avoid `Google::Cloud::PermissionDeniedError`
|
@@ -464,30 +506,28 @@ operation.
|
|
464
506
|
|
465
507
|
Later, you can use `seek` to reset the subscription's backlog to the snapshot.
|
466
508
|
|
467
|
-
(See {Google::Cloud::PubSub::Subscription#create_snapshot
|
468
|
-
Subscription#create_snapshot} and {Google::Cloud::PubSub::Subscription#seek
|
469
|
-
Subscription#seek})
|
470
|
-
|
471
509
|
```ruby
|
472
510
|
require "google/cloud/pubsub"
|
473
511
|
|
474
512
|
pubsub = Google::Cloud::PubSub.new
|
475
513
|
|
476
|
-
|
477
|
-
|
478
|
-
|
514
|
+
subscription_admin = pubsub.subscription_admin
|
515
|
+
snapshot_path = pubsub.snapshot_path "my-snapshot"
|
516
|
+
subscription = ... # Already created Google::Cloud::PubSub::V1::Subscription
|
517
|
+
snapshot = subscription_admin.create_snapshot name: snapshot_path, subscription: subscription.name
|
479
518
|
|
519
|
+
subscriber = pubsub.subscriber "my-topic-sub"
|
480
520
|
received_messages = sub.pull immediate: false
|
481
|
-
|
521
|
+
subscriber.acknowledge received_messages
|
482
522
|
|
483
|
-
|
523
|
+
subcription_admin.seek subscription: subscription.name, snapshot: snapshot.name
|
484
524
|
```
|
485
525
|
|
486
526
|
## Working Across Projects
|
487
527
|
|
488
528
|
All calls to the Pub/Sub service use the same project and credentials provided
|
489
529
|
to the {Google::Cloud::PubSub.new PubSub.new} method. However, it is common to
|
490
|
-
reference
|
530
|
+
reference publishers or subscribers in other projects, which can be achieved by
|
491
531
|
using the `project` option. The main credentials must have permissions to the
|
492
532
|
topics and subscriptions in other projects.
|
493
533
|
|
@@ -496,12 +536,13 @@ require "google/cloud/pubsub"
|
|
496
536
|
|
497
537
|
pubsub = Google::Cloud::PubSub.new # my-project
|
498
538
|
|
499
|
-
# Get a topic in the current project
|
500
|
-
|
501
|
-
|
502
|
-
|
503
|
-
|
504
|
-
|
539
|
+
# Get a Publisher for a topic in the current project
|
540
|
+
publisher = pubsub.publisher "my-topic"
|
541
|
+
publisher.name #=> "projects/my-project/topics/my-topic"
|
542
|
+
|
543
|
+
# Get a Publisher for a topic in another project
|
544
|
+
other_publisher = pubsub.publisher "other-topic", project: "other-project-id"
|
545
|
+
other_publisher.name #=> "projects/other-project-id/topics/other-topic"
|
505
546
|
```
|
506
547
|
|
507
548
|
It is possible to create a subscription in the current project that pulls
|
@@ -511,14 +552,17 @@ from a topic in another project:
|
|
511
552
|
require "google/cloud/pubsub"
|
512
553
|
|
513
554
|
pubsub = Google::Cloud::PubSub.new # my-project
|
555
|
+
subscription_admin = pubsub.subscription_admin
|
514
556
|
|
515
|
-
# Get a topic in another project
|
516
|
-
|
557
|
+
# Get a Publisher for a topic in another project
|
558
|
+
publisher = pubsub.publisher "other-topic", project: "other-project-id"
|
517
559
|
# Create a subscription in the current project that pulls from
|
518
560
|
# the topic in another project
|
519
|
-
|
520
|
-
|
521
|
-
|
561
|
+
subscription_path = pubsub.subscription_path "my-sub"
|
562
|
+
subscription = subscription_admin.create_subscription name: subscription_path, topic: publisher.name
|
563
|
+
|
564
|
+
subscription.name #=> "projects/my-project/subscriptions/my-sub"
|
565
|
+
publisher.name #=> "projects/other-project-id/topics/other-topic"
|
522
566
|
```
|
523
567
|
|
524
568
|
## Additional information
|