Skip to content

Commit b42888f

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

File tree

2 files changed

+167
-0
lines changed

2 files changed

+167
-0
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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_custom_events]
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-custom-events-job';
38+
// Name of the runnable, which must be unique
39+
// within the job. For example: script 1, barrier 1, and script 2.
40+
const displayName1 = 'script 1';
41+
const displayName2 = 'barrier 1';
42+
const displayName3 = 'script 2';
43+
44+
// Create runnables with custom scripts
45+
const runnable1 = new batch.Runnable({
46+
displayName: displayName1,
47+
script: new batch.Runnable.Script({
48+
commands: [
49+
'-c',
50+
'echo Hello world from script 1 for task ${BATCH_TASK_INDEX}.',
51+
],
52+
}),
53+
});
54+
55+
const runnable2 = new batch.Runnable({
56+
displayName: displayName2,
57+
barrier: new batch.Runnable.Barrier(),
58+
});
59+
60+
const runnable3 = new batch.Runnable({
61+
displayName: displayName3,
62+
script: new batch.Runnable.Script({
63+
// Replace DESCRIPTION with a description
64+
// for the custom status event—for example, halfway done.
65+
commands: [
66+
'sleep 30; echo \'{"batch/custom/event": "DESCRIPTION"}\'; sleep 30',
67+
],
68+
}),
69+
});
70+
71+
const task = new batch.TaskSpec({
72+
runnables: [runnable1, runnable2, runnable3],
73+
maxRetryCount: 2,
74+
maxRunDuration: {seconds: 3600},
75+
});
76+
77+
// Tasks are grouped inside a job using TaskGroups.
78+
const group = new batch.TaskGroup({
79+
taskCount: 3,
80+
taskSpec: task,
81+
});
82+
83+
const job = new batch.Job({
84+
name: jobName,
85+
taskGroups: [group],
86+
labels: {env: 'testing', type: 'script'},
87+
// We use Cloud Logging as it's an option available out of the box
88+
logsPolicy: new batch.LogsPolicy({
89+
destination: batch.LogsPolicy.Destination.CLOUD_LOGGING,
90+
}),
91+
});
92+
// The job's parent is the project and region in which the job will run
93+
const parent = `projects/${projectId}/locations/${region}`;
94+
95+
async function callCreateBatchCustomEvents() {
96+
// Construct request
97+
const request = {
98+
parent,
99+
jobId: jobName,
100+
job,
101+
};
102+
103+
// Run request
104+
const [response] = await batchClient.createJob(request);
105+
console.log(JSON.stringify(response));
106+
}
107+
108+
callCreateBatchCustomEvents();
109+
// [END batch_custom_events]
110+
}
111+
112+
main().catch(err => {
113+
console.error(err);
114+
process.exitCode = 1;
115+
});
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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 custom events job', async () => {
30+
const jobName = 'batch-custom-events-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 custom events', () => {
43+
const response = execSync('node ./create/create_batch_custom_events.js', {
44+
cwd,
45+
});
46+
const runnables = JSON.parse(response).taskGroups[0].taskSpec.runnables;
47+
48+
assert.equal(runnables[0].displayName, 'script 1');
49+
assert.equal(runnables[1].displayName, 'barrier 1');
50+
assert.equal(runnables[2].displayName, 'script 2');
51+
});
52+
});

0 commit comments

Comments
 (0)