|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# Copyright 2016 Google Inc. All Rights Reserved. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +"""This application demonstrates how to perform basic operations on |
| 18 | +subscriptions with the Cloud Pub/Sub API. |
| 19 | +
|
| 20 | +For more information, see the README.md under /pubsub and the documentation |
| 21 | +at https://cloud.google.com/pubsub/docs. |
| 22 | +""" |
| 23 | + |
| 24 | +import argparse |
| 25 | + |
| 26 | +from gcloud import pubsub |
| 27 | + |
| 28 | + |
| 29 | +def list_subscriptions(topic_name): |
| 30 | + """Lists all subscriptions for a given topic.""" |
| 31 | + pubsub_client = pubsub.Client() |
| 32 | + topic = pubsub_client.topic(topic_name) |
| 33 | + |
| 34 | + subscriptions = [] |
| 35 | + next_page_token = None |
| 36 | + while True: |
| 37 | + page, next_page_token = topic.list_subscriptions() |
| 38 | + subscriptions.extend(page) |
| 39 | + if not next_page_token: |
| 40 | + break |
| 41 | + |
| 42 | + for subscription in subscriptions: |
| 43 | + print(subscription.name) |
| 44 | + |
| 45 | + |
| 46 | +def create_subscription(topic_name, subscription_name): |
| 47 | + """Create a new pull subscription on the given topic.""" |
| 48 | + pubsub_client = pubsub.Client() |
| 49 | + topic = pubsub_client.topic(topic_name) |
| 50 | + |
| 51 | + subscription = topic.subscription(subscription_name) |
| 52 | + subscription.create() |
| 53 | + |
| 54 | + print('Subscription {} created on topic {}.'.format( |
| 55 | + subscription.name, topic.name)) |
| 56 | + |
| 57 | + |
| 58 | +def delete_subscription(topic_name, subscription_name): |
| 59 | + """Deletes an existing Pub/Sub topic.""" |
| 60 | + pubsub_client = pubsub.Client() |
| 61 | + topic = pubsub_client.topic(topic_name) |
| 62 | + subscription = topic.subscription(subscription_name) |
| 63 | + |
| 64 | + subscription.delete() |
| 65 | + |
| 66 | + print('Subscription {} deleted on topic {}.'.format( |
| 67 | + subscription.name, topic.name)) |
| 68 | + |
| 69 | + |
| 70 | +def receive_message(topic_name, subscription_name): |
| 71 | + """Receives a message from a pull subscription.""" |
| 72 | + pubsub_client = pubsub.Client() |
| 73 | + topic = pubsub_client.topic(topic_name) |
| 74 | + subscription = topic.subscription(subscription_name) |
| 75 | + |
| 76 | + # Change return_immediately=False to block until messages are |
| 77 | + # received. |
| 78 | + results = subscription.pull(return_immediately=True) |
| 79 | + |
| 80 | + print('Received {} messages.'.format(len(results))) |
| 81 | + |
| 82 | + for ack_id, message in results: |
| 83 | + print('* {}: {}, {}'.format( |
| 84 | + message.message_id, message.data, message.attributes)) |
| 85 | + |
| 86 | + # Acknowledge received messages. If you do not acknowledge, Pub/Sub will |
| 87 | + # redeliver the message. |
| 88 | + if results: |
| 89 | + subscription.acknowledge([ack_id for ack_id, message in results]) |
| 90 | + |
| 91 | + |
| 92 | +if __name__ == '__main__': |
| 93 | + parser = argparse.ArgumentParser( |
| 94 | + description=__doc__, |
| 95 | + formatter_class=argparse.RawDescriptionHelpFormatter |
| 96 | + ) |
| 97 | + |
| 98 | + subparsers = parser.add_subparsers(dest='command') |
| 99 | + list_parser = subparsers.add_parser( |
| 100 | + 'list', help=list_subscriptions.__doc__) |
| 101 | + list_parser.add_argument('topic_name') |
| 102 | + |
| 103 | + create_parser = subparsers.add_parser( |
| 104 | + 'create', help=create_subscription.__doc__) |
| 105 | + create_parser.add_argument('topic_name') |
| 106 | + create_parser.add_argument('subscription_name') |
| 107 | + |
| 108 | + delete_parser = subparsers.add_parser( |
| 109 | + 'delete', help=delete_subscription.__doc__) |
| 110 | + delete_parser.add_argument('topic_name') |
| 111 | + delete_parser.add_argument('subscription_name') |
| 112 | + |
| 113 | + receive_parser = subparsers.add_parser( |
| 114 | + 'receive', help=receive_message.__doc__) |
| 115 | + receive_parser.add_argument('topic_name') |
| 116 | + receive_parser.add_argument('subscription_name') |
| 117 | + |
| 118 | + args = parser.parse_args() |
| 119 | + |
| 120 | + if args.command == 'list': |
| 121 | + list_subscriptions(args.topic_name) |
| 122 | + elif args.command == 'create': |
| 123 | + create_subscription(args.topic_name, args.subscription_name) |
| 124 | + elif args.command == 'delete': |
| 125 | + delete_subscription(args.topic_name, args.subscription_name) |
| 126 | + elif args.command == 'receive': |
| 127 | + receive_message(args.topic_name, args.subscription_name) |
0 commit comments