Skip to content

Commit 7c4dc84

Browse files
gryczjsubfuzion
andauthored
feat: Batch labels job (#3789)
* feat: batch_labels_job * refactor: refactor code in labels allocation sample --------- Co-authored-by: Tony Pujals <[email protected]>
1 parent 009ae80 commit 7c4dc84

File tree

4 files changed

+171
-4
lines changed

4 files changed

+171
-4
lines changed

batch/create/create_batch_labels_allocation.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,7 @@ async function main() {
117117
// [END batch_labels_allocation]
118118
}
119119

120-
process.on('unhandledRejection', err => {
120+
main().catch(err => {
121121
console.error(err.message);
122122
process.exitCode = 1;
123123
});
124-
125-
main();
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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_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+
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-job';
38+
// Name of the label1 to be applied for your Job.
39+
const labelName1 = 'job_label_name_1';
40+
// Value for the label1 to be applied for your Job.
41+
const labelValue1 = 'job_label_value1';
42+
// Name of the label2 to be applied for your Job.
43+
const labelName2 = 'job_label_name_2';
44+
// Value for the label2 to be applied for your Job.
45+
const labelValue2 = 'job_label_value2';
46+
47+
// Define what will be done as part of the job.
48+
const runnable = new batch.Runnable({
49+
container: new batch.Runnable.Container({
50+
imageUri: 'gcr.io/google-containers/busybox',
51+
entrypoint: '/bin/sh',
52+
commands: ['-c', 'echo Hello world! This is task ${BATCH_TASK_INDEX}.'],
53+
}),
54+
});
55+
56+
// Specify what resources are requested by each task.
57+
const computeResource = new batch.ComputeResource({
58+
// In milliseconds per cpu-second. This means the task requires 50% of a single CPUs.
59+
cpuMilli: 500,
60+
// In MiB.
61+
memoryMib: 16,
62+
});
63+
64+
const task = new batch.TaskSpec({
65+
runnables: [runnable],
66+
computeResource,
67+
maxRetryCount: 2,
68+
maxRunDuration: {seconds: 3600},
69+
});
70+
71+
// Tasks are grouped inside a job using TaskGroups.
72+
const group = new batch.TaskGroup({
73+
taskCount: 3,
74+
taskSpec: task,
75+
});
76+
77+
const job = new batch.Job({
78+
name: jobName,
79+
taskGroups: [group],
80+
// We use Cloud Logging as it's an option available out of the box
81+
logsPolicy: new batch.LogsPolicy({
82+
destination: batch.LogsPolicy.Destination.CLOUD_LOGGING,
83+
}),
84+
});
85+
86+
// Labels and their value to be applied to the job and its resources.
87+
job.labels[labelName1] = labelValue1;
88+
job.labels[labelName2] = labelValue2;
89+
90+
// The job's parent is the project and region in which the job will run
91+
const parent = `projects/${projectId}/locations/${region}`;
92+
93+
async function callCreateBatchLabelsJob() {
94+
// Construct request
95+
const request = {
96+
parent,
97+
jobId: jobName,
98+
job,
99+
};
100+
101+
// Run request
102+
const [response] = await batchClient.createJob(request);
103+
console.log(JSON.stringify(response));
104+
}
105+
106+
callCreateBatchLabelsJob();
107+
// [END batch_labels_job]
108+
}
109+
110+
main().catch(err => {
111+
console.error(err.message);
112+
process.exitCode = 1;
113+
});

batch/test/create_batch_labels_allocation.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ describe('Create batch labels allocation', async () => {
4141

4242
it('should create a new job with allocation policy labels', async () => {
4343
const expectedAllocationLabels = {
44-
'batch-job-id': 'batch-labels-allocation-job',
44+
'batch-job-id': jobName,
4545
vm_label_name_1: 'vmLabelValue1',
4646
vm_label_name_2: 'vmLabelValue2',
4747
};
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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 labels for job', async () => {
30+
const jobName = 'batch-labels-job';
31+
const region = 'europe-central2';
32+
let projectId;
33+
34+
before(async () => {
35+
projectId = await batchClient.getProjectId();
36+
});
37+
38+
after(async () => {
39+
await deleteJob(batchClient, projectId, region, jobName);
40+
});
41+
42+
it('should create a new job with labels', async () => {
43+
const expectedJobLabels = {
44+
job_label_name_1: 'job_label_value1',
45+
job_label_name_2: 'job_label_value2',
46+
};
47+
48+
const response = JSON.parse(
49+
execSync('node ./create/create_batch_labels_job.js', {
50+
cwd,
51+
})
52+
);
53+
54+
assert.deepEqual(response.labels, expectedJobLabels);
55+
});
56+
});

0 commit comments

Comments
 (0)