Skip to content

Commit

Permalink
docs(samples): Publish with Retry Setting Example (#355)
Browse files Browse the repository at this point in the history
  • Loading branch information
anguillanneuf authored and JustinBeckwith committed Nov 22, 2018
1 parent 9f67dc1 commit a350e40
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 10 deletions.
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 {v1} = require('@google-cloud/pubsub');

// Creates a publisher client
const client = new v1.PublisherClient({
// 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 = {
retryCodes: {
idempotent: ['UNAVAILABLE', 'DEADLINE_EXCEEDED'],
non_idempotent: [],
},
backoffSettings: {
initialRetryDelayMillis: 100,
retryDelayMultiplier: 1.2,
maxRetryDelayMillis: 1000,
initialRpcTimeoutMillis: 2000,
rpcTimeoutMultiplier: 1.5,
maxRpcTimeoutMillis: 30000,
totalTimeoutMillis: 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

0 comments on commit a350e40

Please sign in to comment.