Skip to content

Add sample for updating a subscription. #1335

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 2, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions pubsub/cloud-client/subscriber.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,34 @@ def delete_subscription(project, subscription_name):
print('Subscription deleted: {}'.format(subscription_path))


def update_subscription(project, subscription_name, ack_deadline_seconds):
"""
Updates an existing Pub/Sub subscription's ackDeadlineSeconds
from 10 seconds (default). Note that certain properties of a
subscription, such as its topic, are not modifiable.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are the fields that are mutable and non mutable documented anywhere?

"""
subscriber = pubsub_v1.SubscriberClient()
subscription_path = subscriber.subscription_path(
project, subscription_name)

subscription = pubsub_v1.types.Subscription(
name=subscription_path,
ack_deadline_seconds=ack_deadline_seconds)

update_mask = {
'paths': {
'ack_deadline_seconds',
}
}

subscriber.update_subscription(subscription, update_mask)
result = subscriber.get_subscription(subscription_path)

print('Subscription updated: {}'.format(subscription_path))
print('New ack_deadline_seconds value is: {}'.format(
result.ack_deadline_seconds))


def receive_messages(project, subscription_name):
"""Receives messages from a pull subscription."""
subscriber = pubsub_v1.SubscriberClient()
Expand Down Expand Up @@ -183,6 +211,11 @@ def callback(message):
'delete', help=delete_subscription.__doc__)
delete_parser.add_argument('subscription_name')

update_parser = subparsers.add_parser(
'update', help=update_subscription.__doc__)
update_parser.add_argument('subscription_name')
update_parser.add_argument('ack_deadline_seconds', type=int)

receive_parser = subparsers.add_parser(
'receive', help=receive_messages.__doc__)
receive_parser.add_argument('subscription_name')
Expand Down Expand Up @@ -214,6 +247,9 @@ def callback(message):
elif args.command == 'delete':
delete_subscription(
args.project, args.subscription_name)
elif args.command == 'update':
update_subscription(
args.project, args.subscription_name, args.ack_deadline_seconds)
elif args.command == 'receive':
receive_messages(args.project, args.subscription_name)
elif args.command == 'receive-flow-control':
Expand Down