forked from GoogleCloudPlatform/python-docs-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubscriber.py
119 lines (89 loc) · 3.94 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
#!/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
from google.cloud import pubsub
def list_subscriptions(topic_name):
"""Lists all subscriptions for a given topic."""
pubsub_client = pubsub.Client()
topic = pubsub_client.topic(topic_name)
for subscription in topic.list_subscriptions():
print(subscription.name)
def create_subscription(topic_name, subscription_name):
"""Create a new pull subscription on the given topic."""
pubsub_client = pubsub.Client()
topic = pubsub_client.topic(topic_name)
subscription = topic.subscription(subscription_name)
subscription.create()
print('Subscription {} created on topic {}.'.format(
subscription.name, topic.name))
def delete_subscription(topic_name, subscription_name):
"""Deletes an existing Pub/Sub topic."""
pubsub_client = pubsub.Client()
topic = pubsub_client.topic(topic_name)
subscription = topic.subscription(subscription_name)
subscription.delete()
print('Subscription {} deleted on topic {}.'.format(
subscription.name, topic.name))
def receive_message(topic_name, subscription_name):
"""Receives a message from a pull subscription."""
pubsub_client = pubsub.Client()
topic = pubsub_client.topic(topic_name)
subscription = topic.subscription(subscription_name)
# Change return_immediately=False to block until messages are
# received.
results = subscription.pull(return_immediately=True)
print('Received {} messages.'.format(len(results)))
for ack_id, message in results:
print('* {}: {}, {}'.format(
message.message_id, message.data, message.attributes))
# Acknowledge received messages. If you do not acknowledge, Pub/Sub will
# redeliver the message.
if results:
subscription.acknowledge([ack_id for ack_id, message in results])
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter
)
subparsers = parser.add_subparsers(dest='command')
list_parser = subparsers.add_parser(
'list', help=list_subscriptions.__doc__)
list_parser.add_argument('topic_name')
create_parser = subparsers.add_parser(
'create', help=create_subscription.__doc__)
create_parser.add_argument('topic_name')
create_parser.add_argument('subscription_name')
delete_parser = subparsers.add_parser(
'delete', help=delete_subscription.__doc__)
delete_parser.add_argument('topic_name')
delete_parser.add_argument('subscription_name')
receive_parser = subparsers.add_parser(
'receive', help=receive_message.__doc__)
receive_parser.add_argument('topic_name')
receive_parser.add_argument('subscription_name')
args = parser.parse_args()
if args.command == 'list':
list_subscriptions(args.topic_name)
elif args.command == 'create':
create_subscription(args.topic_name, args.subscription_name)
elif args.command == 'delete':
delete_subscription(args.topic_name, args.subscription_name)
elif args.command == 'receive':
receive_message(args.topic_name, args.subscription_name)