Skip to content
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

docs(samples): Publish with Retry Setting Example #355

Merged
merged 13 commits into from
Nov 22, 2018
21 changes: 11 additions & 10 deletions samples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,17 @@ __Usage:__ `node topics.js --help`
topics.js <command>
Commands:
topics.js list Lists all topics in the current project.
topics.js create <topicName> Creates a new topic.
topics.js delete <topicName> Deletes a topic.
topics.js publish <topicName> <message> Publishes a message to a topic.
topics.js publish-attributes <topicName> <message> Publishes a message with custom attributes to a Topic
topics.js publish-batch <topicName> <message> Publishes messages to a topic using custom batching settings.
topics.js publish-ordered <topicName> <message> Publishes an ordered message to a topic.
topics.js get-policy <topicName> Gets the IAM policy for a topic.
topics.js set-policy <topicName> Sets the IAM policy for a topic.
topics.js test-permissions <topicName> Tests the permissions for a topic.
topics.js list Lists all topics in the current project.
topics.js create <topicName> Creates a new topic.
topics.js delete <topicName> Deletes a topic.
topics.js publish <topicName> <message> Publishes a message to a topic.
topics.js publish-attributes <topicName> <message> Publishes a message with custom attributes to a Topic
topics.js publish-batch <topicName> <message> Publishes messages to a topic using custom batching settings.
topics.js publish-retry <projectId> <topicName> <message> Publishes a message to a topic with retry settings.
topics.js publish-ordered <topicName> <message> Publishes an ordered message to a topic.
topics.js get-policy <topicName> Gets the IAM policy for a topic.
topics.js set-policy <topicName> Sets the IAM policy for a topic.
topics.js test-permissions <topicName> Tests the permissions for a topic.
Options:
--version Show version number [boolean]
Expand Down
19 changes: 19 additions & 0 deletions samples/system-test/topics.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const topicNameTwo = `nodejs-docs-samples-test-${uuid.v4()}`;
const subscriptionNameOne = `nodejs-docs-samples-test-${uuid.v4()}`;
const subscriptionNameTwo = `nodejs-docs-samples-test-${uuid.v4()}`;
const subscriptionNameThree = `nodejs-docs-samples-test-${uuid.v4()}`;
const subscriptionNameFour = `nodejs-docs-samples-test-${uuid.v4()}`;
const fullTopicNameOne = `projects/${projectId}/topics/${topicNameOne}`;
const expectedMessage = {data: `Hello, world!`};
const cmd = `node topics.js`;
Expand All @@ -54,6 +55,9 @@ after(async () => {
try {
await pubsub.subscription(subscriptionNameThree).delete();
} catch (err) {} // ignore error
try {
await pubsub.subscription(subscriptionNameFour).delete();
} catch (err) {} // ignore error
try {
await pubsub.topic(topicNameTwo).delete();
} catch (err) {} // ignore error
Expand Down Expand Up @@ -196,6 +200,21 @@ it(`should publish with specific batch settings`, async () => {
assert.strictEqual(publishTime - startTime > expectedWait, true);
});

it(`should publish with retry settings`, async () => {
const [subscription] = await pubsub
.topic(topicNameOne)
.subscription(subscriptionNameFour)
.get({autoCreate: true});
await tools.runAsync(
`${cmd} publish-retry ${projectId} ${topicNameOne} "${
expectedMessage.data
}"`,
cwd
);
const receivedMessage = await _pullOneMessage(subscription);
assert.strictEqual(receivedMessage.data.toString(), expectedMessage.data);
});

it(`should set the IAM policy for a topic`, async () => {
await tools.runAsync(`${cmd} set-policy ${topicNameOne}`, cwd);
const results = await pubsub.topic(topicNameOne).iam.getPolicy();
Expand Down
63 changes: 63 additions & 0 deletions samples/topics.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,60 @@ async function publishBatchedMessages(
// [END pubsub_publisher_batch_settings]
}

async function publishWithRetrySettings(projectId, topicName, data) {
// [START pubsub_publisher_retry_settings]
// Imports the Google Cloud client library
const PubSub = require('@google-cloud/pubsub');
JustinBeckwith marked this conversation as resolved.
Show resolved Hide resolved

// Creates a publisher client
const client = new PubSub.v1.PublisherClient({

This comment was marked as spam.

This comment was marked as spam.

// optional auth parameters
});

/**
* TODO(developer): Uncomment the following lines to run the sample.
*/
// const projectId = 'my-project-id'
// const topicName = 'my-topic';
// const data = JSON.stringify({ foo: 'bar' });

const formattedTopic = client.topicPath(projectId, topicName);
// Publishes the message as a string, e.g. "Hello, world!" or JSON.stringify(someObject)
const dataBuffer = Buffer.from(data);
const messagesElement = {
data: dataBuffer,
};
const messages = [messagesElement];
// Build the request
const request = {
topic: formattedTopic,
messages: messages,
};

// Retry settings control how the publisher handles retryable failures
// Default values are shown
const retrySettings = {
retry_codes: {
jkwlui marked this conversation as resolved.
Show resolved Hide resolved
idempotent: ['UNAVAILABLE', 'DEADLINE_EXCEEDED'],
non_idempotent: [],
},
backoffSettings: {
initial_retry_delay_millis: 100,
retry_delay_multiplier: 1.2,
max_retry_delay_millis: 1000,
initial_rpc_timeout_millis: 2000,
rpc_timeout_multiplier: 1.5,
max_rpc_timeout_millis: 30000,
total_timeout_millis: 45000,
},
};

const [response] = await client.publish(request, {retry: retrySettings});
console.log(`Message ${response.messageIds} published.`);

// [END pubsub_publisher_retry_settings]
}

let publishCounterValue = 1;

function getPublishCounterValue() {
Expand Down Expand Up @@ -361,6 +415,14 @@ const cli = require(`yargs`)
);
}
)
.command(
`publish-retry <projectId> <topicName> <message>`,
`Publishes a message to a topic with retry settings.`,
{},
opts => {
publishWithRetrySettings(opts.projectId, opts.topicName, opts.message);
}
)
.command(
`publish-ordered <topicName> <message>`,
`Publishes an ordered message to a topic.`,
Expand Down Expand Up @@ -400,6 +462,7 @@ const cli = require(`yargs`)
.example(`node $0 publish-attributes my-topic "Hello, world!"`)
.example(`node $0 publish-ordered my-topic "Hello, world!"`)
.example(`node $0 publish-batch my-topic "Hello, world!" -w 1000`)
.example(`node $0 publish-retry my-project my-topic "Hello, world!"`)
.example(`node $0 get-policy greetings`)
.example(`node $0 set-policy greetings`)
.example(`node $0 test-permissions greetings`)
Expand Down