Skip to content

Commit 63c55c7

Browse files
Shnatselsofisl
andauthored
feat(batch): Initial samples for Batch (#2775)
* Import code from googleapis/google-cloud-node#3372 * remove test for an autogenerated snippet * Drop the autogenerated README * test: add batch tests Co-authored-by: Sofia Leon <[email protected]>
1 parent 0589599 commit 63c55c7

8 files changed

+608
-0
lines changed

.github/workflows/batch.yaml

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
name: batch
2+
on:
3+
push:
4+
branches:
5+
- main
6+
paths:
7+
- 'batch/**'
8+
pull_request:
9+
paths:
10+
- 'batch/**'
11+
pull_request_target:
12+
types: [labeled]
13+
schedule:
14+
- cron: '0 0 * * 0'
15+
jobs:
16+
test:
17+
if: ${{ github.event.action != 'labeled' || github.event.label.name == 'actions:force-run' }}
18+
runs-on: ubuntu-latest
19+
timeout-minutes: 60
20+
permissions:
21+
contents: 'write'
22+
pull-requests: 'write'
23+
id-token: 'write'
24+
steps:
25+
- uses: actions/checkout@v3
26+
with:
27+
ref: ${{github.event.pull_request.head.ref}}
28+
repository: ${{github.event.pull_request.head.repo.full_name}}
29+
- uses: google-github-actions/[email protected]
30+
with:
31+
workload_identity_provider: 'projects/1046198160504/locations/global/workloadIdentityPools/github-actions-pool/providers/github-actions-provider'
32+
service_account: '[email protected]'
33+
create_credentials_file: 'true'
34+
access_token_lifetime: 600s
35+
- uses: actions/setup-node@v3
36+
with:
37+
node-version: 14
38+
- run: npm install
39+
working-directory: batch
40+
- run: npm test
41+
working-directory: batch
42+
env:
43+
MOCHA_REPORTER_SUITENAME: batch
44+
MOCHA_REPORTER_OUTPUT: batch_sponge_log.xml
45+
MOCHA_REPORTER: xunit
46+
- if: ${{ github.event.action == 'labeled' && github.event.label.name == 'actions:force-run' }}
47+
uses: actions/github-script@v6
48+
with:
49+
github-token: ${{ secrets.GITHUB_TOKEN }}
50+
script: |
51+
try {
52+
await github.rest.issues.removeLabel({
53+
name: 'actions:force-run',
54+
owner: 'GoogleCloudPlatform',
55+
repo: 'nodejs-docs-samples',
56+
issue_number: context.payload.pull_request.number
57+
});
58+
} catch (e) {
59+
if (!e.message.includes('Label does not exist')) {
60+
throw e;
61+
}
62+
}
63+
- if: ${{ github.event_name == 'schedule' && always() }}
64+
run: |
65+
curl https://github.com/googleapis/repo-automation-bots/releases/download/flakybot-1.1.0/flakybot -o flakybot -s -L
66+
chmod +x ./flakybot
67+
./flakybot --repo GoogleCloudPlatform/nodejs-docs-samples --commit_hash ${{github.sha}} --build_url https://github.com/${{github.repository}}/actions/runs/${{github.run_id}}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
// Copyright 2022 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
/**
18+
* Creates a new Batch job that runs the specified container on multiple VM instances at once.
19+
*
20+
* @param {string} projectId - ID or number of the Google Cloud project you want to use.
21+
* @param {string} region - The Google Cloud region to use, e.g. 'us-central1'
22+
* @param {string} jobName - ID used to uniquely identify the Job within this project and region.
23+
* This field should contain at most 63 characters.
24+
* Only alphanumeric characters or '-' are accepted.
25+
* The '-' character cannot be the first or the last one.
26+
*/
27+
function main(projectId, region, jobName) {
28+
// [START batch_create_container_job]
29+
/**
30+
* TODO(developer): Uncomment and replace these variables before running the sample.
31+
*/
32+
// const projectId = 'YOUR_PROJECT_ID';
33+
/**
34+
* The region you want to the job to run in. The regions that support Batch are listed here:
35+
* https://cloud.google.com/batch/docs/get-started#locations
36+
*/
37+
// const region = 'us-central-1';
38+
/**
39+
* The name of the job that will be created.
40+
* It needs to be unique for each project and region pair.
41+
*/
42+
// const jobName = 'YOUR_JOB_NAME';
43+
44+
// Imports the Batch library
45+
const batchLib = require('@google-cloud/batch');
46+
const batch = batchLib.protos.google.cloud.batch.v1;
47+
48+
// Instantiates a client
49+
const batchClient = new batchLib.v1.BatchServiceClient();
50+
51+
// Define what will be done as part of the job.
52+
const task = new batch.TaskSpec();
53+
const runnable = new batch.Runnable();
54+
runnable.container = new batch.Runnable.Container();
55+
runnable.container.imageUri = 'gcr.io/google-containers/busybox';
56+
runnable.container.entrypoint = '/bin/sh';
57+
runnable.container.commands = [
58+
'echo Hello world! This is task ${BATCH_TASK_INDEX}. This job has a total of ${BATCH_TASK_COUNT} tasks.',
59+
];
60+
task.runnables = [runnable];
61+
62+
// We can specify what resources are requested by each task.
63+
const resources = new batch.ComputeResource();
64+
resources.cpuMilli = 2000; // in milliseconds per cpu-second. This means the task requires 2 whole CPUs.
65+
resources.memoryMib = 16;
66+
task.computeResource = resources;
67+
68+
task.maxRetryCount = 2;
69+
task.maxRunDuration = {seconds: 3600};
70+
71+
// Tasks are grouped inside a job using TaskGroups.
72+
const group = new batch.TaskGroup();
73+
group.taskCount = 4;
74+
group.taskSpec = task;
75+
76+
// Policies are used to define on what kind of virtual machines the tasks will run on.
77+
// In this case, we tell the system to use "e2-standard-4" machine type.
78+
// Read more about machine types here: https://cloud.google.com/compute/docs/machine-types
79+
const allocationPolicy = new batch.AllocationPolicy();
80+
const policy = new batch.AllocationPolicy.InstancePolicy();
81+
policy.machineType = 'e2-standard-4';
82+
const instances = new batch.AllocationPolicy.InstancePolicyOrTemplate();
83+
instances.policy = policy;
84+
allocationPolicy.instances = [instances];
85+
86+
const job = new batch.Job();
87+
job.name = jobName;
88+
job.taskGroups = [group];
89+
job.allocationPolicy = allocationPolicy;
90+
job.labels = {env: 'testing', type: 'container'};
91+
// We use Cloud Logging as it's an out option available out of the box
92+
job.logsPolicy = new batch.LogsPolicy();
93+
job.logsPolicy.destination = batch.LogsPolicy.Destination.CLOUD_LOGGING;
94+
95+
// The job's parent is the project and region in which the job will run
96+
const parent = `projects/${projectId}/locations/${region}`;
97+
98+
async function callCreateJob() {
99+
// Construct request
100+
const request = {
101+
parent,
102+
jobId: jobName,
103+
job,
104+
};
105+
106+
// Run request
107+
const response = await batchClient.createJob(request);
108+
console.log(response);
109+
}
110+
111+
callCreateJob();
112+
// [END batch_create_container_job]
113+
}
114+
115+
process.on('unhandledRejection', err => {
116+
console.error(err.message);
117+
process.exitCode = 1;
118+
});
119+
main(...process.argv.slice(2));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
// Copyright 2022 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
/**
18+
* Creates a new Batch job that runs the specified script on multiple VM instances at once.
19+
*
20+
* @param {string} projectId - ID or number of the Google Cloud project you want to use.
21+
* @param {string} region - The Google Cloud region to use, e.g. 'us-central1'
22+
* @param {string} jobName - ID used to uniquely identify the Job within this project and region.
23+
* This field should contain at most 63 characters.
24+
* Only alphanumeric characters or '-' are accepted.
25+
* The '-' character cannot be the first or the last one.
26+
*/
27+
function main(projectId, region, jobName) {
28+
// [START batch_create_script_job]
29+
/**
30+
* TODO(developer): Uncomment and replace these variables before running the sample.
31+
*/
32+
// const projectId = 'YOUR_PROJECT_ID';
33+
/**
34+
* The region you want to the job to run in. The regions that support Batch are listed here:
35+
* https://cloud.google.com/batch/docs/get-started#locations
36+
*/
37+
// const region = 'us-central-1';
38+
/**
39+
* The name of the job that will be created.
40+
* It needs to be unique for each project and region pair.
41+
*/
42+
// const jobName = 'YOUR_JOB_NAME';
43+
44+
// Imports the Batch library
45+
const batchLib = require('@google-cloud/batch');
46+
const batch = batchLib.protos.google.cloud.batch.v1;
47+
48+
// Instantiates a client
49+
const batchClient = new batchLib.v1.BatchServiceClient();
50+
51+
// Define what will be done as part of the job.
52+
const task = new batch.TaskSpec();
53+
const runnable = new batch.Runnable();
54+
runnable.script = new batch.Runnable.Script();
55+
runnable.script.text =
56+
'echo Hello world! This is task ${BATCH_TASK_INDEX}. This job has a total of ${BATCH_TASK_COUNT} tasks.';
57+
// You can also run a script from a file. Just remember, that needs to be a script that's
58+
// already on the VM that will be running the job. Using runnable.script.text and runnable.script.path is mutually
59+
// exclusive.
60+
// runnable.script.path = '/tmp/test.sh'
61+
task.runnables = [runnable];
62+
63+
// We can specify what resources are requested by each task.
64+
const resources = new batch.ComputeResource();
65+
resources.cpuMilli = 2000; // in milliseconds per cpu-second. This means the task requires 2 whole CPUs.
66+
resources.memoryMib = 16;
67+
task.computeResource = resources;
68+
69+
task.maxRetryCount = 2;
70+
task.maxRunDuration = {seconds: 3600};
71+
72+
// Tasks are grouped inside a job using TaskGroups.
73+
const group = new batch.TaskGroup();
74+
group.taskCount = 4;
75+
group.taskSpec = task;
76+
77+
// Policies are used to define on what kind of virtual machines the tasks will run on.
78+
// In this case, we tell the system to use "e2-standard-4" machine type.
79+
// Read more about machine types here: https://cloud.google.com/compute/docs/machine-types
80+
const allocationPolicy = new batch.AllocationPolicy();
81+
const policy = new batch.AllocationPolicy.InstancePolicy();
82+
policy.machineType = 'e2-standard-4';
83+
const instances = new batch.AllocationPolicy.InstancePolicyOrTemplate();
84+
instances.policy = policy;
85+
allocationPolicy.instances = [instances];
86+
87+
const job = new batch.Job();
88+
job.name = jobName;
89+
job.taskGroups = [group];
90+
job.allocationPolicy = allocationPolicy;
91+
job.labels = {env: 'testing', type: 'script'};
92+
// We use Cloud Logging as it's an out option available out of the box
93+
job.logsPolicy = new batch.LogsPolicy();
94+
job.logsPolicy.destination = batch.LogsPolicy.Destination.CLOUD_LOGGING;
95+
96+
// The job's parent is the project and region in which the job will run
97+
const parent = `projects/${projectId}/locations/${region}`;
98+
99+
async function callCreateJob() {
100+
// Construct request
101+
const request = {
102+
parent,
103+
jobId: jobName,
104+
job,
105+
};
106+
107+
// Run request
108+
const response = await batchClient.createJob(request);
109+
console.log(response);
110+
}
111+
112+
callCreateJob();
113+
// [END batch_create_script_job]
114+
}
115+
116+
process.on('unhandledRejection', err => {
117+
console.error(err.message);
118+
process.exitCode = 1;
119+
});
120+
main(...process.argv.slice(2));

batch/delete/delete_job.js

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright 2022 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
/**
18+
* Deletes the specified job.
19+
*
20+
* @param {string} projectId - ID or number of the Google Cloud project you want to use.
21+
* @param {string} region - The Google Cloud region to use, e.g. 'us-central1'
22+
* @param {string} jobName - ID used to uniquely identify the Job within this project and region.
23+
* This field should contain at most 63 characters.
24+
* Only alphanumeric characters or '-' are accepted.
25+
* The '-' character cannot be the first or the last one.
26+
*/
27+
function main(projectId, region, jobName) {
28+
// [START batch_delete_job]
29+
/**
30+
* TODO(developer): Uncomment and replace these variables before running the sample.
31+
*/
32+
// const projectId = 'YOUR_PROJECT_ID';
33+
/**
34+
* The region that hosts the job.
35+
*/
36+
// const region = 'us-central-1';
37+
/**
38+
* The name of the job you want to delete.
39+
*/
40+
// const jobName = 'YOUR_JOB_NAME';
41+
42+
// Imports the Batch library
43+
const batchLib = require('@google-cloud/batch');
44+
45+
// Instantiates a client
46+
const batchClient = new batchLib.v1.BatchServiceClient();
47+
48+
async function callDeleteJob() {
49+
// Construct request
50+
const request = {
51+
name: `projects/${projectId}/locations/${region}/jobs/${jobName}`,
52+
};
53+
54+
// Run request
55+
const [operation] = await batchClient.deleteJob(request);
56+
const [response] = await operation.promise();
57+
console.log(response);
58+
}
59+
60+
callDeleteJob();
61+
// [END batch_delete_job]
62+
}
63+
64+
process.on('unhandledRejection', err => {
65+
console.error(err.message);
66+
process.exitCode = 1;
67+
});
68+
main(...process.argv.slice(2));

0 commit comments

Comments
 (0)