-
Notifications
You must be signed in to change notification settings - Fork 207
/
subscriber.py
1093 lines (906 loc) · 42.4 KB
/
subscriber.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# Copyright 2016 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""This application demonstrates how to perform basic operations on
subscriptions with the Cloud Pub/Sub API.
For more information, see the README.md under /pubsub and the documentation
at https://cloud.google.com/pubsub/docs.
"""
import argparse
import typing
from typing import Optional
if typing.TYPE_CHECKING:
from google.pubsub_v1 import types as gapic_types
def list_subscriptions_in_topic(project_id: str, topic_id: str) -> None:
"""Lists all subscriptions for a given topic."""
# [START pubsub_list_topic_subscriptions]
from google.cloud import pubsub_v1
# TODO(developer)
# project_id = "your-project-id"
# topic_id = "your-topic-id"
publisher = pubsub_v1.PublisherClient()
topic_path = publisher.topic_path(project_id, topic_id)
response = publisher.list_topic_subscriptions(request={"topic": topic_path})
for subscription in response:
print(subscription)
# [END pubsub_list_topic_subscriptions]
def list_subscriptions_in_project(project_id: str) -> None:
"""Lists all subscriptions in the current project."""
# [START pubsub_list_subscriptions]
from google.cloud import pubsub_v1
# TODO(developer)
# project_id = "your-project-id"
subscriber = pubsub_v1.SubscriberClient()
project_path = f"projects/{project_id}"
# Wrap the subscriber in a 'with' block to automatically call close() to
# close the underlying gRPC channel when done.
with subscriber:
for subscription in subscriber.list_subscriptions(
request={"project": project_path}
):
print(subscription.name)
# [END pubsub_list_subscriptions]
def create_subscription(project_id: str, topic_id: str, subscription_id: str) -> None:
"""Create a new pull subscription on the given topic."""
# [START pubsub_create_pull_subscription]
from google.cloud import pubsub_v1
# TODO(developer)
# project_id = "your-project-id"
# topic_id = "your-topic-id"
# subscription_id = "your-subscription-id"
publisher = pubsub_v1.PublisherClient()
subscriber = pubsub_v1.SubscriberClient()
topic_path = publisher.topic_path(project_id, topic_id)
subscription_path = subscriber.subscription_path(project_id, subscription_id)
# Wrap the subscriber in a 'with' block to automatically call close() to
# close the underlying gRPC channel when done.
with subscriber:
subscription = subscriber.create_subscription(
request={"name": subscription_path, "topic": topic_path}
)
print(f"Subscription created: {subscription}")
# [END pubsub_create_pull_subscription]
def create_subscription_with_dead_letter_topic(
project_id: str,
topic_id: str,
subscription_id: str,
dead_letter_topic_id: str,
max_delivery_attempts: int = 5,
) -> None:
"""Create a subscription with dead letter policy."""
# [START pubsub_dead_letter_create_subscription]
from google.cloud import pubsub_v1
from google.cloud.pubsub_v1.types import DeadLetterPolicy
# TODO(developer)
# project_id = "your-project-id"
# endpoint = "https://my-test-project.appspot.com/push"
# TODO(developer): This is an existing topic that the subscription
# with dead letter policy is attached to.
# topic_id = "your-topic-id"
# TODO(developer): This is an existing subscription with a dead letter policy.
# subscription_id = "your-subscription-id"
# TODO(developer): This is an existing dead letter topic that the subscription
# with dead letter policy will forward dead letter messages to.
# dead_letter_topic_id = "your-dead-letter-topic-id"
# TODO(developer): This is the maximum number of delivery attempts allowed
# for a message before it gets delivered to a dead letter topic.
# max_delivery_attempts = 5
publisher = pubsub_v1.PublisherClient()
subscriber = pubsub_v1.SubscriberClient()
topic_path = publisher.topic_path(project_id, topic_id)
subscription_path = subscriber.subscription_path(project_id, subscription_id)
dead_letter_topic_path = publisher.topic_path(project_id, dead_letter_topic_id)
dead_letter_policy = DeadLetterPolicy(
dead_letter_topic=dead_letter_topic_path,
max_delivery_attempts=max_delivery_attempts,
)
with subscriber:
request = {
"name": subscription_path,
"topic": topic_path,
"dead_letter_policy": dead_letter_policy,
}
subscription = subscriber.create_subscription(request)
print(f"Subscription created: {subscription.name}")
print(
f"It will forward dead letter messages to: {subscription.dead_letter_policy.dead_letter_topic}."
)
print(
f"After {subscription.dead_letter_policy.max_delivery_attempts} delivery attempts."
)
# [END pubsub_dead_letter_create_subscription]
def create_push_subscription(
project_id: str, topic_id: str, subscription_id: str, endpoint: str
) -> None:
"""Create a new push subscription on the given topic."""
# [START pubsub_create_push_subscription]
from google.cloud import pubsub_v1
# TODO(developer)
# project_id = "your-project-id"
# topic_id = "your-topic-id"
# subscription_id = "your-subscription-id"
# endpoint = "https://my-test-project.appspot.com/push"
publisher = pubsub_v1.PublisherClient()
subscriber = pubsub_v1.SubscriberClient()
topic_path = publisher.topic_path(project_id, topic_id)
subscription_path = subscriber.subscription_path(project_id, subscription_id)
push_config = pubsub_v1.types.PushConfig(push_endpoint=endpoint)
# Wrap the subscriber in a 'with' block to automatically call close() to
# close the underlying gRPC channel when done.
with subscriber:
subscription = subscriber.create_subscription(
request={
"name": subscription_path,
"topic": topic_path,
"push_config": push_config,
}
)
print(f"Push subscription created: {subscription}.")
print(f"Endpoint for subscription is: {endpoint}")
# [END pubsub_create_push_subscription]
def create_subscription_with_ordering(
project_id: str, topic_id: str, subscription_id: str
) -> None:
"""Create a subscription with ordering enabled."""
# [START pubsub_enable_subscription_ordering]
from google.cloud import pubsub_v1
# TODO(developer): Choose an existing topic.
# project_id = "your-project-id"
# topic_id = "your-topic-id"
# subscription_id = "your-subscription-id"
publisher = pubsub_v1.PublisherClient()
subscriber = pubsub_v1.SubscriberClient()
topic_path = publisher.topic_path(project_id, topic_id)
subscription_path = subscriber.subscription_path(project_id, subscription_id)
with subscriber:
subscription = subscriber.create_subscription(
request={
"name": subscription_path,
"topic": topic_path,
"enable_message_ordering": True,
}
)
print(f"Created subscription with ordering: {subscription}")
# [END pubsub_enable_subscription_ordering]
def create_subscription_with_filtering(
project_id: str, topic_id: str, subscription_id: str, filter: str,
) -> None:
"""Create a subscription with filtering enabled."""
# [START pubsub_create_subscription_with_filter]
from google.cloud import pubsub_v1
# TODO(developer): Choose an existing topic.
# project_id = "your-project-id"
# topic_id = "your-topic-id"
# subscription_id = "your-subscription-id"
# filter = "attributes.author=\"unknown\""
publisher = pubsub_v1.PublisherClient()
subscriber = pubsub_v1.SubscriberClient()
topic_path = publisher.topic_path(project_id, topic_id)
subscription_path = subscriber.subscription_path(project_id, subscription_id)
with subscriber:
subscription = subscriber.create_subscription(
request={"name": subscription_path, "topic": topic_path, "filter": filter}
)
print(f"Created subscription with filtering enabled: {subscription}")
# [END pubsub_create_subscription_with_filter]
def create_subscription_with_exactly_once_delivery(
project_id: str, topic_id: str, subscription_id: str
) -> None:
"""Create a subscription with exactly once delivery enabled."""
# [START pubsub_create_subscription_with_exactly_once_delivery]
from google.cloud import pubsub_v1
# TODO(developer): Choose an existing topic.
# project_id = "your-project-id"
# topic_id = "your-topic-id"
# subscription_id = "your-subscription-id"
publisher = pubsub_v1.PublisherClient()
subscriber = pubsub_v1.SubscriberClient()
topic_path = publisher.topic_path(project_id, topic_id)
subscription_path = subscriber.subscription_path(project_id, subscription_id)
with subscriber:
subscription = subscriber.create_subscription(
request={
"name": subscription_path,
"topic": topic_path,
"enable_exactly_once_delivery": True,
}
)
print(
f"Created subscription with exactly once delivery enabled: {subscription}"
)
# [END pubsub_create_subscription_with_exactly_once_delivery]
def delete_subscription(project_id: str, subscription_id: str) -> None:
"""Deletes an existing Pub/Sub topic."""
# [START pubsub_delete_subscription]
from google.cloud import pubsub_v1
# TODO(developer)
# project_id = "your-project-id"
# subscription_id = "your-subscription-id"
subscriber = pubsub_v1.SubscriberClient()
subscription_path = subscriber.subscription_path(project_id, subscription_id)
# Wrap the subscriber in a 'with' block to automatically call close() to
# close the underlying gRPC channel when done.
with subscriber:
subscriber.delete_subscription(request={"subscription": subscription_path})
print(f"Subscription deleted: {subscription_path}.")
# [END pubsub_delete_subscription]
def update_push_subscription(
project_id: str, topic_id: str, subscription_id: str, endpoint: str
) -> None:
"""
Updates an existing Pub/Sub subscription's push endpoint URL.
Note that certain properties of a subscription, such as
its topic, are not modifiable.
"""
# [START pubsub_update_push_configuration]
from google.cloud import pubsub_v1
# TODO(developer)
# project_id = "your-project-id"
# topic_id = "your-topic-id"
# subscription_id = "your-subscription-id"
# endpoint = "https://my-test-project.appspot.com/push"
subscriber = pubsub_v1.SubscriberClient()
subscription_path = subscriber.subscription_path(project_id, subscription_id)
push_config = pubsub_v1.types.PushConfig(push_endpoint=endpoint)
subscription = pubsub_v1.types.Subscription(
name=subscription_path, topic=topic_id, push_config=push_config
)
update_mask = {"paths": {"push_config"}}
# Wrap the subscriber in a 'with' block to automatically call close() to
# close the underlying gRPC channel when done.
with subscriber:
result = subscriber.update_subscription(
request={"subscription": subscription, "update_mask": update_mask}
)
print(f"Subscription updated: {subscription_path}")
print(f"New endpoint for subscription is: {result.push_config}.")
# [END pubsub_update_push_configuration]
def update_subscription_with_dead_letter_policy(
project_id: str,
topic_id: str,
subscription_id: str,
dead_letter_topic_id: str,
max_delivery_attempts: int = 5,
) -> "gapic_types.Subscription":
"""Update a subscription's dead letter policy."""
# [START pubsub_dead_letter_update_subscription]
from google.cloud import pubsub_v1
from google.cloud.pubsub_v1.types import DeadLetterPolicy, FieldMask
# TODO(developer)
# project_id = "your-project-id"
# TODO(developer): This is an existing topic that the subscription
# with dead letter policy is attached to.
# topic_id = "your-topic-id"
# TODO(developer): This is an existing subscription with a dead letter policy.
# subscription_id = "your-subscription-id"
# TODO(developer): This is an existing dead letter topic that the subscription
# with dead letter policy will forward dead letter messages to.
# dead_letter_topic_id = "your-dead-letter-topic-id"
# TODO(developer): This is the maximum number of delivery attempts allowed
# for a message before it gets delivered to a dead letter topic.
# max_delivery_attempts = 5
publisher = pubsub_v1.PublisherClient()
subscriber = pubsub_v1.SubscriberClient()
topic_path = publisher.topic_path(project_id, topic_id)
subscription_path = subscriber.subscription_path(project_id, subscription_id)
dead_letter_topic_path = publisher.topic_path(project_id, dead_letter_topic_id)
subscription_before_update = subscriber.get_subscription(
request={"subscription": subscription_path}
)
print(f"Before the update: {subscription_before_update}.")
# Indicates which fields in the provided subscription to update.
update_mask = FieldMask(paths=["dead_letter_policy"])
# Construct a dead letter policy you expect to have after the update.
dead_letter_policy = DeadLetterPolicy(
dead_letter_topic=dead_letter_topic_path,
max_delivery_attempts=max_delivery_attempts,
)
# Construct the subscription with the dead letter policy you expect to have
# after the update. Here, values in the required fields (name, topic) help
# identify the subscription.
subscription = pubsub_v1.types.Subscription(
name=subscription_path, topic=topic_path, dead_letter_policy=dead_letter_policy,
)
with subscriber:
subscription_after_update = subscriber.update_subscription(
request={"subscription": subscription, "update_mask": update_mask}
)
print(f"After the update: {subscription_after_update}.")
# [END pubsub_dead_letter_update_subscription]
return subscription_after_update
def remove_dead_letter_policy(
project_id: str, topic_id: str, subscription_id: str
) -> "gapic_types.Subscription":
"""Remove dead letter policy from a subscription."""
# [START pubsub_dead_letter_remove]
from google.cloud import pubsub_v1
from google.cloud.pubsub_v1.types import FieldMask
# TODO(developer)
# project_id = "your-project-id"
# TODO(developer): This is an existing topic that the subscription
# with dead letter policy is attached to.
# topic_id = "your-topic-id"
# TODO(developer): This is an existing subscription with a dead letter policy.
# subscription_id = "your-subscription-id"
publisher = pubsub_v1.PublisherClient()
subscriber = pubsub_v1.SubscriberClient()
topic_path = publisher.topic_path(project_id, topic_id)
subscription_path = subscriber.subscription_path(project_id, subscription_id)
subscription_before_update = subscriber.get_subscription(
request={"subscription": subscription_path}
)
print(f"Before removing the policy: {subscription_before_update}.")
# Indicates which fields in the provided subscription to update.
update_mask = FieldMask(paths=["dead_letter_policy"])
# Construct the subscription (without any dead letter policy) that you
# expect to have after the update.
subscription = pubsub_v1.types.Subscription(
name=subscription_path, topic=topic_path
)
with subscriber:
subscription_after_update = subscriber.update_subscription(
request={"subscription": subscription, "update_mask": update_mask}
)
print(f"After removing the policy: {subscription_after_update}.")
# [END pubsub_dead_letter_remove]
return subscription_after_update
def receive_messages(
project_id: str, subscription_id: str, timeout: Optional[float] = None
) -> None:
"""Receives messages from a pull subscription."""
# [START pubsub_subscriber_async_pull]
# [START pubsub_quickstart_subscriber]
from concurrent.futures import TimeoutError
from google.cloud import pubsub_v1
# TODO(developer)
# project_id = "your-project-id"
# subscription_id = "your-subscription-id"
# Number of seconds the subscriber should listen for messages
# timeout = 5.0
subscriber = pubsub_v1.SubscriberClient()
# The `subscription_path` method creates a fully qualified identifier
# in the form `projects/{project_id}/subscriptions/{subscription_id}`
subscription_path = subscriber.subscription_path(project_id, subscription_id)
def callback(message: pubsub_v1.subscriber.message.Message) -> None:
print(f"Received {message}.")
message.ack()
streaming_pull_future = subscriber.subscribe(subscription_path, callback=callback)
print(f"Listening for messages on {subscription_path}..\n")
# Wrap subscriber in a 'with' block to automatically call close() when done.
with subscriber:
try:
# When `timeout` is not set, result() will block indefinitely,
# unless an exception is encountered first.
streaming_pull_future.result(timeout=timeout)
except TimeoutError:
streaming_pull_future.cancel() # Trigger the shutdown.
streaming_pull_future.result() # Block until the shutdown is complete.
# [END pubsub_subscriber_async_pull]
# [END pubsub_quickstart_subscriber]
def receive_messages_with_custom_attributes(
project_id: str, subscription_id: str, timeout: Optional[float] = None
) -> None:
"""Receives messages from a pull subscription."""
# [START pubsub_subscriber_async_pull_custom_attributes]
from concurrent.futures import TimeoutError
from google.cloud import pubsub_v1
# TODO(developer)
# project_id = "your-project-id"
# subscription_id = "your-subscription-id"
# Number of seconds the subscriber should listen for messages
# timeout = 5.0
subscriber = pubsub_v1.SubscriberClient()
subscription_path = subscriber.subscription_path(project_id, subscription_id)
def callback(message: pubsub_v1.subscriber.message.Message) -> None:
print(f"Received {message.data!r}.")
if message.attributes:
print("Attributes:")
for key in message.attributes:
value = message.attributes.get(key)
print(f"{key}: {value}")
message.ack()
streaming_pull_future = subscriber.subscribe(subscription_path, callback=callback)
print(f"Listening for messages on {subscription_path}..\n")
# Wrap subscriber in a 'with' block to automatically call close() when done.
with subscriber:
try:
# When `timeout` is not set, result() will block indefinitely,
# unless an exception is encountered first.
streaming_pull_future.result(timeout=timeout)
except TimeoutError:
streaming_pull_future.cancel() # Trigger the shutdown.
streaming_pull_future.result() # Block until the shutdown is complete.
# [END pubsub_subscriber_async_pull_custom_attributes]
def receive_messages_with_flow_control(
project_id: str, subscription_id: str, timeout: Optional[float] = None
) -> None:
"""Receives messages from a pull subscription with flow control."""
# [START pubsub_subscriber_flow_settings]
from concurrent.futures import TimeoutError
from google.cloud import pubsub_v1
# TODO(developer)
# project_id = "your-project-id"
# subscription_id = "your-subscription-id"
# Number of seconds the subscriber should listen for messages
# timeout = 5.0
subscriber = pubsub_v1.SubscriberClient()
subscription_path = subscriber.subscription_path(project_id, subscription_id)
def callback(message: pubsub_v1.subscriber.message.Message) -> None:
print(f"Received {message.data!r}.")
message.ack()
# Limit the subscriber to only have ten outstanding messages at a time.
flow_control = pubsub_v1.types.FlowControl(max_messages=10)
streaming_pull_future = subscriber.subscribe(
subscription_path, callback=callback, flow_control=flow_control
)
print(f"Listening for messages on {subscription_path}..\n")
# Wrap subscriber in a 'with' block to automatically call close() when done.
with subscriber:
try:
# When `timeout` is not set, result() will block indefinitely,
# unless an exception is encountered first.
streaming_pull_future.result(timeout=timeout)
except TimeoutError:
streaming_pull_future.cancel() # Trigger the shutdown.
streaming_pull_future.result() # Block until the shutdown is complete.
# [END pubsub_subscriber_flow_settings]
def receive_messages_with_blocking_shutdown(
project_id: str, subscription_id: str, timeout: float = 5.0
) -> None:
"""Shuts down a pull subscription by awaiting message callbacks to complete."""
# [START pubsub_subscriber_blocking_shutdown]
import time
from concurrent.futures import TimeoutError
from google.cloud import pubsub_v1
# TODO(developer)
# project_id = "your-project-id"
# subscription_id = "your-subscription-id"
# Number of seconds the subscriber should listen for messages
# timeout = 5.0
subscriber = pubsub_v1.SubscriberClient()
subscription_path = subscriber.subscription_path(project_id, subscription_id)
def callback(message: pubsub_v1.subscriber.message.Message) -> None:
print(f"Received {message.data!r}.")
time.sleep(timeout + 3.0) # Pocess longer than streaming pull future timeout.
message.ack()
print(f"Done processing the message {message.data!r}.")
streaming_pull_future = subscriber.subscribe(
subscription_path, callback=callback, await_callbacks_on_shutdown=True,
)
print(f"Listening for messages on {subscription_path}..\n")
# Wrap subscriber in a 'with' block to automatically call close() when done.
with subscriber:
try:
# When `timeout` is not set, result() will block indefinitely,
# unless an exception is encountered first.
streaming_pull_future.result(timeout=timeout)
except TimeoutError:
streaming_pull_future.cancel()
print("Streaming pull future canceled.")
streaming_pull_future.result() # Blocks until shutdown complete.
print("Done waiting for the stream shutdown.")
# The "Done waiting..." message is only printed *after* the processing of all
# received messages has completed.
# [END pubsub_subscriber_blocking_shutdown]
def receive_messages_with_exactly_once_delivery_enabled(
project_id: str, subscription_id: str, timeout: Optional[float] = None
) -> None:
"""Receives messages from a pull subscription with exactly-once delivery enabled."""
# [START pubsub_subscriber_exactly_once]
from concurrent.futures import TimeoutError
from google.cloud import pubsub_v1
from google.cloud.pubsub_v1.subscriber import exceptions as sub_exceptions
# TODO(developer)
# project_id = "your-project-id"
# subscription_id = "your-subscription-id"
# Number of seconds the subscriber should listen for messages
# timeout = 5.0
subscriber = pubsub_v1.SubscriberClient()
# The `subscription_path` method creates a fully qualified identifier
# in the form `projects/{project_id}/subscriptions/{subscription_id}`
subscription_path = subscriber.subscription_path(project_id, subscription_id)
def callback(message: pubsub_v1.subscriber.message.Message) -> None:
print(f"Received {message}.")
# Use `ack_with_response()` instead of `ack()` to get a future that tracks
# the result of the acknowledge call. When exactly-once delivery is enabled
# on the subscription, the message is guaranteed to not be delivered again
# if the ack future succeeds.
ack_future = message.ack_with_response()
try:
# Block on result of acknowledge call.
# When `timeout` is not set, result() will block indefinitely,
# unless an exception is encountered first.
ack_future.result(timeout=timeout)
print(f"Ack for message {message.message_id} successful.")
except sub_exceptions.AcknowledgeError as e:
print(
f"Ack for message {message.message_id} failed with error: {e.error_code}"
)
streaming_pull_future = subscriber.subscribe(subscription_path, callback=callback)
print(f"Listening for messages on {subscription_path}..\n")
# Wrap subscriber in a 'with' block to automatically call close() when done.
with subscriber:
try:
# When `timeout` is not set, result() will block indefinitely,
# unless an exception is encountered first.
streaming_pull_future.result(timeout=timeout)
except TimeoutError:
streaming_pull_future.cancel() # Trigger the shutdown.
streaming_pull_future.result() # Block until the shutdown is complete.
# [END pubsub_subscriber_exactly_once]
def synchronous_pull(project_id: str, subscription_id: str) -> None:
"""Pulling messages synchronously."""
# [START pubsub_subscriber_sync_pull]
from google.api_core import retry
from google.cloud import pubsub_v1
# TODO(developer)
# project_id = "your-project-id"
# subscription_id = "your-subscription-id"
subscriber = pubsub_v1.SubscriberClient()
subscription_path = subscriber.subscription_path(project_id, subscription_id)
NUM_MESSAGES = 3
# Wrap the subscriber in a 'with' block to automatically call close() to
# close the underlying gRPC channel when done.
with subscriber:
# The subscriber pulls a specific number of messages. The actual
# number of messages pulled may be smaller than max_messages.
response = subscriber.pull(
request={"subscription": subscription_path, "max_messages": NUM_MESSAGES},
retry=retry.Retry(deadline=300),
)
if len(response.received_messages) == 0:
return
ack_ids = []
for received_message in response.received_messages:
print(f"Received: {received_message.message.data}.")
ack_ids.append(received_message.ack_id)
# Acknowledges the received messages so they will not be sent again.
subscriber.acknowledge(
request={"subscription": subscription_path, "ack_ids": ack_ids}
)
print(
f"Received and acknowledged {len(response.received_messages)} messages from {subscription_path}."
)
# [END pubsub_subscriber_sync_pull]
def synchronous_pull_with_lease_management(
project_id: str, subscription_id: str
) -> None:
"""Pulling messages synchronously with lease management"""
# [START pubsub_subscriber_sync_pull_with_lease]
import logging
import multiprocessing
import sys
import time
from google.api_core import retry
from google.cloud import pubsub_v1
multiprocessing.log_to_stderr()
logger = multiprocessing.get_logger()
logger.setLevel(logging.INFO)
processes = dict()
# TODO(developer)
# project_id = "your-project-id"
# subscription_id = "your-subscription-id"
subscriber = pubsub_v1.SubscriberClient()
subscription_path = subscriber.subscription_path(project_id, subscription_id)
response = subscriber.pull(
request={"subscription": subscription_path, "max_messages": 3},
retry=retry.Retry(deadline=300),
)
if len(response.received_messages) == 0:
return
# Start a process for each message based on its size modulo 10.
for message in response.received_messages:
process = multiprocessing.Process(
target=time.sleep, args=(sys.getsizeof(message) % 10,)
)
processes[process] = (message.ack_id, message.message.data)
process.start()
while processes:
# Take a break every second.
if processes:
time.sleep(1)
for process in list(processes):
ack_id, msg_data = processes[process]
# If the process is running, reset the ack deadline.
if process.is_alive():
subscriber.modify_ack_deadline(
request={
"subscription": subscription_path,
"ack_ids": [ack_id],
# Must be between 10 and 600.
"ack_deadline_seconds": 15,
}
)
logger.info(f"Reset ack deadline for {msg_data}.")
# If the process is complete, acknowledge the message.
else:
subscriber.acknowledge(
request={"subscription": subscription_path, "ack_ids": [ack_id]}
)
logger.info(f"Acknowledged {msg_data}.")
processes.pop(process)
print(
f"Received and acknowledged {len(response.received_messages)} messages from {subscription_path}."
)
# Close the underlying gPRC channel. Alternatively, wrap subscriber in
# a 'with' block to automatically call close() when done.
subscriber.close()
# [END pubsub_subscriber_sync_pull_with_lease]
def listen_for_errors(
project_id: str, subscription_id: str, timeout: Optional[float] = None
) -> None:
"""Receives messages and catches errors from a pull subscription."""
# [START pubsub_subscriber_error_listener]
from google.cloud import pubsub_v1
# TODO(developer)
# project_id = "your-project-id"
# subscription_id = "your-subscription-id"
# Number of seconds the subscriber should listen for messages
# timeout = 5.0
subscriber = pubsub_v1.SubscriberClient()
subscription_path = subscriber.subscription_path(project_id, subscription_id)
def callback(message: pubsub_v1.subscriber.message.Message) -> None:
print(f"Received {message}.")
message.ack()
streaming_pull_future = subscriber.subscribe(subscription_path, callback=callback)
print(f"Listening for messages on {subscription_path}..\n")
# Wrap subscriber in a 'with' block to automatically call close() when done.
with subscriber:
# When `timeout` is not set, result() will block indefinitely,
# unless an exception is encountered first.
try:
streaming_pull_future.result(timeout=timeout)
except Exception as e:
print(
f"Listening for messages on {subscription_path} threw an exception: {e}."
)
streaming_pull_future.cancel() # Trigger the shutdown.
streaming_pull_future.result() # Block until the shutdown is complete.
# [END pubsub_subscriber_error_listener]
def receive_messages_with_delivery_attempts(
project_id: str, subscription_id: str, timeout: Optional[float] = None
) -> None:
# [START pubsub_dead_letter_delivery_attempt]
from concurrent.futures import TimeoutError
from google.cloud import pubsub_v1
# TODO(developer)
# project_id = "your-project-id"
# subscription_id = "your-subscription-id"
subscriber = pubsub_v1.SubscriberClient()
subscription_path = subscriber.subscription_path(project_id, subscription_id)
def callback(message: pubsub_v1.subscriber.message.Message) -> None:
print(f"Received {message}.")
print(f"With delivery attempts: {message.delivery_attempt}.")
message.ack()
streaming_pull_future = subscriber.subscribe(subscription_path, callback=callback)
print(f"Listening for messages on {subscription_path}..\n")
# Wrap subscriber in a 'with' block to automatically call close() when done.
with subscriber:
# When `timeout` is not set, result() will block indefinitely,
# unless an exception is encountered first.
try:
streaming_pull_future.result(timeout=timeout)
except TimeoutError:
streaming_pull_future.cancel() # Trigger the shutdown.
streaming_pull_future.result() # Block until the shutdown is complete.
# [END pubsub_dead_letter_delivery_attempt]
if __name__ == "__main__": # noqa
parser = argparse.ArgumentParser(
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter,
)
parser.add_argument("project_id", help="Your Google Cloud project ID")
subparsers = parser.add_subparsers(dest="command")
list_in_topic_parser = subparsers.add_parser(
"list-in-topic", help=list_subscriptions_in_topic.__doc__
)
list_in_topic_parser.add_argument("topic_id")
list_in_project_parser = subparsers.add_parser(
"list-in-project", help=list_subscriptions_in_project.__doc__
)
create_parser = subparsers.add_parser("create", help=create_subscription.__doc__)
create_parser.add_argument("topic_id")
create_parser.add_argument("subscription_id")
create_with_dead_letter_policy_parser = subparsers.add_parser(
"create-with-dead-letter-policy",
help=create_subscription_with_dead_letter_topic.__doc__,
)
create_with_dead_letter_policy_parser.add_argument("topic_id")
create_with_dead_letter_policy_parser.add_argument("subscription_id")
create_with_dead_letter_policy_parser.add_argument("dead_letter_topic_id")
create_with_dead_letter_policy_parser.add_argument(
"max_delivery_attempts", type=int, nargs="?", default=5
)
create_push_parser = subparsers.add_parser(
"create-push", help=create_push_subscription.__doc__
)
create_push_parser.add_argument("topic_id")
create_push_parser.add_argument("subscription_id")
create_push_parser.add_argument("endpoint")
create_subscription_with_ordering_parser = subparsers.add_parser(
"create-with-ordering", help=create_subscription_with_ordering.__doc__
)
create_subscription_with_ordering_parser.add_argument("topic_id")
create_subscription_with_ordering_parser.add_argument("subscription_id")
create_subscription_with_filtering_parser = subparsers.add_parser(
"create-with-filtering", help=create_subscription_with_filtering.__doc__
)
create_subscription_with_filtering_parser.add_argument("topic_id")
create_subscription_with_filtering_parser.add_argument("subscription_id")
create_subscription_with_filtering_parser.add_argument("filter")
create_subscription_with_exactly_once_delivery_parser = subparsers.add_parser(
"create-with-exactly-once",
help=create_subscription_with_exactly_once_delivery.__doc__,
)
create_subscription_with_exactly_once_delivery_parser.add_argument("topic_id")
create_subscription_with_exactly_once_delivery_parser.add_argument(
"subscription_id"
)
delete_parser = subparsers.add_parser("delete", help=delete_subscription.__doc__)
delete_parser.add_argument("subscription_id")
update_push_parser = subparsers.add_parser(
"update-push", help=update_push_subscription.__doc__
)
update_push_parser.add_argument("topic_id")
update_push_parser.add_argument("subscription_id")
update_push_parser.add_argument("endpoint")
update_dead_letter_policy_parser = subparsers.add_parser(
"update-dead-letter-policy",
help=update_subscription_with_dead_letter_policy.__doc__,
)
update_dead_letter_policy_parser.add_argument("topic_id")
update_dead_letter_policy_parser.add_argument("subscription_id")
update_dead_letter_policy_parser.add_argument("dead_letter_topic_id")
update_dead_letter_policy_parser.add_argument(
"max_delivery_attempts", type=int, nargs="?", default=5
)
remove_dead_letter_policy_parser = subparsers.add_parser(
"remove-dead-letter-policy", help=remove_dead_letter_policy.__doc__
)
remove_dead_letter_policy_parser.add_argument("topic_id")
remove_dead_letter_policy_parser.add_argument("subscription_id")
receive_parser = subparsers.add_parser("receive", help=receive_messages.__doc__)
receive_parser.add_argument("subscription_id")
receive_parser.add_argument("timeout", default=None, type=float, nargs="?")
receive_with_custom_attributes_parser = subparsers.add_parser(
"receive-custom-attributes",
help=receive_messages_with_custom_attributes.__doc__,
)
receive_with_custom_attributes_parser.add_argument("subscription_id")
receive_with_custom_attributes_parser.add_argument(
"timeout", default=None, type=float, nargs="?"
)
receive_with_flow_control_parser = subparsers.add_parser(
"receive-flow-control", help=receive_messages_with_flow_control.__doc__
)
receive_with_flow_control_parser.add_argument("subscription_id")
receive_with_flow_control_parser.add_argument(
"timeout", default=None, type=float, nargs="?"
)
receive_with_blocking_shutdown_parser = subparsers.add_parser(
"receive-blocking-shutdown",
help=receive_messages_with_blocking_shutdown.__doc__,
)
receive_with_blocking_shutdown_parser.add_argument("subscription_id")
receive_with_blocking_shutdown_parser.add_argument(
"timeout", default=None, type=float, nargs="?"
)
receive_messages_with_exactly_once_delivery_enabled_parser = subparsers.add_parser(
"receive-messages-with-exactly-once-delivery-enabled",
help=receive_messages_with_exactly_once_delivery_enabled.__doc__,
)
receive_messages_with_exactly_once_delivery_enabled_parser.add_argument(
"subscription_id"
)
receive_messages_with_exactly_once_delivery_enabled_parser.add_argument(
"timeout", default=None, type=float, nargs="?"
)
synchronous_pull_parser = subparsers.add_parser(
"receive-synchronously", help=synchronous_pull.__doc__
)
synchronous_pull_parser.add_argument("subscription_id")
synchronous_pull_with_lease_management_parser = subparsers.add_parser(
"receive-synchronously-with-lease",
help=synchronous_pull_with_lease_management.__doc__,
)
synchronous_pull_with_lease_management_parser.add_argument("subscription_id")