Skip to content

Commit

Permalink
sample: add samples for dead letter topics (#1010)
Browse files Browse the repository at this point in the history
* 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
laljikanjareeya and feywind authored Jun 1, 2020
1 parent 1e8ced3 commit 85f17a1
Show file tree
Hide file tree
Showing 5 changed files with 383 additions and 0 deletions.
69 changes: 69 additions & 0 deletions samples/createSubscriptionWithDeadLetterPolicy.js
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));
66 changes: 66 additions & 0 deletions samples/removeDeadLetterPolicy.js
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));
87 changes: 87 additions & 0 deletions samples/synchronousPullWithDeliveryAttempts.js
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));
94 changes: 94 additions & 0 deletions samples/system-test/subscriptions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,14 @@ describe('subscriptions', () => {
const runId = uuid.v4();
const topicNameOne = `topic1-${runId}`;
const topicNameTwo = `topic2-${runId}`;
const topicNameThree = `topic3-${runId}`;
const subscriptionNameOne = `sub1-${runId}`;
const subscriptionNameTwo = `sub2-${runId}`;
const subscriptionNameThree = `sub3-${runId}`;
const subscriptionNameFour = `sub4-${runId}`;
const subscriptionNameFive = `sub5-${runId}`;
const subscriptionNameSix = `sub6-${runId}`;
const subscriptionNameSeven = `sub7-${runId}`;
const fullTopicNameOne = `projects/${projectId}/topics/${topicNameOne}`;
const fullSubscriptionNameOne = `projects/${projectId}/subscriptions/${subscriptionNameOne}`;
const fullSubscriptionNameTwo = `projects/${projectId}/subscriptions/${subscriptionNameTwo}`;
Expand All @@ -45,6 +49,7 @@ describe('subscriptions', () => {
return Promise.all([
pubsub.createTopic(topicNameOne),
pubsub.createTopic(topicNameTwo),
pubsub.createTopic(topicNameThree),
]);
});

Expand Down Expand Up @@ -285,4 +290,93 @@ describe('subscriptions', () => {
assert.ok(subscriptions);
assert(subscriptions.every(s => s.name !== fullSubscriptionNameOne));
});

it('should create a subscription with dead letter policy.', async () => {
const output = execSync(
`${commandFor(
'createSubscriptionWithDeadLetterPolicy'
)} ${topicNameTwo} ${subscriptionNameFive} ${topicNameThree}`
);
assert.include(
output,
`Created subscription ${subscriptionNameFive} with dead letter topic ${topicNameThree}.`
);
const [subscription] = await pubsub
.topic(topicNameTwo)
.subscription(subscriptionNameFive)
.get();
assert.strictEqual(
subscription.metadata.deadLetterPolicy.maxDeliveryAttempts,
10
);
});

it('should listen for messages synchronously with delivery attempts.', async () => {
await pubsub.topic(topicNameOne).createSubscription(subscriptionNameSix, {
deadLetterPolicy: {
deadLetterTopic: pubsub.topic(topicNameThree).name,
maxDeliveryAttempts: 10,
},
});

await pubsub.topic(topicNameOne).publish(Buffer.from('Hello, world!'));
const output = execSync(
`${commandFor(
'synchronousPullWithDeliveryAttempts'
)} ${projectId} ${subscriptionNameSix}`
);
assert.match(output, /Hello/);
assert.match(output, /Delivery Attempt: 1/);
});

it('should update a subscription with dead lettter policy.', async () => {
await pubsub
.topic(topicNameOne)
.subscription(subscriptionNameSeven, {
deadLetterPolicy: {
deadLetterTopic: pubsub.topic(topicNameThree).name,
maxDeliveryAttempts: 10,
},
})
.get({autoCreate: true});

execSync(
`${commandFor(
'updateDeadLetterPolicy'
)} ${topicNameOne} ${subscriptionNameSeven}`
);

const [subscription] = await pubsub
.topic(topicNameOne)
.subscription(subscriptionNameSeven)
.get();
assert.equal(
subscription.metadata.deadLetterPolicy.maxDeliveryAttempts,
15
);
});

it('should remove dead lettter policy.', async () => {
await pubsub
.topic(topicNameOne)
.subscription(subscriptionNameSeven, {
deadLetterPolicy: {
deadLetterTopic: pubsub.topic(topicNameThree).name,
maxDeliveryAttempts: 10,
},
})
.get({autoCreate: true});

execSync(
`${commandFor(
'removeDeadLetterPolicy'
)} ${topicNameOne} ${subscriptionNameSeven}`
);

const [subscription] = await pubsub
.topic(topicNameOne)
.subscription(subscriptionNameSeven)
.get();
assert.isNull(subscription.metadata.deadLetterPolicy);
});
});
Loading

0 comments on commit 85f17a1

Please sign in to comment.