diff --git a/samples/README.md b/samples/README.md index 8ec1a1846..f5c695dce 100644 --- a/samples/README.md +++ b/samples/README.md @@ -89,16 +89,17 @@ __Usage:__ `node topics.js --help` topics.js Commands: - topics.js list Lists all topics in the current project. - topics.js create Creates a new topic. - topics.js delete Deletes a topic. - topics.js publish Publishes a message to a topic. - topics.js publish-attributes Publishes a message with custom attributes to a Topic - topics.js publish-batch Publishes messages to a topic using custom batching settings. - topics.js publish-ordered Publishes an ordered message to a topic. - topics.js get-policy Gets the IAM policy for a topic. - topics.js set-policy Sets the IAM policy for a topic. - topics.js test-permissions Tests the permissions for a topic. + topics.js list Lists all topics in the current project. + topics.js create Creates a new topic. + topics.js delete Deletes a topic. + topics.js publish Publishes a message to a topic. + topics.js publish-attributes Publishes a message with custom attributes to a Topic + topics.js publish-batch Publishes messages to a topic using custom batching settings. + topics.js publish-retry Publishes a message to a topic with retry settings. + topics.js publish-ordered Publishes an ordered message to a topic. + topics.js get-policy Gets the IAM policy for a topic. + topics.js set-policy Sets the IAM policy for a topic. + topics.js test-permissions Tests the permissions for a topic. Options: --version Show version number [boolean] diff --git a/samples/system-test/topics.test.js b/samples/system-test/topics.test.js index 3691bbb2a..8649659f9 100644 --- a/samples/system-test/topics.test.js +++ b/samples/system-test/topics.test.js @@ -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`; @@ -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 @@ -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(); diff --git a/samples/topics.js b/samples/topics.js index 5932bc94d..b32643d79 100755 --- a/samples/topics.js +++ b/samples/topics.js @@ -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() { @@ -361,6 +415,14 @@ const cli = require(`yargs`) ); } ) + .command( + `publish-retry `, + `Publishes a message to a topic with retry settings.`, + {}, + opts => { + publishWithRetrySettings(opts.projectId, opts.topicName, opts.message); + } + ) .command( `publish-ordered `, `Publishes an ordered message to a topic.`, @@ -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`)