Skip to content

Commit c93a485

Browse files
anguillanneufplamut
authored andcommitted
samples: add Pub/Sub dead letter queue samples [(#3904)](GoogleCloudPlatform/python-docs-samples#3904)
1 parent 1407699 commit c93a485

File tree

4 files changed

+471
-209
lines changed

4 files changed

+471
-209
lines changed

samples/snippets/README.rst

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ To run this sample:
7474

7575
.. code-block:: bash
7676
77-
$ python publisher.py
77+
$ python publisher.py --help
7878
7979
usage: publisher.py [-h]
8080
project_id
@@ -124,11 +124,11 @@ To run this sample:
124124

125125
.. code-block:: bash
126126
127-
$ python subscriber.py
127+
$ python subscriber.py --help
128128
129129
usage: subscriber.py [-h]
130130
project_id
131-
{list_in_topic,list_in_project,create,create-push,delete,update,receive,receive-custom-attributes,receive-flow-control,receive-synchronously,receive-synchronously-with-lease,listen_for_errors}
131+
{list-in-topic,list-in-project,create,create-with-dead-letter-policy,create-push,delete,update-push,update-dead-letter-policy,remove-dead-letter-policy,receive,receive-custom-attributes,receive-flow-control,receive-synchronously,receive-synchronously-with-lease,listen-for-errors,receive-messages-with-delivery-attempts}
132132
...
133133
134134
This application demonstrates how to perform basic operations on
@@ -139,15 +139,21 @@ To run this sample:
139139
140140
positional arguments:
141141
project_id Your Google Cloud project ID
142-
{list_in_topic,list_in_project,create,create-push,delete,update,receive,receive-custom-attributes,receive-flow-control,receive-synchronously,receive-synchronously-with-lease,listen_for_errors}
143-
list_in_topic Lists all subscriptions for a given topic.
144-
list_in_project Lists all subscriptions in the current project.
142+
{list-in-topic,list-in-project,create,create-with-dead-letter-policy,create-push,delete,update-push,update-dead-letter-policy,remove-dead-letter-policy,receive,receive-custom-attributes,receive-flow-control,receive-synchronously,receive-synchronously-with-lease,listen-for-errors,receive-messages-with-delivery-attempts}
143+
list-in-topic Lists all subscriptions for a given topic.
144+
list-in-project Lists all subscriptions in the current project.
145145
create Create a new pull subscription on the given topic.
146+
create-with-dead-letter-policy
147+
Create a subscription with dead letter policy.
146148
create-push Create a new push subscription on the given topic.
147149
delete Deletes an existing Pub/Sub topic.
148-
update Updates an existing Pub/Sub subscription's push
150+
update-push Updates an existing Pub/Sub subscription's push
149151
endpoint URL. Note that certain properties of a
150152
subscription, such as its topic, are not modifiable.
153+
update-dead-letter-policy
154+
Update a subscription's dead letter policy.
155+
remove-dead-letter-policy
156+
Remove dead letter policy from a subscription.
151157
receive Receives messages from a pull subscription.
152158
receive-custom-attributes
153159
Receives messages from a pull subscription.
@@ -158,8 +164,9 @@ To run this sample:
158164
Pulling messages synchronously.
159165
receive-synchronously-with-lease
160166
Pulling messages synchronously with lease management
161-
listen_for_errors Receives messages and catches errors from a pull
167+
listen-for-errors Receives messages and catches errors from a pull
162168
subscription.
169+
receive-messages-with-delivery-attempts
163170
164171
optional arguments:
165172
-h, --help show this help message and exit

samples/snippets/publisher.py

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ def list_topics(project_id):
2929
# [START pubsub_list_topics]
3030
from google.cloud import pubsub_v1
3131

32-
# TODO project_id = "Your Google Cloud Project ID"
32+
# TODO(developer)
33+
# project_id = "your-project-id"
3334

3435
publisher = pubsub_v1.PublisherClient()
3536
project_path = publisher.project_path(project_id)
@@ -45,8 +46,9 @@ def create_topic(project_id, topic_name):
4546
# [START pubsub_create_topic]
4647
from google.cloud import pubsub_v1
4748

48-
# TODO project_id = "Your Google Cloud Project ID"
49-
# TODO topic_name = "Your Pub/Sub topic name"
49+
# TODO(developer)
50+
# project_id = "your-project-id"
51+
# topic_name = "your-topic-id"
5052

5153
publisher = pubsub_v1.PublisherClient()
5254
topic_path = publisher.topic_path(project_id, topic_name)
@@ -63,8 +65,9 @@ def delete_topic(project_id, topic_name):
6365
# [START pubsub_delete_topic]
6466
from google.cloud import pubsub_v1
6567

66-
# TODO project_id = "Your Google Cloud Project ID"
67-
# TODO topic_name = "Your Pub/Sub topic name"
68+
# TODO(developer)
69+
# project_id = "your-project-id"
70+
# topic_name = "your-topic-id"
6871

6972
publisher = pubsub_v1.PublisherClient()
7073
topic_path = publisher.topic_path(project_id, topic_name)
@@ -81,8 +84,9 @@ def publish_messages(project_id, topic_name):
8184
# [START pubsub_publish]
8285
from google.cloud import pubsub_v1
8386

84-
# TODO project_id = "Your Google Cloud Project ID"
85-
# TODO topic_name = "Your Pub/Sub topic name"
87+
# TODO(developer)
88+
# project_id = "your-project-id"
89+
# topic_name = "your-topic-id"
8690

8791
publisher = pubsub_v1.PublisherClient()
8892
# The `topic_path` method creates a fully qualified identifier
@@ -108,8 +112,9 @@ def publish_messages_with_custom_attributes(project_id, topic_name):
108112
# [START pubsub_publish_custom_attributes]
109113
from google.cloud import pubsub_v1
110114

111-
# TODO project_id = "Your Google Cloud Project ID"
112-
# TODO topic_name = "Your Pub/Sub topic name"
115+
# TODO(developer)
116+
# project_id = "your-project-id"
117+
# topic_name = "your-topic-id"
113118

114119
publisher = pubsub_v1.PublisherClient()
115120
topic_path = publisher.topic_path(project_id, topic_name)
@@ -135,8 +140,9 @@ def publish_messages_with_error_handler(project_id, topic_name):
135140

136141
from google.cloud import pubsub_v1
137142

138-
# TODO project_id = "Your Google Cloud Project ID"
139-
# TODO topic_name = "Your Pub/Sub topic name"
143+
# TODO(developer)
144+
# project_id = "your-project-id"
145+
# topic_name = "your-topic-id"
140146

141147
publisher = pubsub_v1.PublisherClient()
142148
topic_path = publisher.topic_path(project_id, topic_name)
@@ -177,8 +183,9 @@ def publish_messages_with_batch_settings(project_id, topic_name):
177183
# [START pubsub_publisher_batch_settings]
178184
from google.cloud import pubsub_v1
179185

180-
# TODO project_id = "Your Google Cloud Project ID"
181-
# TODO topic_name = "Your Pub/Sub topic name"
186+
# TODO(developer)
187+
# project_id = "your-project-id"
188+
# topic_name = "your-topic-id"
182189

183190
# Configure the batch to publish as soon as there is ten messages,
184191
# one kilobyte of data, or one second has passed.
@@ -212,8 +219,9 @@ def publish_messages_with_retry_settings(project_id, topic_name):
212219
# [START pubsub_publisher_retry_settings]
213220
from google.cloud import pubsub_v1
214221

215-
# TODO project_id = "Your Google Cloud Project ID"
216-
# TODO topic_name = "Your Pub/Sub topic name"
222+
# TODO(developer)
223+
# project_id = "your-project-id"
224+
# topic_name = "your-topic-id"
217225

218226
# Configure the retry settings. Defaults will be overwritten.
219227
retry_settings = {
@@ -267,8 +275,7 @@ def publish_messages_with_retry_settings(project_id, topic_name):
267275

268276
if __name__ == "__main__":
269277
parser = argparse.ArgumentParser(
270-
description=__doc__,
271-
formatter_class=argparse.RawDescriptionHelpFormatter,
278+
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter,
272279
)
273280
parser.add_argument("project_id", help="Your Google Cloud project ID")
274281

@@ -281,9 +288,7 @@ def publish_messages_with_retry_settings(project_id, topic_name):
281288
delete_parser = subparsers.add_parser("delete", help=delete_topic.__doc__)
282289
delete_parser.add_argument("topic_name")
283290

284-
publish_parser = subparsers.add_parser(
285-
"publish", help=publish_messages.__doc__
286-
)
291+
publish_parser = subparsers.add_parser("publish", help=publish_messages.__doc__)
287292
publish_parser.add_argument("topic_name")
288293

289294
publish_with_custom_attributes_parser = subparsers.add_parser(
@@ -293,8 +298,7 @@ def publish_messages_with_retry_settings(project_id, topic_name):
293298
publish_with_custom_attributes_parser.add_argument("topic_name")
294299

295300
publish_with_error_handler_parser = subparsers.add_parser(
296-
"publish-with-error-handler",
297-
help=publish_messages_with_error_handler.__doc__,
301+
"publish-with-error-handler", help=publish_messages_with_error_handler.__doc__,
298302
)
299303
publish_with_error_handler_parser.add_argument("topic_name")
300304

@@ -321,9 +325,7 @@ def publish_messages_with_retry_settings(project_id, topic_name):
321325
elif args.command == "publish":
322326
publish_messages(args.project_id, args.topic_name)
323327
elif args.command == "publish-with-custom-attributes":
324-
publish_messages_with_custom_attributes(
325-
args.project_id, args.topic_name
326-
)
328+
publish_messages_with_custom_attributes(args.project_id, args.topic_name)
327329
elif args.command == "publish-with-error-handler":
328330
publish_messages_with_error_handler(args.project_id, args.topic_name)
329331
elif args.command == "publish-with-batch-settings":

0 commit comments

Comments
 (0)