Skip to content

Commit ff7e79b

Browse files
gryczjsubfuzion
andauthored
feat: batch_create_nfs_job (#3784)
* feat: batch_create_nfs_job * chore: update copyright year --------- Co-authored-by: Tony Pujals <[email protected]> Co-authored-by: Tony Pujals <[email protected]>
1 parent b42888f commit ff7e79b

File tree

2 files changed

+195
-0
lines changed

2 files changed

+195
-0
lines changed

batch/create/create_nfs_job.js

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
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+
});

batch/test/create_nfs_job.test.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
const path = require('path');
20+
const assert = require('assert');
21+
const {describe, it} = require('mocha');
22+
const cp = require('child_process');
23+
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
24+
const cwd = path.join(__dirname, '..');
25+
const {BatchServiceClient} = require('@google-cloud/batch').v1;
26+
const {deleteJob} = require('./batchClient_operations');
27+
const batchClient = new BatchServiceClient();
28+
29+
describe('Create batch NFS job', async () => {
30+
const jobName = 'batch-nfs-job';
31+
const region = 'europe-central2';
32+
33+
let projectId;
34+
35+
before(async () => {
36+
projectId = await batchClient.getProjectId();
37+
});
38+
39+
after(async () => {
40+
await deleteJob(batchClient, projectId, region, jobName);
41+
});
42+
43+
it('should create a new job with NFS', async () => {
44+
const expectedVolumes = [
45+
{
46+
mountOptions: [],
47+
mountPath: '/mnt/disks',
48+
nfs: {server: '0.0.0.0', remotePath: '/your_nfs_path'},
49+
source: 'nfs',
50+
},
51+
];
52+
53+
const response = execSync('node ./create/create_nfs_job.js', {
54+
cwd,
55+
});
56+
57+
assert.deepEqual(
58+
JSON.parse(response).taskGroups[0].taskSpec.volumes,
59+
expectedVolumes
60+
);
61+
});
62+
});

0 commit comments

Comments
 (0)