|
| 1 | +/* |
| 2 | + * Copyright 2024 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_labels_allocation] |
| 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 projectId = 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 jobName = 'batch-labels-allocation-job'; |
| 38 | + // Name of the label1 to be applied for your Job. |
| 39 | + const labelName1 = 'vm_label_name_1'; |
| 40 | + // Value for the label1 to be applied for your Job. |
| 41 | + const labelValue1 = 'vmLabelValue1'; |
| 42 | + // Name of the label2 to be applied for your Job. |
| 43 | + const labelName2 = 'vm_label_name_2'; |
| 44 | + // Value for the label2 to be applied for your Job. |
| 45 | + const labelValue2 = 'vmLabelValue2'; |
| 46 | + |
| 47 | + // Define what will be done as part of the job. |
| 48 | + const runnable = new batch.Runnable({ |
| 49 | + script: new batch.Runnable.Script({ |
| 50 | + commands: ['-c', 'echo Hello world! This is task ${BATCH_TASK_INDEX}.'], |
| 51 | + }), |
| 52 | + }); |
| 53 | + |
| 54 | + // Specify what resources are requested by each task. |
| 55 | + const computeResource = new batch.ComputeResource({ |
| 56 | + // In milliseconds per cpu-second. This means the task requires 50% of a single CPUs. |
| 57 | + cpuMilli: 500, |
| 58 | + // In MiB. |
| 59 | + memoryMib: 16, |
| 60 | + }); |
| 61 | + |
| 62 | + const task = new batch.TaskSpec({ |
| 63 | + runnables: [runnable], |
| 64 | + computeResource, |
| 65 | + maxRetryCount: 2, |
| 66 | + maxRunDuration: {seconds: 3600}, |
| 67 | + }); |
| 68 | + |
| 69 | + // Tasks are grouped inside a job using TaskGroups. |
| 70 | + const group = new batch.TaskGroup({ |
| 71 | + taskCount: 3, |
| 72 | + taskSpec: task, |
| 73 | + }); |
| 74 | + |
| 75 | + // Policies are used to define on what kind of virtual machines the tasks will run on. |
| 76 | + // In this case, we tell the system to use "e2-standard-4" machine type. |
| 77 | + // Read more about machine types here: https://cloud.google.com/compute/docs/machine-types |
| 78 | + const instancePolicy = new batch.AllocationPolicy.InstancePolicy({ |
| 79 | + machineType: 'e2-standard-4', |
| 80 | + }); |
| 81 | + |
| 82 | + const allocationPolicy = new batch.AllocationPolicy({ |
| 83 | + instances: [{policy: instancePolicy}], |
| 84 | + }); |
| 85 | + // Labels and their value to be applied to the job and its resources. |
| 86 | + allocationPolicy.labels[labelName1] = labelValue1; |
| 87 | + allocationPolicy.labels[labelName2] = labelValue2; |
| 88 | + |
| 89 | + const job = new batch.Job({ |
| 90 | + name: jobName, |
| 91 | + taskGroups: [group], |
| 92 | + labels: {env: 'testing', type: 'script'}, |
| 93 | + allocationPolicy, |
| 94 | + // We use Cloud Logging as it's an option available out of the box |
| 95 | + logsPolicy: new batch.LogsPolicy({ |
| 96 | + destination: batch.LogsPolicy.Destination.CLOUD_LOGGING, |
| 97 | + }), |
| 98 | + }); |
| 99 | + |
| 100 | + // The job's parent is the project and region in which the job will run |
| 101 | + const parent = `projects/${projectId}/locations/${region}`; |
| 102 | + |
| 103 | + async function callCreateBatchLabelsAllocation() { |
| 104 | + // Construct request |
| 105 | + const request = { |
| 106 | + parent, |
| 107 | + jobId: jobName, |
| 108 | + job, |
| 109 | + }; |
| 110 | + |
| 111 | + // Run request |
| 112 | + const [response] = await batchClient.createJob(request); |
| 113 | + console.log(JSON.stringify(response)); |
| 114 | + } |
| 115 | + |
| 116 | + callCreateBatchLabelsAllocation(); |
| 117 | + // [END batch_labels_allocation] |
| 118 | +} |
| 119 | + |
| 120 | +process.on('unhandledRejection', err => { |
| 121 | + console.error(err.message); |
| 122 | + process.exitCode = 1; |
| 123 | +}); |
| 124 | + |
| 125 | +main(); |
0 commit comments