in batch/snippets/src/main/java/com/example/batch/CreateLocalSsdJob.java [67:159]
public static Job createLocalSsdJob(String projectId, String region, String jobName,
String localSsdName, int ssdSize, String machineType)
throws IOException, ExecutionException, InterruptedException, TimeoutException {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests.
try (BatchServiceClient batchServiceClient = BatchServiceClient.create()) {
// Define what will be done as part of the job.
Runnable runnable =
Runnable.newBuilder()
.setScript(
Script.newBuilder()
.setText(
"echo Hello world! This is task ${BATCH_TASK_INDEX}. "
+ "This job has a total of ${BATCH_TASK_COUNT} tasks.")
// You can also run a script from a file. Just remember, that needs to be a
// script that's already on the VM that will be running the job.
// Using setText() and setPath() is mutually exclusive.
// .setPath("/tmp/test.sh")
.build())
.build();
Volume volume = Volume.newBuilder()
.setDeviceName(localSsdName)
.setMountPath("/mnt/disks/" + localSsdName)
.addMountOptions("rw")
.addMountOptions("async")
.build();
TaskSpec task = TaskSpec.newBuilder()
// Jobs can be divided into tasks. In this case, we have only one task.
.addVolumes(volume)
.addRunnables(runnable)
.setMaxRetryCount(2)
.setMaxRunDuration(Duration.newBuilder().setSeconds(3600).build())
.build();
// Tasks are grouped inside a job using TaskGroups.
// Currently, it's possible to have only one task group.
TaskGroup taskGroup = TaskGroup.newBuilder()
.setTaskCount(3)
.setParallelism(1)
.setTaskSpec(task)
.build();
// Policies are used to define on what kind of virtual machines the tasks will run on.
InstancePolicy policy = InstancePolicy.newBuilder()
.setMachineType(machineType)
.addDisks(AttachedDisk.newBuilder()
.setDeviceName(localSsdName)
// For example, local SSD uses type "local-ssd".
// Persistent disks and boot disks use "pd-balanced", "pd-extreme", "pd-ssd"
// or "pd-standard".
.setNewDisk(Disk.newBuilder().setSizeGb(ssdSize).setType("local-ssd")))
.build();
AllocationPolicy allocationPolicy =
AllocationPolicy.newBuilder()
.addInstances(
InstancePolicyOrTemplate.newBuilder()
.setPolicy(policy)
.build())
.build();
Job job =
Job.newBuilder()
.addTaskGroups(taskGroup)
.setAllocationPolicy(allocationPolicy)
.putLabels("env", "testing")
.putLabels("type", "script")
// We use Cloud Logging as it's an out of the box available option.
.setLogsPolicy(
LogsPolicy.newBuilder().setDestination(LogsPolicy.Destination.CLOUD_LOGGING))
.build();
CreateJobRequest createJobRequest =
CreateJobRequest.newBuilder()
// The job's parent is the region in which the job will run.
.setParent(String.format("projects/%s/locations/%s", projectId, region))
.setJob(job)
.setJobId(jobName)
.build();
Job result =
batchServiceClient
.createJobCallable()
.futureCall(createJobRequest)
.get(5, TimeUnit.MINUTES);
System.out.printf("Successfully created the job: %s", result.getName());
return result;
}
}