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

Function main

batch/create/create_batch_notifications.js:19–100  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

17'use strict';
18
19async 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 // Project ID or project number of the Google Cloud project you want to use.
32 const PROJECT_ID = await batchClient.getProjectId();
33 // Name of the region you want to use to run the job. Regions that are
34 // available for Batch are listed on: https://cloud.google.com/batch/docs/get-started#locations
35 const REGION = 'europe-central2';
36 // The name of the job that will be created.
37 // It needs to be unique for each project and region pair.
38 const JOB_NAME = 'job-name-batch-notifications';
39 // The Pub/Sub topic ID to send the notifications to.
40 const TOPIC_ID = 'topic-id';
41
42 // Define what will be done as part of the job.
43 const task = new batch.TaskSpec();
44 const runnable = new batch.Runnable();
45 runnable.script = new batch.Runnable.Script();
46 runnable.script.commands = [
47 '-c',
48 'echo Hello world! This is task ${BATCH_TASK_INDEX}.',
49 ];
50 task.runnables = [runnable];
51 task.maxRetryCount = 2;
52 task.maxRunDuration = {seconds: 3600};
53
54 // Tasks are grouped inside a job using TaskGroups.
55 const group = new batch.TaskGroup();
56 group.taskCount = 3;
57 group.taskSpec = task;
58
59 // Create batch notification when job state changed
60 const notification1 = new batch.JobNotification();
61 notification1.pubsubTopic = `projects/${PROJECT_ID}/topics/${TOPIC_ID}`;
62 notification1.message = {
63 type: 'JOB_STATE_CHANGED',
64 };
65
66 // Create batch notification when task state changed
67 const notification2 = new batch.JobNotification();
68 notification2.pubsubTopic = `projects/${PROJECT_ID}/topics/${TOPIC_ID}`;
69 notification2.message = {
70 type: 'TASK_STATE_CHANGED',
71 newTaskState: 'FAILED',
72 };
73
74 const job = new batch.Job();
75 job.name = JOB_NAME;
76 job.taskGroups = [group];

Callers 1

Calls 2

getProjectIdMethod · 0.80

Tested by

no test coverage detected