|
| 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_create_nfs_job] |
| 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 projectId = 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 jobName = 'batch-nfs-job'; |
| 39 | + // The path of the NFS directory that you want this job to access. |
| 40 | + const nfsPath = '/your_nfs_path'; |
| 41 | + // The IP address of the Network File System. |
| 42 | + const nfsIpAddress = '0.0.0.0'; |
| 43 | + // The mount path that the job's tasks use to access the NFS. |
| 44 | + const mountPath = '/mnt/disks'; |
| 45 | + |
| 46 | + // Define what will be done as part of the job. |
| 47 | + const runnable = new batch.Runnable({ |
| 48 | + script: new batch.Runnable.Script({ |
| 49 | + commands: [ |
| 50 | + '-c', |
| 51 | + 'echo Hello world from task ${BATCH_TASK_INDEX}. >> ' + |
| 52 | + '/mnt/share/output_task_${BATCH_TASK_INDEX}.txt', |
| 53 | + ], |
| 54 | + }), |
| 55 | + }); |
| 56 | + |
| 57 | + // Define a volume that uses NFS. |
| 58 | + const volume = new batch.Volume({ |
| 59 | + nfs: new batch.NFS({ |
| 60 | + server: nfsIpAddress, |
| 61 | + remotePath: nfsPath, |
| 62 | + }), |
| 63 | + mountPath, |
| 64 | + }); |
| 65 | + |
| 66 | + // Specify what resources are requested by each task. |
| 67 | + const computeResource = new batch.ComputeResource({ |
| 68 | + // In milliseconds per cpu-second. This means the task requires 50% of a single CPUs. |
| 69 | + cpuMilli: 500, |
| 70 | + // In MiB. |
| 71 | + memoryMib: 16, |
| 72 | + }); |
| 73 | + |
| 74 | + const task = new batch.TaskSpec({ |
| 75 | + runnables: [runnable], |
| 76 | + volumes: [volume], |
| 77 | + computeResource, |
| 78 | + maxRetryCount: 2, |
| 79 | + maxRunDuration: {seconds: 3600}, |
| 80 | + }); |
| 81 | + |
| 82 | + // Tasks are grouped inside a job using TaskGroups. |
| 83 | + const group = new batch.TaskGroup({ |
| 84 | + taskCount: 3, |
| 85 | + taskSpec: task, |
| 86 | + }); |
| 87 | + |
| 88 | + // Policies are used to define on what kind of virtual machines the tasks will run on. |
| 89 | + // In this case, we tell the system to use "e2-standard-4" machine type. |
| 90 | + // Read more about machine types here: https://cloud.google.com/compute/docs/machine-types |
| 91 | + const instancePolicy = new batch.AllocationPolicy.InstancePolicy({ |
| 92 | + machineType: 'e2-standard-4', |
| 93 | + }); |
| 94 | + |
| 95 | + const allocationPolicy = new batch.AllocationPolicy.InstancePolicyOrTemplate({ |
| 96 | + instances: [{policy: instancePolicy}], |
| 97 | + }); |
| 98 | + |
| 99 | + const job = new batch.Job({ |
| 100 | + name: jobName, |
| 101 | + taskGroups: [group], |
| 102 | + allocationPolicy, |
| 103 | + labels: {env: 'testing', type: 'script'}, |
| 104 | + // We use Cloud Logging as it's an option available out of the box |
| 105 | + logsPolicy: new batch.LogsPolicy({ |
| 106 | + destination: batch.LogsPolicy.Destination.CLOUD_LOGGING, |
| 107 | + }), |
| 108 | + }); |
| 109 | + |
| 110 | + // The job's parent is the project and region in which the job will run |
| 111 | + const parent = `projects/${projectId}/locations/${region}`; |
| 112 | + |
| 113 | + async function callCreateBatchNfsJob() { |
| 114 | + // Construct request |
| 115 | + const request = { |
| 116 | + parent, |
| 117 | + jobId: jobName, |
| 118 | + job, |
| 119 | + }; |
| 120 | + |
| 121 | + // Run request |
| 122 | + const [response] = await batchClient.createJob(request); |
| 123 | + console.log(JSON.stringify(response)); |
| 124 | + } |
| 125 | + |
| 126 | + callCreateBatchNfsJob(); |
| 127 | + // [END batch_create_nfs_job] |
| 128 | +} |
| 129 | + |
| 130 | +main().catch(err => { |
| 131 | + console.error(err); |
| 132 | + process.exitCode = 1; |
| 133 | +}); |
0 commit comments