MCPcopy Create free account
hub / github.com/GoogleCloudPlatform/nodejs-docs-samples / main

Function main

cloud-tasks/snippets/quickstart.js:20–69  ·  view source on GitHub ↗

* Create a task for a given queue with an arbitrary payload.

(project, location, queue, payload, inSeconds)

Source from the content-addressed store, hash-verified

18 * Create a task for a given queue with an arbitrary payload.
19 */
20function main(project, location, queue, payload, inSeconds) {
21 // [START cloud_tasks_quickstart]
22 // Imports the Google Cloud Tasks library.
23 const {CloudTasksClient} = require('@google-cloud/tasks');
24
25 // Instantiates a client.
26 const client = new CloudTasksClient();
27
28 async function quickstart() {
29 // TODO(developer): Uncomment these lines and replace with your values.
30 // const project = 'my-project-id';
31 // const queue = 'my-appengine-queue';
32 // const location = 'us-central1';
33 // const payload = 'hello';
34
35 // Construct the fully qualified queue name.
36 const parent = client.queuePath(project, location, queue);
37
38 const task = {
39 appEngineHttpRequest: {
40 httpMethod: 'POST',
41 relativeUri: '/log_payload',
42 },
43 };
44
45 if (payload) {
46 task.appEngineHttpRequest.body = Buffer.from(payload).toString('base64');
47 }
48
49 if (inSeconds) {
50 task.scheduleTime = {
51 seconds: inSeconds + Date.now() / 1000,
52 };
53 }
54
55 const request = {
56 parent: parent,
57 task: task,
58 };
59
60 console.log('Sending task:');
61 console.log(task);
62 // Send create task request.
63 const [response] = await client.createTask(request);
64 const name = response.name;
65 console.log(`Created task ${name}`);
66 }
67 quickstart();
68 // [END cloud_tasks_quickstart]
69}
70
71process.on('unhandledRejection', err => {
72 console.error(err.message);

Callers 1

quickstart.jsFile · 0.70

Calls 1

quickstartFunction · 0.70

Tested by

no test coverage detected