Skip to content

Commit

Permalink
samples: add Pub/Sub dead letter queue samples (#3904)
Browse files Browse the repository at this point in the history
  • Loading branch information
anguillanneuf committed May 30, 2020
1 parent 159ee7f commit 879f220
Show file tree
Hide file tree
Showing 4 changed files with 471 additions and 209 deletions.
23 changes: 15 additions & 8 deletions pubsub/cloud-client/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ To run this sample:

.. code-block:: bash
$ python publisher.py
$ python publisher.py --help
usage: publisher.py [-h]
project_id
Expand Down Expand Up @@ -124,11 +124,11 @@ To run this sample:

.. code-block:: bash
$ python subscriber.py
$ python subscriber.py --help
usage: subscriber.py [-h]
project_id
{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}
{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}
...
This application demonstrates how to perform basic operations on
Expand All @@ -139,15 +139,21 @@ To run this sample:
positional arguments:
project_id Your Google Cloud project ID
{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}
list_in_topic Lists all subscriptions for a given topic.
list_in_project Lists all subscriptions in the current project.
{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}
list-in-topic Lists all subscriptions for a given topic.
list-in-project Lists all subscriptions in the current project.
create Create a new pull subscription on the given topic.
create-with-dead-letter-policy
Create a subscription with dead letter policy.
create-push Create a new push subscription on the given topic.
delete Deletes an existing Pub/Sub topic.
update Updates an existing Pub/Sub subscription's push
update-push Updates an existing Pub/Sub subscription's push
endpoint URL. Note that certain properties of a
subscription, such as its topic, are not modifiable.
update-dead-letter-policy
Update a subscription's dead letter policy.
remove-dead-letter-policy
Remove dead letter policy from a subscription.
receive Receives messages from a pull subscription.
receive-custom-attributes
Receives messages from a pull subscription.
Expand All @@ -158,8 +164,9 @@ To run this sample:
Pulling messages synchronously.
receive-synchronously-with-lease
Pulling messages synchronously with lease management
listen_for_errors Receives messages and catches errors from a pull
listen-for-errors Receives messages and catches errors from a pull
subscription.
receive-messages-with-delivery-attempts
optional arguments:
-h, --help show this help message and exit
Expand Down
52 changes: 27 additions & 25 deletions pubsub/cloud-client/publisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ def list_topics(project_id):
# [START pubsub_list_topics]
from google.cloud import pubsub_v1

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

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

# TODO project_id = "Your Google Cloud Project ID"
# TODO topic_name = "Your Pub/Sub topic name"
# TODO(developer)
# project_id = "your-project-id"
# topic_name = "your-topic-id"

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

# TODO project_id = "Your Google Cloud Project ID"
# TODO topic_name = "Your Pub/Sub topic name"
# TODO(developer)
# project_id = "your-project-id"
# topic_name = "your-topic-id"

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

# TODO project_id = "Your Google Cloud Project ID"
# TODO topic_name = "Your Pub/Sub topic name"
# TODO(developer)
# project_id = "your-project-id"
# topic_name = "your-topic-id"

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

# TODO project_id = "Your Google Cloud Project ID"
# TODO topic_name = "Your Pub/Sub topic name"
# TODO(developer)
# project_id = "your-project-id"
# topic_name = "your-topic-id"

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

from google.cloud import pubsub_v1

# TODO project_id = "Your Google Cloud Project ID"
# TODO topic_name = "Your Pub/Sub topic name"
# TODO(developer)
# project_id = "your-project-id"
# topic_name = "your-topic-id"

publisher = pubsub_v1.PublisherClient()
topic_path = publisher.topic_path(project_id, topic_name)
Expand Down Expand Up @@ -177,8 +183,9 @@ def publish_messages_with_batch_settings(project_id, topic_name):
# [START pubsub_publisher_batch_settings]
from google.cloud import pubsub_v1

# TODO project_id = "Your Google Cloud Project ID"
# TODO topic_name = "Your Pub/Sub topic name"
# TODO(developer)
# project_id = "your-project-id"
# topic_name = "your-topic-id"

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

# TODO project_id = "Your Google Cloud Project ID"
# TODO topic_name = "Your Pub/Sub topic name"
# TODO(developer)
# project_id = "your-project-id"
# topic_name = "your-topic-id"

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

if __name__ == "__main__":
parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter,
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter,
)
parser.add_argument("project_id", help="Your Google Cloud project ID")

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

publish_parser = subparsers.add_parser(
"publish", help=publish_messages.__doc__
)
publish_parser = subparsers.add_parser("publish", help=publish_messages.__doc__)
publish_parser.add_argument("topic_name")

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

publish_with_error_handler_parser = subparsers.add_parser(
"publish-with-error-handler",
help=publish_messages_with_error_handler.__doc__,
"publish-with-error-handler", help=publish_messages_with_error_handler.__doc__,
)
publish_with_error_handler_parser.add_argument("topic_name")

Expand All @@ -321,9 +325,7 @@ def publish_messages_with_retry_settings(project_id, topic_name):
elif args.command == "publish":
publish_messages(args.project_id, args.topic_name)
elif args.command == "publish-with-custom-attributes":
publish_messages_with_custom_attributes(
args.project_id, args.topic_name
)
publish_messages_with_custom_attributes(args.project_id, args.topic_name)
elif args.command == "publish-with-error-handler":
publish_messages_with_error_handler(args.project_id, args.topic_name)
elif args.command == "publish-with-batch-settings":
Expand Down
Loading

0 comments on commit 879f220

Please sign in to comment.