Skip to content

Commit f7f7b15

Browse files
author
Joanna Grycz
committed
feat: batch_labels_runnable
1 parent 7c4dc84 commit f7f7b15

File tree

2 files changed

+201
-0
lines changed

2 files changed

+201
-0
lines changed
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
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_runnable]
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-runnable';
38+
// Name of the label1 to be applied for your Job.
39+
const labelName1 = 'runnable_label_name_1';
40+
// Value for the label1 to be applied for your Job.
41+
const labelValue1 = 'runnable_label_value1';
42+
// Name of the label2 to be applied for your Job.
43+
const labelName2 = 'runnable_label_name_2';
44+
// Value for the label2 to be applied for your Job.
45+
const labelValue2 = 'runnable_label_value2';
46+
47+
const container = new batch.Runnable.Container({
48+
imageUri: 'gcr.io/google-containers/busybox',
49+
entrypoint: '/bin/sh',
50+
commands: ['-c', 'echo Hello world! This is task ${BATCH_TASK_INDEX}.'],
51+
});
52+
53+
const script = new batch.Runnable.Script({
54+
commands: ['-c', 'echo Hello world! This is task ${BATCH_TASK_INDEX}.'],
55+
});
56+
57+
const runnable1 = new batch.Runnable({
58+
container,
59+
// Label and its value to be applied to the container
60+
// that processes data from a specific region.
61+
labels: {
62+
[labelName1]: labelValue1,
63+
},
64+
});
65+
66+
const runnable2 = new batch.Runnable({
67+
script,
68+
// Label and its value to be applied to the script
69+
// that performs some analysis on the processed data.
70+
labels: {
71+
[labelName2]: labelValue2,
72+
},
73+
});
74+
75+
// Specify what resources are requested by each task.
76+
const computeResource = new batch.ComputeResource({
77+
// In milliseconds per cpu-second. This means the task requires 50% of a single CPUs.
78+
cpuMilli: 500,
79+
// In MiB.
80+
memoryMib: 16,
81+
});
82+
83+
const task = new batch.TaskSpec({
84+
runnables: [runnable1, runnable2],
85+
computeResource,
86+
maxRetryCount: 2,
87+
maxRunDuration: {seconds: 3600},
88+
});
89+
90+
// Tasks are grouped inside a job using TaskGroups.
91+
const group = new batch.TaskGroup({
92+
taskCount: 3,
93+
taskSpec: task,
94+
});
95+
96+
const job = new batch.Job({
97+
name: jobName,
98+
taskGroups: [group],
99+
// We use Cloud Logging as it's an option available out of the box
100+
logsPolicy: new batch.LogsPolicy({
101+
destination: batch.LogsPolicy.Destination.CLOUD_LOGGING,
102+
}),
103+
});
104+
105+
// The job's parent is the project and region in which the job will run
106+
const parent = `projects/${projectId}/locations/${region}`;
107+
108+
async function callCreateBatchLabelsRunnable() {
109+
// Construct request
110+
const request = {
111+
parent,
112+
jobId: jobName,
113+
job,
114+
};
115+
116+
// Run request
117+
const [response] = await batchClient.createJob(request);
118+
console.log(JSON.stringify(response));
119+
}
120+
121+
callCreateBatchLabelsRunnable();
122+
// [END batch_labels_runnable]
123+
}
124+
125+
main().catch(err => {
126+
console.error(err.message);
127+
process.exitCode = 1;
128+
});
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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 runnable', async () => {
30+
const runnableName = 'batch-labels-runnable';
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, runnableName);
40+
});
41+
42+
it('should create a new job with labels for runnables', async () => {
43+
const expectedRunnableLabels = [
44+
{
45+
executable: 'container',
46+
labels: {
47+
runnable_label_name_1: 'runnable_label_value1',
48+
},
49+
},
50+
{
51+
executable: 'script',
52+
labels: {
53+
runnable_label_name_2: 'runnable_label_value2',
54+
},
55+
},
56+
];
57+
58+
const response = JSON.parse(
59+
execSync('node ./create/create_batch_labels_runnable.js', {
60+
cwd,
61+
})
62+
);
63+
const runnables = response.taskGroups[0].taskSpec.runnables;
64+
65+
runnables.forEach((runnable, index) => {
66+
assert.equal(
67+
runnable.executable,
68+
expectedRunnableLabels[index].executable
69+
);
70+
assert.deepEqual(runnable.labels, expectedRunnableLabels[index].labels);
71+
});
72+
});
73+
});

0 commit comments

Comments
 (0)