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