|
| 1 | +/* |
| 2 | + * Copyright 2021 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +'use strict'; |
| 18 | + |
| 19 | +async function main() { |
| 20 | + // [START batch_notifications] |
| 21 | + // Imports the Batch library |
| 22 | + const batchLib = require('@google-cloud/batch'); |
| 23 | + const batch = batchLib.protos.google.cloud.batch.v1; |
| 24 | + |
| 25 | + // Instantiates a client |
| 26 | + const batchClient = new batchLib.v1.BatchServiceClient(); |
| 27 | + |
| 28 | + /** |
| 29 | + * TODO(developer): Update these variables before running the sample. |
| 30 | + */ |
| 31 | + const PROJECT_ID = await batchClient.getProjectId(); |
| 32 | + // Name of the region you want to use to run the job. Regions that are |
| 33 | + // available for Batch are listed on: https://cloud.google.com/batch/docs/get-started#locations |
| 34 | + const REGION = 'europe-central2'; |
| 35 | + // The name of the job that will be created. |
| 36 | + // It needs to be unique for each project and region pair. |
| 37 | + const JOB_NAME = 'job-name-batch-notifications'; |
| 38 | + // The Pub/Sub topic ID to send the notifications to. |
| 39 | + const TOPIC_ID = 'topic-id'; |
| 40 | + |
| 41 | + // Define what will be done as part of the job. |
| 42 | + const task = new batch.TaskSpec(); |
| 43 | + const runnable = new batch.Runnable(); |
| 44 | + runnable.script = new batch.Runnable.Script(); |
| 45 | + runnable.script.commands = [ |
| 46 | + '-c', |
| 47 | + 'echo Hello world! This is task ${BATCH_TASK_INDEX}.', |
| 48 | + ]; |
| 49 | + task.runnables = [runnable]; |
| 50 | + task.maxRetryCount = 2; |
| 51 | + task.maxRunDuration = {seconds: 3600}; |
| 52 | + |
| 53 | + // Tasks are grouped inside a job using TaskGroups. |
| 54 | + const group = new batch.TaskGroup(); |
| 55 | + group.taskCount = 3; |
| 56 | + group.taskSpec = task; |
| 57 | + |
| 58 | + const job = new batch.Job(); |
| 59 | + job.name = JOB_NAME; |
| 60 | + job.taskGroups = [group]; |
| 61 | + job.labels = {env: 'testing', type: 'script'}; |
| 62 | + // We use Cloud Logging as it's an option available out of the box |
| 63 | + job.logsPolicy = new batch.LogsPolicy(); |
| 64 | + job.logsPolicy.destination = batch.LogsPolicy.Destination.CLOUD_LOGGING; |
| 65 | + |
| 66 | + // Create batch notification when job state changed |
| 67 | + const notification1 = new batch.JobNotification(); |
| 68 | + notification1.pubsubTopic = `projects/${PROJECT_ID}/topics/${TOPIC_ID}`; |
| 69 | + notification1.message = { |
| 70 | + type: 'JOB_STATE_CHANGED', |
| 71 | + }; |
| 72 | + |
| 73 | + // Create batch notification when task state changed |
| 74 | + const notification2 = new batch.JobNotification(); |
| 75 | + notification2.pubsubTopic = `projects/${PROJECT_ID}/topics/${TOPIC_ID}`; |
| 76 | + notification2.message = { |
| 77 | + type: 'TASK_STATE_CHANGED', |
| 78 | + newTaskState: 'FAILED', |
| 79 | + }; |
| 80 | + |
| 81 | + job.name = JOB_NAME; |
| 82 | + job.taskGroups = [group]; |
| 83 | + job.notifications = [notification1, notification2]; |
| 84 | + job.labels = {env: 'testing', type: 'script'}; |
| 85 | + // We use Cloud Logging as it's an option available out of the box |
| 86 | + job.logsPolicy = new batch.LogsPolicy(); |
| 87 | + job.logsPolicy.destination = batch.LogsPolicy.Destination.CLOUD_LOGGING; |
| 88 | + // The job's parent is the project and region in which the job will run |
| 89 | + const parent = `projects/${PROJECT_ID}/locations/${REGION}`; |
| 90 | + |
| 91 | + async function callCreateBatchNotifications() { |
| 92 | + // Construct request |
| 93 | + const request = { |
| 94 | + parent, |
| 95 | + jobId: JOB_NAME, |
| 96 | + job, |
| 97 | + }; |
| 98 | + |
| 99 | + // Run request |
| 100 | + const [response] = await batchClient.createJob(request); |
| 101 | + console.log(JSON.stringify(response)); |
| 102 | + } |
| 103 | + |
| 104 | + callCreateBatchNotifications(); |
| 105 | + // [END batch_notifications] |
| 106 | +} |
| 107 | + |
| 108 | +process.on('unhandledRejection', err => { |
| 109 | + console.error(err.message); |
| 110 | + process.exitCode = 1; |
| 111 | +}); |
| 112 | + |
| 113 | +main(); |
0 commit comments