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

Function main

cloud-tasks/snippets/createTask.js:25–82  ·  view source on GitHub ↗

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

(
  project = 'my-project-id', // Your GCP Project id
  queue = 'my-appengine-queue', // Name of your Queue
  location = 'us-central1', // The GCP region of your queue
  payload = 'Hello, World!', // The task HTTP request body
  inSeconds = 0 // Delay in task execution
)

Source from the content-addressed store, hash-verified

23 * Create a task for a given queue with an arbitrary payload.
24 */
25function main(
26 project = 'my-project-id', // Your GCP Project id
27 queue = 'my-appengine-queue', // Name of your Queue
28 location = 'us-central1', // The GCP region of your queue
29 payload = 'Hello, World!', // The task HTTP request body
30 inSeconds = 0 // Delay in task execution
31) {
32 // [START cloud_tasks_appengine_create_task]
33 // Imports the Google Cloud Tasks library.
34 const {CloudTasksClient} = require('@google-cloud/tasks');
35
36 // Instantiates a client.
37 const client = new CloudTasksClient();
38
39 async function createTask() {
40 // TODO(developer): Uncomment these lines and replace with your values.
41 // const project = 'my-project-id';
42 // const queue = 'my-appengine-queue';
43 // const location = 'us-central1';
44 // const payload = 'Hello, World!';
45
46 // Construct the fully qualified queue name.
47 const parent = client.queuePath(project, location, queue);
48
49 const task = {
50 appEngineHttpRequest: {
51 headers: {
52 'Content-Type': 'text/plain', // Set content type to ensure compatibility your application's request parsing
53 },
54 httpMethod: 'POST',
55 relativeUri: '/log_payload',
56 },
57 };
58
59 if (payload) {
60 task.appEngineHttpRequest.body = Buffer.from(payload).toString('base64');
61 }
62
63 if (inSeconds) {
64 // The time when the task is scheduled to be attempted.
65 task.scheduleTime = {
66 seconds: inSeconds + Date.now() / 1000,
67 };
68 }
69
70 console.log('Sending task:');
71 console.log(task);
72
73 // Send create task request.
74 const request = {parent: parent, task: task};
75 const [response] = await client.createTask(request);
76 const name = response.name;
77 console.log(`Created task ${name}`);
78 }
79
80 createTask();
81 // [END cloud_tasks_appengine_create_task]
82}

Callers 1

createTask.jsFile · 0.70

Calls 1

createTaskFunction · 0.85

Tested by

no test coverage detected