|
| 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(5, TimeUnit.MINUTES); |
| 136 | + |
| 137 | + System.out.printf("Successfully created the job: %s", result.getName()); |
| 138 | + } |
| 139 | + } |
| 140 | +} |
| 141 | +// [END batch_create_script_job] |
0 commit comments