|
| 1 | +// Copyright 2022 Google LLC |
| 2 | +// |
| 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 | +// https://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 | +'use strict'; |
| 16 | + |
| 17 | +/** |
| 18 | + * Creates a new Batch job that runs the specified script on multiple VM instances at once. |
| 19 | + * |
| 20 | + * @param {string} projectId - ID or number of the Google Cloud project you want to use. |
| 21 | + * @param {string} region - The Google Cloud region to use, e.g. 'us-central1' |
| 22 | + * @param {string} jobName - ID used to uniquely identify the Job within this project and region. |
| 23 | + * This field should contain at most 63 characters. |
| 24 | + * Only alphanumeric characters or '-' are accepted. |
| 25 | + * The '-' character cannot be the first or the last one. |
| 26 | + */ |
| 27 | +function main(projectId, region, jobName) { |
| 28 | + // [START batch_create_script_job] |
| 29 | + /** |
| 30 | + * TODO(developer): Uncomment and replace these variables before running the sample. |
| 31 | + */ |
| 32 | + // const projectId = 'YOUR_PROJECT_ID'; |
| 33 | + /** |
| 34 | + * The region you want to the job to run in. The regions that support Batch are listed here: |
| 35 | + * https://cloud.google.com/batch/docs/get-started#locations |
| 36 | + */ |
| 37 | + // const region = 'us-central-1'; |
| 38 | + /** |
| 39 | + * The name of the job that will be created. |
| 40 | + * It needs to be unique for each project and region pair. |
| 41 | + */ |
| 42 | + // const jobName = 'YOUR_JOB_NAME'; |
| 43 | + |
| 44 | + // Imports the Batch library |
| 45 | + const batchLib = require('@google-cloud/batch'); |
| 46 | + const batch = batchLib.protos.google.cloud.batch.v1; |
| 47 | + |
| 48 | + // Instantiates a client |
| 49 | + const batchClient = new batchLib.v1.BatchServiceClient(); |
| 50 | + |
| 51 | + // Define what will be done as part of the job. |
| 52 | + const task = new batch.TaskSpec(); |
| 53 | + const runnable = new batch.Runnable(); |
| 54 | + runnable.script = new batch.Runnable.Script(); |
| 55 | + runnable.script.text = |
| 56 | + 'echo Hello world! This is task ${BATCH_TASK_INDEX}. This job has a total of ${BATCH_TASK_COUNT} tasks.'; |
| 57 | + // You can also run a script from a file. Just remember, that needs to be a script that's |
| 58 | + // already on the VM that will be running the job. Using runnable.script.text and runnable.script.path is mutually |
| 59 | + // exclusive. |
| 60 | + // runnable.script.path = '/tmp/test.sh' |
| 61 | + task.runnables = [runnable]; |
| 62 | + |
| 63 | + // We can specify what resources are requested by each task. |
| 64 | + const resources = new batch.ComputeResource(); |
| 65 | + resources.cpuMilli = 2000; // in milliseconds per cpu-second. This means the task requires 2 whole CPUs. |
| 66 | + resources.memoryMib = 16; |
| 67 | + task.computeResource = resources; |
| 68 | + |
| 69 | + task.maxRetryCount = 2; |
| 70 | + task.maxRunDuration = {seconds: 3600}; |
| 71 | + |
| 72 | + // Tasks are grouped inside a job using TaskGroups. |
| 73 | + const group = new batch.TaskGroup(); |
| 74 | + group.taskCount = 4; |
| 75 | + group.taskSpec = task; |
| 76 | + |
| 77 | + // Policies are used to define on what kind of virtual machines the tasks will run on. |
| 78 | + // In this case, we tell the system to use "e2-standard-4" machine type. |
| 79 | + // Read more about machine types here: https://cloud.google.com/compute/docs/machine-types |
| 80 | + const allocationPolicy = new batch.AllocationPolicy(); |
| 81 | + const policy = new batch.AllocationPolicy.InstancePolicy(); |
| 82 | + policy.machineType = 'e2-standard-4'; |
| 83 | + const instances = new batch.AllocationPolicy.InstancePolicyOrTemplate(); |
| 84 | + instances.policy = policy; |
| 85 | + allocationPolicy.instances = [instances]; |
| 86 | + |
| 87 | + const job = new batch.Job(); |
| 88 | + job.name = jobName; |
| 89 | + job.taskGroups = [group]; |
| 90 | + job.allocationPolicy = allocationPolicy; |
| 91 | + job.labels = {env: 'testing', type: 'script'}; |
| 92 | + // We use Cloud Logging as it's an out option available out of the box |
| 93 | + job.logsPolicy = new batch.LogsPolicy(); |
| 94 | + job.logsPolicy.destination = batch.LogsPolicy.Destination.CLOUD_LOGGING; |
| 95 | + |
| 96 | + // The job's parent is the project and region in which the job will run |
| 97 | + const parent = `projects/${projectId}/locations/${region}`; |
| 98 | + |
| 99 | + async function callCreateJob() { |
| 100 | + // Construct request |
| 101 | + const request = { |
| 102 | + parent, |
| 103 | + jobId: jobName, |
| 104 | + job, |
| 105 | + }; |
| 106 | + |
| 107 | + // Run request |
| 108 | + const response = await batchClient.createJob(request); |
| 109 | + console.log(response); |
| 110 | + } |
| 111 | + |
| 112 | + callCreateJob(); |
| 113 | + // [END batch_create_script_job] |
| 114 | +} |
| 115 | + |
| 116 | +process.on('unhandledRejection', err => { |
| 117 | + console.error(err.message); |
| 118 | + process.exitCode = 1; |
| 119 | +}); |
| 120 | +main(...process.argv.slice(2)); |
0 commit comments