Indrajeet Gour
Indrajeet Gour

Reputation: 4500

python pubsub subscription expiration

I have python code which create both topic and subscription, but I wanted to create the subscription with no expiration date currently, 31 days is the default value for subscription expiration if nothing is given at the time creating the subscription, my python code for the same:

publisher = pubsub_v1.PublisherClient()
subscriber = pubsub_v1.SubscriberClient()

project_path_topic = publisher.project_path('project')
project_path_sub = subscriber.project_path('project')

topic_path = publisher.topic_path('project', 'topic-name')
subscription_path = subscriber.subscription_path('project', 'subscription-name')

topic_path = publisher.topic_path(project_id, topic_name)
subscription_path = subscriber.subscription_path(project_id, sub_name)

# topic and sub creation
topic_created = publisher.create_topic(topic_path)
subscription = subscriber.create_subscription(subscription_path, topic_path)

Now, I wanted to override the expiration for never but I did not find any existing flag from the document which I could easily set in my python code.

There is the way which uses command line but I did not wanted to use that as my whole code is already written in python than why not to deal this through python api only.

Please help if you know how I can achieve it in the python code base, assuming all the langs will going to use same google backend apis only.(I could see that backend api it dealing with expiration policy(expirationPolicy) but did not see the same in python api doc)

Note - I am creating pull base subscription not push.

Upvotes: 1

Views: 1092

Answers (1)

guillaume blaquiere
guillaume blaquiere

Reputation: 75930

It's very bad documented, but I succeeded! Here the code

    update_mask = {"paths": {"expiration_policy"}}
    # expiration_policy = pubsub_v1.types.ExpirationPolicy(ttl= pubsub_v1.types.Duration(seconds=605000))
    expiration_policy = pubsub_v1.types.ExpirationPolicy()
    subscription = pubsub_v1.types.Subscription(name=subscription_path,  expiration_policy=expiration_policy)
    subscription = subscriber.update_subscription(request={"subscription": subscription, "update_mask": update_mask})

Upvotes: 4

Related Questions