|
| 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_with_bucket] |
| 16 | +from google.cloud import batch_v1 |
| 17 | + |
| 18 | + |
| 19 | +def create_script_job_with_bucket(project_id: str, region: str, job_name: str, bucket_name: str) -> batch_v1.Job: |
| 20 | + """ |
| 21 | + This method shows how to create a sample Batch Job that will run |
| 22 | + a simple command on Cloud Compute instances. |
| 23 | +
|
| 24 | + Args: |
| 25 | + project_id: project ID or project number of the Cloud project you want to use. |
| 26 | + region: name of the region you want to use to run the job. Regions that are |
| 27 | + available for Batch are listed on: https://cloud.google.com/batch/docs/get-started#locations |
| 28 | + job_name: the name of the job that will be created. |
| 29 | + It needs to be unique for each project and region pair. |
| 30 | + bucket_name: name of the bucket to be mounted for your Job. |
| 31 | +
|
| 32 | + Returns: |
| 33 | + A job object representing the job created. |
| 34 | + """ |
| 35 | + client = batch_v1.BatchServiceClient() |
| 36 | + |
| 37 | + # Define what will be done as part of the job. |
| 38 | + task = batch_v1.TaskSpec() |
| 39 | + runnable = batch_v1.Runnable() |
| 40 | + runnable.script = batch_v1.Runnable.Script() |
| 41 | + runnable.script.text = "echo Hello world from task ${BATCH_TASK_INDEX}. >> /mnt/share/output_task_${BATCH_TASK_INDEX}.txt" |
| 42 | + task.runnables = [runnable] |
| 43 | + |
| 44 | + gcs_bucket = batch_v1.GCS() |
| 45 | + gcs_bucket.remote_path = bucket_name |
| 46 | + gcs_volume = batch_v1.Volume() |
| 47 | + gcs_volume.gcs = gcs_bucket |
| 48 | + gcs_volume.mount_path = '/mnt/share' |
| 49 | + task.volumes = [gcs_volume] |
| 50 | + |
| 51 | + # We can specify what resources are requested by each task. |
| 52 | + resources = batch_v1.ComputeResource() |
| 53 | + resources.cpu_milli = 500 # in milliseconds per cpu-second. This means the task requires 50% of a single CPUs. |
| 54 | + resources.memory_mib = 16 |
| 55 | + task.compute_resource = resources |
| 56 | + |
| 57 | + task.max_retry_count = 2 |
| 58 | + task.max_run_duration = "3600s" |
| 59 | + |
| 60 | + # Tasks are grouped inside a job using TaskGroups. |
| 61 | + group = batch_v1.TaskGroup() |
| 62 | + group.task_count = 4 |
| 63 | + group.task_spec = task |
| 64 | + |
| 65 | + # Policies are used to define on what kind of virtual machines the tasks will run on. |
| 66 | + # In this case, we tell the system to use "e2-standard-4" machine type. |
| 67 | + # Read more about machine types here: https://cloud.google.com/compute/docs/machine-types |
| 68 | + allocation_policy = batch_v1.AllocationPolicy() |
| 69 | + policy = batch_v1.AllocationPolicy.InstancePolicy() |
| 70 | + policy.machine_type = "e2-standard-4" |
| 71 | + instances = batch_v1.AllocationPolicy.InstancePolicyOrTemplate() |
| 72 | + instances.policy = policy |
| 73 | + allocation_policy.instances = [instances] |
| 74 | + |
| 75 | + job = batch_v1.Job() |
| 76 | + job.task_groups = [group] |
| 77 | + job.allocation_policy = allocation_policy |
| 78 | + job.labels = {"env": "testing", "type": "script", "mount": "bucket"} |
| 79 | + # We use Cloud Logging as it's an out of the box available option |
| 80 | + job.logs_policy = batch_v1.LogsPolicy() |
| 81 | + job.logs_policy.destination = batch_v1.LogsPolicy.Destination.CLOUD_LOGGING |
| 82 | + |
| 83 | + create_request = batch_v1.CreateJobRequest() |
| 84 | + create_request.job = job |
| 85 | + create_request.job_id = job_name |
| 86 | + # The job's parent is the region in which the job will run |
| 87 | + create_request.parent = f"projects/{project_id}/locations/{region}" |
| 88 | + |
| 89 | + return client.create_job(create_request) |
| 90 | +# [END batch_create_script_job_with_bucket] |
0 commit comments