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

Function main

batch/create/create_with_mounted_bucket.js:28–126  ·  view source on GitHub ↗

* Creates a new Batch job that runs the specified script on multiple VM instances at once. * * @param {string} projectId - ID or number of the Google Cloud project you want to use. * @param {string} region - The Google Cloud region to use, e.g. 'us-central1' * @param {string} jobName - ID used t

(projectId, region, jobName, bucketName)

Source from the content-addressed store, hash-verified

26 * @param {string} bucketName - The name of the bucket to be mounted.
27 */
28async function main(projectId, region, jobName, bucketName) {
29 // [START batch_create_script_job_with_bucket]
30 /**
31 * TODO(developer): Uncomment and replace these variables before running the sample.
32 */
33 // const projectId = 'YOUR_PROJECT_ID';
34 /**
35 * The region you want to the job to run in. The regions that support Batch are listed here:
36 * https://cloud.google.com/batch/docs/get-started#locations
37 */
38 // const region = 'us-central-1';
39 /**
40 * The name of the job that will be created.
41 * It needs to be unique for each project and region pair.
42 */
43 // const jobName = 'YOUR_JOB_NAME';
44 /**
45 * The name of the bucket to be mounted.
46 */
47 // const bucketName = 'YOUR_BUCKET_NAME';
48
49 // Imports the Batch library
50 const batchLib = require('@google-cloud/batch');
51 const batch = batchLib.protos.google.cloud.batch.v1;
52
53 // Instantiates a client
54 const batchClient = new batchLib.v1.BatchServiceClient();
55
56 // Define what will be done as part of the job.
57 const task = new batch.TaskSpec();
58 const runnable = new batch.Runnable();
59 runnable.script = new batch.Runnable.Script();
60 runnable.script.text =
61 'echo Hello world from task ${BATCH_TASK_INDEX}. >> /mnt/share/output_task_${BATCH_TASK_INDEX}.txt';
62 // You can also run a script from a file. Just remember, that needs to be a script that's
63 // already on the VM that will be running the job. Using runnable.script.text and runnable.script.path is mutually
64 // exclusive.
65 // runnable.script.path = '/tmp/test.sh'
66 task.runnables = [runnable];
67
68 const gcsBucket = new batch.GCS();
69 gcsBucket.remotePath = bucketName;
70 const gcsVolume = new batch.Volume();
71 gcsVolume.gcs = gcsBucket;
72 gcsVolume.mountPath = '/mnt/share';
73 task.volumes = [gcsVolume];
74
75 // We can specify what resources are requested by each task.
76 const resources = new batch.ComputeResource();
77 resources.cpuMilli = 2000; // in milliseconds per cpu-second. This means the task requires 2 whole CPUs.
78 resources.memoryMib = 16;
79 task.computeResource = resources;
80
81 task.maxRetryCount = 2;
82 task.maxRunDuration = {seconds: 3600};
83
84 // Tasks are grouped inside a job using TaskGroups.
85 const group = new batch.TaskGroup();

Callers 1

Calls 1

callCreateJobFunction · 0.70

Tested by

no test coverage detected