Skip to content

Commit 6fe7417

Browse files
authored
feat: batch_labels_runnable (#3792)
* feat: batch_labels_runnable * refactor: add missing doc for projectId * Add missing await * Changes after review
1 parent 9af3f5d commit 6fe7417

11 files changed

+214
-5
lines changed

batch/create/create_batch_custom_events.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ async function main() {
2828
/**
2929
* TODO(developer): Update these variables before running the sample.
3030
*/
31+
// Project ID or project number of the Google Cloud project you want to use.
3132
const projectId = await batchClient.getProjectId();
3233
// Name of the region you want to use to run the job. Regions that are
3334
// available for Batch are listed on: https://cloud.google.com/batch/docs/get-started#locations
@@ -105,7 +106,7 @@ async function main() {
105106
console.log(JSON.stringify(response));
106107
}
107108

108-
callCreateBatchCustomEvents();
109+
await callCreateBatchCustomEvents();
109110
// [END batch_custom_events]
110111
}
111112

batch/create/create_batch_labels_allocation.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ async function main() {
2828
/**
2929
* TODO(developer): Update these variables before running the sample.
3030
*/
31+
// Project ID or project number of the Google Cloud project you want to use.
3132
const projectId = await batchClient.getProjectId();
3233
// Name of the region you want to use to run the job. Regions that are
3334
// available for Batch are listed on: https://cloud.google.com/batch/docs/get-started#locations
@@ -113,7 +114,7 @@ async function main() {
113114
console.log(JSON.stringify(response));
114115
}
115116

116-
callCreateBatchLabelsAllocation();
117+
await callCreateBatchLabelsAllocation();
117118
// [END batch_labels_allocation]
118119
}
119120

batch/create/create_batch_labels_job.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ async function main() {
2828
/**
2929
* TODO(developer): Update these variables before running the sample.
3030
*/
31+
// Project ID or project number of the Google Cloud project you want to use.
3132
const projectId = await batchClient.getProjectId();
3233
// Name of the region you want to use to run the job. Regions that are
3334
// available for Batch are listed on: https://cloud.google.com/batch/docs/get-started#locations
@@ -103,7 +104,7 @@ async function main() {
103104
console.log(JSON.stringify(response));
104105
}
105106

106-
callCreateBatchLabelsJob();
107+
await callCreateBatchLabelsJob();
107108
// [END batch_labels_job]
108109
}
109110

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

batch/create/create_batch_notifications.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ async function main() {
2828
/**
2929
* TODO(developer): Update these variables before running the sample.
3030
*/
31+
// Project ID or project number of the Google Cloud project you want to use.
3132
const PROJECT_ID = await batchClient.getProjectId();
3233
// Name of the region you want to use to run the job. Regions that are
3334
// available for Batch are listed on: https://cloud.google.com/batch/docs/get-started#locations

batch/create/create_gpu_job.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ async function main() {
2828
/**
2929
* TODO(developer): Update these variables before running the sample.
3030
*/
31+
// Project ID or project number of the Google Cloud project you want to use.
3132
const projectId = await batchClient.getProjectId();
3233
// Name of the region you want to use to run the job. Regions that are
3334
// available for Batch are listed on: https://cloud.google.com/batch/docs/get-started#locations

batch/create/create_gpu_job_n1.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ async function main() {
2828
/**
2929
* TODO(developer): Update these variables before running the sample.
3030
*/
31+
// Project ID or project number of the Google Cloud project you want to use.
3132
const projectId = await batchClient.getProjectId();
3233
// Name of the region you want to use to run the job. Regions that are
3334
// available for Batch are listed on: https://cloud.google.com/batch/docs/get-started#locations

batch/create/create_local_ssd_job.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ async function main() {
2828
/**
2929
* TODO(developer): Update these variables before running the sample.
3030
*/
31+
// Project ID or project number of the Google Cloud project you want to use.
3132
const projectId = await batchClient.getProjectId();
3233
// Name of the region you want to use to run the job. Regions that are
3334
// available for Batch are listed on: https://cloud.google.com/batch/docs/get-started#locations

batch/create/create_nfs_job.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ async function main() {
123123
console.log(JSON.stringify(response));
124124
}
125125

126-
callCreateBatchNfsJob();
126+
await callCreateBatchNfsJob();
127127
// [END batch_create_nfs_job]
128128
}
129129

batch/create/create_using_secret_manager.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ async function main() {
9999
console.log(JSON.stringify(response));
100100
}
101101

102-
callCreateUsingSecretManager();
102+
await callCreateUsingSecretManager();
103103
// [END batch_create_using_secret_manager]
104104
}
105105

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 jobName = 'example-job';
31+
const region = 'us-central1';
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 for runnables', async () => {
43+
const expectedRunnableLabels = [
44+
{
45+
executable: 'container',
46+
labels: {
47+
RUNNABLE_LABEL_NAME1: 'RUNNABLE_LABEL_VALUE1',
48+
},
49+
},
50+
{
51+
executable: 'script',
52+
labels: {
53+
RUNNABLE_LABEL_NAME2: '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)