Skip to content

Commit 756e851

Browse files
docs(samples): added basic samples and tests (#43)
* docs(samples): added basic samples and tests * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * minor refactoring * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * updated comments Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent 10cfa60 commit 756e851

File tree

6 files changed

+567
-0
lines changed

6 files changed

+567
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
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+
// http://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+
// [START batch_create_container_job]
16+
17+
import com.google.cloud.batch.v1.AllocationPolicy;
18+
import com.google.cloud.batch.v1.AllocationPolicy.InstancePolicy;
19+
import com.google.cloud.batch.v1.AllocationPolicy.InstancePolicyOrTemplate;
20+
import com.google.cloud.batch.v1.BatchServiceClient;
21+
import com.google.cloud.batch.v1.ComputeResource;
22+
import com.google.cloud.batch.v1.CreateJobRequest;
23+
import com.google.cloud.batch.v1.Job;
24+
import com.google.cloud.batch.v1.LogsPolicy;
25+
import com.google.cloud.batch.v1.LogsPolicy.Destination;
26+
import com.google.cloud.batch.v1.Runnable;
27+
import com.google.cloud.batch.v1.Runnable.Container;
28+
import com.google.cloud.batch.v1.TaskGroup;
29+
import com.google.cloud.batch.v1.TaskSpec;
30+
import com.google.protobuf.Duration;
31+
import java.io.IOException;
32+
import java.util.concurrent.ExecutionException;
33+
import java.util.concurrent.TimeUnit;
34+
import java.util.concurrent.TimeoutException;
35+
36+
public class CreateWithContainerNoMounting {
37+
38+
public static void main(String[] args)
39+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
40+
// TODO(developer): Replace these variables before running the sample.
41+
// Project ID or project number of the Cloud project you want to use.
42+
String projectId = "YOUR_PROJECT_ID";
43+
44+
// Name of the region you want to use to run the job. Regions that are
45+
// available for Batch are listed on: https://cloud.google.com/batch/docs/get-started#locations
46+
String region = "europe-central2";
47+
48+
// The name of the job that will be created.
49+
// It needs to be unique for each project and region pair.
50+
String jobName = "JOB_NAME";
51+
52+
createContainerJob(projectId, region, jobName);
53+
}
54+
55+
// This method shows how to create a sample Batch Job that will run a simple command inside a
56+
// container on Cloud Compute instances.
57+
public static void createContainerJob(String projectId, String region, String jobName)
58+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
59+
// Initialize client that will be used to send requests. This client only needs to be created
60+
// once, and can be reused for multiple requests. After completing all of your requests, call
61+
// the `batchServiceClient.close()` method on the client to safely
62+
// clean up any remaining background resources.
63+
try (BatchServiceClient batchServiceClient = BatchServiceClient.create()) {
64+
65+
// Define what will be done as part of the job.
66+
Runnable runnable =
67+
Runnable.newBuilder()
68+
.setContainer(
69+
Container.newBuilder()
70+
.setImageUri("gcr.io/google-containers/busybox")
71+
.setEntrypoint("/bin/sh")
72+
.addCommands("-c")
73+
.addCommands(
74+
"echo Hello world! This is task ${BATCH_TASK_INDEX}. "
75+
+ "This job has a total of ${BATCH_TASK_COUNT} tasks.")
76+
.build())
77+
.build();
78+
79+
// We can specify what resources are requested by each task.
80+
ComputeResource computeResource =
81+
ComputeResource.newBuilder()
82+
// In milliseconds per cpu-second. This means the task requires 2 whole CPUs.
83+
.setCpuMilli(2000)
84+
// In MiB.
85+
.setMemoryMib(16)
86+
.build();
87+
88+
TaskSpec task =
89+
TaskSpec.newBuilder()
90+
// Jobs can be divided into tasks. In this case, we have only one task.
91+
.addRunnables(runnable)
92+
.setComputeResource(computeResource)
93+
.setMaxRetryCount(2)
94+
.setMaxRunDuration(Duration.newBuilder().setSeconds(3600).build())
95+
.build();
96+
97+
// Tasks are grouped inside a job using TaskGroups.
98+
TaskGroup taskGroup = TaskGroup.newBuilder().setTaskCount(4).setTaskSpec(task).build();
99+
100+
// Policies are used to define on what kind of virtual machines the tasks will run on.
101+
// In this case, we tell the system to use "e2-standard-4" machine type.
102+
// Read more about machine types here: https://cloud.google.com/compute/docs/machine-types
103+
InstancePolicy instancePolicy =
104+
InstancePolicy.newBuilder().setMachineType("e2-standard-4").build();
105+
106+
AllocationPolicy allocationPolicy =
107+
AllocationPolicy.newBuilder()
108+
.addInstances(InstancePolicyOrTemplate.newBuilder().setPolicy(instancePolicy).build())
109+
.build();
110+
111+
Job job =
112+
Job.newBuilder()
113+
.addTaskGroups(taskGroup)
114+
.setAllocationPolicy(allocationPolicy)
115+
.putLabels("env", "testing")
116+
.putLabels("type", "container")
117+
// We use Cloud Logging as it's an out of the box available option.
118+
.setLogsPolicy(
119+
LogsPolicy.newBuilder().setDestination(Destination.CLOUD_LOGGING).build())
120+
.build();
121+
122+
CreateJobRequest createJobRequest =
123+
CreateJobRequest.newBuilder()
124+
// The job's parent is the region in which the job will run.
125+
.setParent(String.format("projects/%s/locations/%s", projectId, region))
126+
.setJob(job)
127+
.setJobId(jobName)
128+
.build();
129+
130+
Job result =
131+
batchServiceClient
132+
.createJobCallable()
133+
.futureCall(createJobRequest)
134+
.get(3, TimeUnit.MINUTES);
135+
136+
System.out.printf("Successfully created the job: %s", result.getName());
137+
}
138+
}
139+
}
140+
// [END batch_create_container_job]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
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+
// http://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+
// [START batch_create_script_job]
16+
17+
import com.google.cloud.batch.v1.AllocationPolicy;
18+
import com.google.cloud.batch.v1.AllocationPolicy.InstancePolicy;
19+
import com.google.cloud.batch.v1.AllocationPolicy.InstancePolicyOrTemplate;
20+
import com.google.cloud.batch.v1.BatchServiceClient;
21+
import com.google.cloud.batch.v1.ComputeResource;
22+
import com.google.cloud.batch.v1.CreateJobRequest;
23+
import com.google.cloud.batch.v1.Job;
24+
import com.google.cloud.batch.v1.LogsPolicy;
25+
import com.google.cloud.batch.v1.LogsPolicy.Destination;
26+
import com.google.cloud.batch.v1.Runnable;
27+
import com.google.cloud.batch.v1.Runnable.Script;
28+
import com.google.cloud.batch.v1.TaskGroup;
29+
import com.google.cloud.batch.v1.TaskSpec;
30+
import com.google.protobuf.Duration;
31+
import java.io.IOException;
32+
import java.util.concurrent.ExecutionException;
33+
import java.util.concurrent.TimeUnit;
34+
import java.util.concurrent.TimeoutException;
35+
36+
public class CreateWithScriptNoMounting {
37+
38+
public static void main(String[] args)
39+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
40+
// TODO(developer): Replace these variables before running the sample.
41+
// Project ID or project number of the Cloud project you want to use.
42+
String projectId = "YOUR_PROJECT_ID";
43+
44+
// Name of the region you want to use to run the job. Regions that are
45+
// available for Batch are listed on: https://cloud.google.com/batch/docs/get-started#locations
46+
String region = "europe-central2";
47+
48+
// The name of the job that will be created.
49+
// It needs to be unique for each project and region pair.
50+
String jobName = "JOB_NAME";
51+
52+
createScriptJob(projectId, region, jobName);
53+
}
54+
55+
// This method shows how to create a sample Batch Job that will run
56+
// a simple command on Cloud Compute instances.
57+
public static void createScriptJob(String projectId, String region, String jobName)
58+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
59+
// Initialize client that will be used to send requests. This client only needs to be created
60+
// once, and can be reused for multiple requests. After completing all of your requests, call
61+
// the `batchServiceClient.close()` method on the client to safely
62+
// clean up any remaining background resources.
63+
try (BatchServiceClient batchServiceClient = BatchServiceClient.create()) {
64+
65+
// Define what will be done as part of the job.
66+
Runnable runnable =
67+
Runnable.newBuilder()
68+
.setScript(
69+
Script.newBuilder()
70+
.setText(
71+
"echo Hello world! This is task ${BATCH_TASK_INDEX}. "
72+
+ "This job has a total of ${BATCH_TASK_COUNT} tasks.")
73+
// You can also run a script from a file. Just remember, that needs to be a
74+
// script that's already on the VM that will be running the job.
75+
// Using setText() and setPath() is mutually exclusive.
76+
// .setPath("/tmp/test.sh")
77+
.build())
78+
.build();
79+
80+
// We can specify what resources are requested by each task.
81+
ComputeResource computeResource =
82+
ComputeResource.newBuilder()
83+
// In milliseconds per cpu-second. This means the task requires 2 whole CPUs.
84+
.setCpuMilli(2000)
85+
// In MiB.
86+
.setMemoryMib(16)
87+
.build();
88+
89+
TaskSpec task =
90+
TaskSpec.newBuilder()
91+
// Jobs can be divided into tasks. In this case, we have only one task.
92+
.addRunnables(runnable)
93+
.setComputeResource(computeResource)
94+
.setMaxRetryCount(2)
95+
.setMaxRunDuration(Duration.newBuilder().setSeconds(3600).build())
96+
.build();
97+
98+
// Tasks are grouped inside a job using TaskGroups.
99+
TaskGroup taskGroup = TaskGroup.newBuilder().setTaskCount(4).setTaskSpec(task).build();
100+
101+
// Policies are used to define on what kind of virtual machines the tasks will run on.
102+
// In this case, we tell the system to use "e2-standard-4" machine type.
103+
// Read more about machine types here: https://cloud.google.com/compute/docs/machine-types
104+
InstancePolicy instancePolicy =
105+
InstancePolicy.newBuilder().setMachineType("e2-standard-4").build();
106+
107+
AllocationPolicy allocationPolicy =
108+
AllocationPolicy.newBuilder()
109+
.addInstances(InstancePolicyOrTemplate.newBuilder().setPolicy(instancePolicy).build())
110+
.build();
111+
112+
Job job =
113+
Job.newBuilder()
114+
.addTaskGroups(taskGroup)
115+
.setAllocationPolicy(allocationPolicy)
116+
.putLabels("env", "testing")
117+
.putLabels("type", "script")
118+
// We use Cloud Logging as it's an out of the box available option.
119+
.setLogsPolicy(
120+
LogsPolicy.newBuilder().setDestination(Destination.CLOUD_LOGGING).build())
121+
.build();
122+
123+
CreateJobRequest createJobRequest =
124+
CreateJobRequest.newBuilder()
125+
// The job's parent is the region in which the job will run.
126+
.setParent(String.format("projects/%s/locations/%s", projectId, region))
127+
.setJob(job)
128+
.setJobId(jobName)
129+
.build();
130+
131+
Job result =
132+
batchServiceClient
133+
.createJobCallable()
134+
.futureCall(createJobRequest)
135+
.get(3, TimeUnit.MINUTES);
136+
137+
System.out.printf("Successfully created the job: %s", result.getName());
138+
}
139+
}
140+
}
141+
// [END batch_create_script_job]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
// http://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+
// [START batch_delete_job]
16+
17+
import com.google.cloud.batch.v1.BatchServiceClient;
18+
import java.io.IOException;
19+
import java.util.concurrent.ExecutionException;
20+
import java.util.concurrent.TimeUnit;
21+
import java.util.concurrent.TimeoutException;
22+
23+
public class DeleteJob {
24+
25+
public static void main(String[] args)
26+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
27+
// TODO(developer): Replace these variables before running the sample.
28+
// Project ID or project number of the Cloud project you want to use.
29+
String projectId = "YOUR_PROJECT_ID";
30+
31+
// Name of the region hosts the job.
32+
String region = "europe-central2";
33+
34+
// The name of the job that you want to delete.
35+
String jobName = "JOB_NAME";
36+
37+
deleteJob(projectId, region, jobName);
38+
}
39+
40+
// Triggers the deletion of a Job.
41+
public static void deleteJob(String projectId, String region, String jobName)
42+
throws IOException, ExecutionException, InterruptedException, TimeoutException {
43+
// Initialize client that will be used to send requests. This client only needs to be created
44+
// once, and can be reused for multiple requests. After completing all of your requests, call
45+
// the `batchServiceClient.close()` method on the client to safely
46+
// clean up any remaining background resources.
47+
try (BatchServiceClient batchServiceClient = BatchServiceClient.create()) {
48+
49+
// Construct the parent path of the job.
50+
String name = String.format("projects/%s/locations/%s/jobs/%s", projectId, region, jobName);
51+
52+
batchServiceClient.deleteJobAsync(name).get(3, TimeUnit.MINUTES);
53+
System.out.printf("Delete the job: %s", jobName);
54+
}
55+
}
56+
}
57+
// [END batch_delete_job]
+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
// http://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+
// [START batch_get_job]
16+
17+
import com.google.cloud.batch.v1.BatchServiceClient;
18+
import com.google.cloud.batch.v1.Job;
19+
import com.google.cloud.batch.v1.JobName;
20+
import java.io.IOException;
21+
22+
public class GetJob {
23+
24+
public static void main(String[] args) throws IOException {
25+
// TODO(developer): Replace these variables before running the sample.
26+
// Project ID or project number of the Cloud project you want to use.
27+
String projectId = "YOUR_PROJECT_ID";
28+
29+
// Name of the region hosts the job.
30+
String region = "europe-central2";
31+
32+
// The name of the job you want to retrieve information about.
33+
String jobName = "JOB_NAME";
34+
35+
getJob(projectId, region, jobName);
36+
}
37+
38+
// Retrieve information about a Batch Job.
39+
public static void getJob(String projectId, String region, String jobName) throws IOException {
40+
// Initialize client that will be used to send requests. This client only needs to be created
41+
// once, and can be reused for multiple requests. After completing all of your requests, call
42+
// the `batchServiceClient.close()` method on the client to safely
43+
// clean up any remaining background resources.
44+
try (BatchServiceClient batchServiceClient = BatchServiceClient.create()) {
45+
46+
Job job =
47+
batchServiceClient.getJob(
48+
JobName.newBuilder()
49+
.setProject(projectId)
50+
.setLocation(region)
51+
.setJob(jobName)
52+
.build());
53+
54+
System.out.printf("Retrieved the job: %s ", job.getName());
55+
}
56+
}
57+
}
58+
// [END batch_get_job]

0 commit comments

Comments
 (0)