|
| 1 | +// Copyright 2025 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +// This is a generated sample, using the typeless sample bot. Please |
| 16 | +// look for the source TypeScript sample (.ts) for modifications. |
| 17 | +'use strict'; |
| 18 | + |
| 19 | +/** |
| 20 | + * This sample demonstrates how to use the `timeout` option when closing a Pub/Sub |
| 21 | + * subscription using the Node.js client library. The timeout allows for graceful |
| 22 | + * shutdown, attempting to nack any buffered messages before closing. |
| 23 | + * |
| 24 | + * For more information, see the README.md under /pubsub and the documentation |
| 25 | + * at https://cloud.google.com/pubsub/docs. |
| 26 | + */ |
| 27 | + |
| 28 | +// sample-metadata: |
| 29 | +// title: Close Subscription with Timeout |
| 30 | +// description: Demonstrates closing a subscription with a specified timeout for graceful shutdown. |
| 31 | +// usage: node closeSubscriptionWithTimeout.js <topic-name> <subscription-name> |
| 32 | + |
| 33 | +// This sample is currently speculative. |
| 34 | +// -START pubsub_close_subscription_with_timeout] |
| 35 | + |
| 36 | +// Imports the Google Cloud client library |
| 37 | +const { |
| 38 | + PubSub, |
| 39 | + Duration, |
| 40 | + SubscriptionCloseBehaviors, |
| 41 | +} = require('@google-cloud/pubsub'); |
| 42 | + |
| 43 | +// Creates a client; cache this for further use |
| 44 | +const pubsub = new PubSub(); |
| 45 | + |
| 46 | +async function closeSubscriptionWithTimeout( |
| 47 | + topicNameOrId, |
| 48 | + subscriptionNameOrId, |
| 49 | +) { |
| 50 | + const topic = pubsub.topic(topicNameOrId); |
| 51 | + |
| 52 | + // Closes the subscription immediately, not waiting for anything. |
| 53 | + let subscription = topic.subscription(subscriptionNameOrId, { |
| 54 | + closeOptions: { |
| 55 | + timeout: Duration.from({seconds: 0}), |
| 56 | + }, |
| 57 | + }); |
| 58 | + await subscription.close(); |
| 59 | + |
| 60 | + // Shuts down the gRPC connection, and waits for just before the timeout |
| 61 | + // to send nacks for buffered messages. If `timeout` were missing, this |
| 62 | + // would wait for the maximum leasing timeout. |
| 63 | + subscription = topic.subscription(subscriptionNameOrId, { |
| 64 | + closeOptions: { |
| 65 | + behavior: SubscriptionCloseBehaviors.WaitForProcessing, |
| 66 | + timeout: Duration.from({seconds: 10}), |
| 67 | + }, |
| 68 | + }); |
| 69 | + await subscription.close(); |
| 70 | + |
| 71 | + // Shuts down the gRPC connection, sends nacks for buffered messages, and waits |
| 72 | + // through the timeout for nacks to send. |
| 73 | + subscription = topic.subscription(subscriptionNameOrId, { |
| 74 | + closeOptions: { |
| 75 | + behavior: SubscriptionCloseBehaviors.NackImmediately, |
| 76 | + timeout: Duration.from({seconds: 10}), |
| 77 | + }, |
| 78 | + }); |
| 79 | + await subscription.close(); |
| 80 | +} |
| 81 | +// -END pubsub_close_subscription_with_timeout] |
| 82 | + |
| 83 | +// Presumes topic and subscription have been created prior to running the sample. |
| 84 | +// If you uncomment the cleanup code above, the sample will delete them afterwards. |
| 85 | +function main( |
| 86 | + topicNameOrId = 'YOUR_TOPIC_NAME_OR_ID', |
| 87 | + subscriptionNameOrId = 'YOUR_SUBSCRIPTION_NAME_OR_ID', |
| 88 | +) { |
| 89 | + closeSubscriptionWithTimeout(topicNameOrId, subscriptionNameOrId).catch( |
| 90 | + err => { |
| 91 | + console.error(err.message); |
| 92 | + process.exitCode = 1; |
| 93 | + }, |
| 94 | + ); |
| 95 | +} |
| 96 | + |
| 97 | +main(...process.argv.slice(2)); |
0 commit comments