-
Notifications
You must be signed in to change notification settings - Fork 227
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sample: add samples for dead letter topics (#1010)
* feat: added docs for DQL samples * feat: samples for DLQ * test:added test cases for dql * fix: lint issue * fix: review changes Co-authored-by: Megan Potter <[email protected]>
- Loading branch information
1 parent
1e8ced3
commit 85f17a1
Showing
5 changed files
with
383 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// Copyright 2020 Google LLC | ||
// | ||
// 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 Google Cloud Pub/Sub API. | ||
* | ||
* For more information, see the README.md under /pubsub and the documentation | ||
* at https://cloud.google.com/pubsub/docs. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
// sample-metadata: | ||
// title: Create Subscription With Dead Letter Policy | ||
// description: Creates a new subscription With Dead Letter Policy. | ||
// usage: node createSubscriptionWithDeadLetterPolicy.js <topic-name> <subscription-name> <dead-letter-topic-name> | ||
|
||
function main( | ||
topicName = 'YOUR_TOPIC_NAME', | ||
subscriptionName = 'YOUR_SUBSCRIPTION_NAME', | ||
deadLetterTopicName = 'YOUR_DEAD_LETTER_TOPIC_NAME' | ||
) { | ||
// [START pubsub_dead_letter_create_subscription] | ||
/** | ||
* TODO(developer): Uncomment these variables before running the sample. | ||
*/ | ||
// const topicName = 'YOUR_TOPIC_NAME'; | ||
// const subscriptionName = 'YOUR_SUBSCRIPTION_NAME'; | ||
// const deadLetterTopicName = 'YOUR_DEAD_LETTER_TOPIC_NAME'; | ||
|
||
// Imports the Google Cloud client library | ||
const {PubSub} = require('@google-cloud/pubsub'); | ||
|
||
// Creates a client; cache this for further use | ||
const pubSubClient = new PubSub(); | ||
|
||
async function createSubscriptionWithDeadLetterPolicy() { | ||
// Creates a new subscription | ||
await pubSubClient.topic(topicName).createSubscription(subscriptionName, { | ||
deadLetterPolicy: { | ||
deadLetterTopic: pubSubClient.topic(deadLetterTopicName).name, | ||
maxDeliveryAttempts: 10, | ||
}, | ||
}); | ||
console.log( | ||
`Created subscription ${subscriptionName} with dead letter topic ${deadLetterTopicName}.` | ||
); | ||
console.log( | ||
'To process dead letter messages, remember to add a subscription to your dead letter topic.' | ||
); | ||
} | ||
|
||
createSubscriptionWithDeadLetterPolicy().catch(console.error); | ||
// [END pubsub_dead_letter_create_subscription] | ||
} | ||
|
||
main(...process.argv.slice(2)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Copyright 2020 Google LLC | ||
// | ||
// 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 Google Cloud Pub/Sub API. | ||
* | ||
* For more information, see the README.md under /pubsub and the documentation | ||
* at https://cloud.google.com/pubsub/docs. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
// sample-metadata: | ||
// title: Remove Dead Letter Policy | ||
// description: Remove Dead Letter Policy from subscription. | ||
// usage: node removeDeadLetterPolicy.js <topic-name> <subscription-name> | ||
|
||
function main( | ||
topicName = 'YOUR_TOPIC_NAME', | ||
subscriptionName = 'YOUR_SUBSCRIPTION_NAME' | ||
) { | ||
// [START pubsub_dead_letter_remove] | ||
/** | ||
* TODO(developer): Uncomment these variables before running the sample. | ||
*/ | ||
// const topicName = 'YOUR_TOPIC_NAME'; | ||
// const subscriptionName = 'YOUR_SUBSCRIPTION_NAME'; | ||
|
||
// Imports the Google Cloud client library | ||
const {PubSub} = require('@google-cloud/pubsub'); | ||
|
||
// Creates a client; cache this for further use | ||
const pubSubClient = new PubSub(); | ||
|
||
async function removeDeadLetterPolicy() { | ||
const metadata = { | ||
deadLetterPolicy: null, | ||
}; | ||
|
||
await pubSubClient | ||
.topic(topicName) | ||
.subscription(subscriptionName) | ||
.setMetadata(metadata); | ||
|
||
console.log( | ||
`Removed dead letter topic from ${subscriptionName} subscription.` | ||
); | ||
} | ||
|
||
removeDeadLetterPolicy().catch(console.error); | ||
// [END pubsub_dead_letter_remove] | ||
} | ||
|
||
main(...process.argv.slice(2)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// Copyright 2020 Google LLC | ||
// | ||
// 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 Google Cloud Pub/Sub API. | ||
* | ||
* For more information, see the README.md under /pubsub and the documentation | ||
* at https://cloud.google.com/pubsub/docs. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
// sample-metadata: | ||
// title: Synchronous Pull with delivery attempt. | ||
// description: Receive messages synchronously with delivery attempt. | ||
// usage: node synchronousPullWithDeliveryAttempts.js <project-id> <subscription-name> | ||
|
||
function main( | ||
projectId = 'YOUR_PROJECT_ID', | ||
subscriptionName = 'YOUR_SUBSCRIPTION_NAME' | ||
) { | ||
// [START pubsub_dead_letter_delivery_attempt] | ||
/** | ||
* TODO(developer): Uncomment these variables before running the sample. | ||
*/ | ||
// const projectId = 'YOUR_PROJECT_ID'; | ||
// const subscriptionName = 'YOUR_SUBSCRIPTION_NAME'; | ||
|
||
// Imports the Google Cloud client library. v1 is for the lower level | ||
// proto access. | ||
const {v1} = require('@google-cloud/pubsub'); | ||
|
||
// Creates a client; cache this for further use. | ||
const subClient = new v1.SubscriberClient(); | ||
|
||
async function synchronousPullWithDeliveryAttempts() { | ||
const formattedSubscription = subClient.subscriptionPath( | ||
projectId, | ||
subscriptionName | ||
); | ||
|
||
// The maximum number of messages returned for this request. | ||
// Pub/Sub may return fewer than the number specified. | ||
const request = { | ||
subscription: formattedSubscription, | ||
maxMessages: 10, | ||
}; | ||
|
||
// The subscriber pulls a specified number of messages. | ||
const [response] = await subClient.pull(request); | ||
|
||
// Process the messages. | ||
const ackIds = []; | ||
for (const message of response.receivedMessages) { | ||
console.log(`Received message: ${message.message.data}`); | ||
console.log(`Delivery Attempt: ${message.deliveryAttempt}`); | ||
ackIds.push(message.ackId); | ||
} | ||
|
||
// Acknowledge all of the messages. You could also ackknowledge | ||
// these individually, but this is more efficient. | ||
const ackRequest = { | ||
subscription: formattedSubscription, | ||
ackIds: ackIds, | ||
}; | ||
await subClient.acknowledge(ackRequest); | ||
|
||
console.log('Done.'); | ||
} | ||
|
||
synchronousPullWithDeliveryAttempts().catch(console.error); | ||
// [END pubsub_dead_letter_delivery_attempt] | ||
} | ||
|
||
main(...process.argv.slice(2)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.