forked from GoogleCloudPlatform/nodejs-docs-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateTask.js
More file actions
90 lines (79 loc) · 3.08 KB
/
createTask.js
File metadata and controls
90 lines (79 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
'use strict';
// [START cloud_tasks_app_create_task]
const MAX_SCHEDULE_LIMIT = 30 * 60 * 60 * 24; // Represents 30 days in seconds.
const createHttpTaskWithToken = async function (
project = 'my-project-id', // Your GCP Project id
queue = 'my-queue', // Name of your Queue
location = 'us-central1', // The GCP region of your queue
url = 'https://example.com/taskhandler', // The full url path that the request will be sent to
email = '<member>@<project-id>.iam.gserviceaccount.com', // Cloud IAM service account
payload = 'Hello, World!', // The task HTTP request body
date = new Date() // Intended date to schedule task
) {
// Imports the Google Cloud Tasks library.
const {v2beta3} = require('@google-cloud/tasks');
// Instantiates a client.
const client = new v2beta3.CloudTasksClient();
// Construct the fully qualified queue name.
const parent = client.queuePath(project, location, queue);
// Convert message to buffer.
const convertedPayload = JSON.stringify(payload);
const body = Buffer.from(convertedPayload).toString('base64');
const task = {
httpRequest: {
httpMethod: 'POST',
url,
oidcToken: {
serviceAccountEmail: email,
},
headers: {
'Content-Type': 'application/json',
},
body,
},
};
const convertedDate = new Date(date);
const currentDate = new Date();
// Schedule time can not be in the past.
if (convertedDate < currentDate) {
console.error('Scheduled date in the past.');
} else if (convertedDate > currentDate) {
const date_diff_in_seconds = (convertedDate - currentDate) / 1000;
// Restrict schedule time to the 30 day maximum.
if (date_diff_in_seconds > MAX_SCHEDULE_LIMIT) {
console.error('Schedule time is over 30 day maximum.');
}
// Construct future date in Unix time.
const date_in_seconds =
Math.min(date_diff_in_seconds, MAX_SCHEDULE_LIMIT) + Date.now() / 1000;
// Add schedule time to request in Unix time using Timestamp structure.
// https://googleapis.dev/nodejs/tasks/latest/google.protobuf.html#.Timestamp
task.scheduleTime = {
seconds: date_in_seconds,
};
}
try {
// Send create task request.
const [response] = await client.createTask({parent, task});
console.log(`Created task ${response.name}`);
return response.name;
} catch (error) {
// Construct error for Stackdriver Error Reporting
console.error(Error(error.message));
}
};
module.exports = createHttpTaskWithToken;
// [END cloud_tasks_app_create_task]