Skip to content

Commit a6faced

Browse files
feat: Batch created job with custom labels. 3 new samples (#12483)
* Created Samples for creating jobs with custom labels * Changed type annotations * Changed from two to one runnable
1 parent e80da05 commit a6faced

File tree

4 files changed

+375
-0
lines changed

4 files changed

+375
-0
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
# Copyright 2024 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+
import google.auth
16+
17+
# [START batch_labels_allocation]
18+
from google.cloud import batch_v1
19+
20+
21+
def create_job_with_custom_allocation_policy_labels(
22+
project_id: str, region: str, job_name: str, labels: dict
23+
) -> batch_v1.Job:
24+
"""
25+
This method shows the creation of a Batch job with custom labels which describe the allocation policy.
26+
Args:
27+
project_id (str): project ID or project number of the Cloud project you want to use.
28+
region (str): name of the region you want to use to run the job. Regions that are
29+
available for Batch are listed on: https://cloud.google.com/batch/docs/locations
30+
job_name (str): the name of the job that will be created.
31+
labels (dict): a dictionary of key-value pairs that will be used as labels
32+
E.g., {"label_key1": "label_value2", "label_key2": "label_value2"}
33+
Returns:
34+
batch_v1.Job: The created Batch job object containing configuration details.
35+
"""
36+
client = batch_v1.BatchServiceClient()
37+
38+
runnable = batch_v1.Runnable()
39+
runnable.container = batch_v1.Runnable.Container()
40+
runnable.container.image_uri = "gcr.io/google-containers/busybox"
41+
runnable.container.entrypoint = "/bin/sh"
42+
runnable.container.commands = [
43+
"-c",
44+
"echo Hello world!",
45+
]
46+
47+
# Create a task specification and assign the runnable and volume to it
48+
task = batch_v1.TaskSpec()
49+
task.runnables = [runnable]
50+
51+
# Specify what resources are requested by each task.
52+
resources = batch_v1.ComputeResource()
53+
resources.cpu_milli = 2000 # in milliseconds per cpu-second. This means the task requires 2 whole CPUs.
54+
resources.memory_mib = 16 # in MiB
55+
task.compute_resource = resources
56+
57+
task.max_retry_count = 2
58+
task.max_run_duration = "3600s"
59+
60+
# Create a task group and assign the task specification to it
61+
group = batch_v1.TaskGroup()
62+
group.task_count = 3
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+
policy = batch_v1.AllocationPolicy.InstancePolicy()
69+
policy.machine_type = "e2-standard-4"
70+
instances = batch_v1.AllocationPolicy.InstancePolicyOrTemplate()
71+
instances.policy = policy
72+
allocation_policy = batch_v1.AllocationPolicy()
73+
allocation_policy.instances = [instances]
74+
75+
# Assign the provided labels to the allocation policy
76+
allocation_policy.labels = labels
77+
78+
# Create the job and assign the task group and allocation policy to it
79+
job = batch_v1.Job()
80+
job.task_groups = [group]
81+
job.allocation_policy = allocation_policy
82+
83+
# We use Cloud Logging as it's an out of the box available option
84+
job.logs_policy = batch_v1.LogsPolicy()
85+
job.logs_policy.destination = batch_v1.LogsPolicy.Destination.CLOUD_LOGGING
86+
87+
# Create the job request and set the job and job ID
88+
create_request = batch_v1.CreateJobRequest()
89+
create_request.job = job
90+
create_request.job_id = job_name
91+
# The job's parent is the region in which the job will run
92+
create_request.parent = f"projects/{project_id}/locations/{region}"
93+
94+
return client.create_job(create_request)
95+
96+
97+
# [END batch_labels_allocation]
98+
99+
100+
if __name__ == "__main__":
101+
PROJECT_ID = google.auth.default()[1]
102+
REGION = "us-central1"
103+
job_name = "your-job-name"
104+
labels = {"label_key1": "label_value2", "label_key2": "label_value2"}
105+
create_job_with_custom_allocation_policy_labels(
106+
PROJECT_ID, REGION, job_name, labels
107+
)
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
# Copyright 2024 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+
import google.auth
16+
17+
# [START batch_labels_job]
18+
from google.cloud import batch_v1
19+
20+
21+
def create_job_with_custom_job_labels(
22+
project_id: str,
23+
region: str,
24+
job_name: str,
25+
labels: dict,
26+
) -> batch_v1.Job:
27+
"""
28+
This method creates a Batch job with custom labels.
29+
Args:
30+
project_id (str): project ID or project number of the Cloud project you want to use.
31+
region (str): name of the region you want to use to run the job. Regions that are
32+
available for Batch are listed on: https://cloud.google.com/batch/docs/locations
33+
job_name (str): the name of the job that will be created.
34+
labels (dict): A dictionary of custom labels to be added to the job.
35+
E.g., {"label_key1": "label_value2", "label_key2": "label_value2"}
36+
Returns:
37+
batch_v1.Job: The created Batch job object containing configuration details.
38+
"""
39+
client = batch_v1.BatchServiceClient()
40+
41+
runnable = batch_v1.Runnable()
42+
runnable.container = batch_v1.Runnable.Container()
43+
runnable.container.image_uri = "gcr.io/google-containers/busybox"
44+
runnable.container.entrypoint = "/bin/sh"
45+
runnable.container.commands = [
46+
"-c",
47+
"echo Hello world!",
48+
]
49+
50+
# Create a task specification and assign the runnable and volume to it
51+
task = batch_v1.TaskSpec()
52+
task.runnables = [runnable]
53+
54+
# Specify what resources are requested by each task.
55+
resources = batch_v1.ComputeResource()
56+
resources.cpu_milli = 2000 # in milliseconds per cpu-second. This means the task requires 2 whole CPUs.
57+
resources.memory_mib = 16 # in MiB
58+
task.compute_resource = resources
59+
60+
task.max_retry_count = 2
61+
task.max_run_duration = "3600s"
62+
63+
# Create a task group and assign the task specification to it
64+
group = batch_v1.TaskGroup()
65+
group.task_count = 3
66+
group.task_spec = task
67+
68+
# Policies are used to define on what kind of virtual machines the tasks will run on.
69+
# In this case, we tell the system to use "e2-standard-4" machine type.
70+
# Read more about machine types here: https://cloud.google.com/compute/docs/machine-types
71+
policy = batch_v1.AllocationPolicy.InstancePolicy()
72+
policy.machine_type = "e2-standard-4"
73+
instances = batch_v1.AllocationPolicy.InstancePolicyOrTemplate()
74+
instances.policy = policy
75+
allocation_policy = batch_v1.AllocationPolicy()
76+
allocation_policy.instances = [instances]
77+
78+
# Create the job and assign the task group and allocation policy to it
79+
job = batch_v1.Job()
80+
job.task_groups = [group]
81+
job.allocation_policy = allocation_policy
82+
83+
# Set the labels for the job
84+
job.labels = labels
85+
86+
# We use Cloud Logging as it's an out of the box available option
87+
job.logs_policy = batch_v1.LogsPolicy()
88+
job.logs_policy.destination = batch_v1.LogsPolicy.Destination.CLOUD_LOGGING
89+
90+
# Create the job request and set the job and job ID
91+
create_request = batch_v1.CreateJobRequest()
92+
create_request.job = job
93+
create_request.job_id = job_name
94+
# The job's parent is the region in which the job will run
95+
create_request.parent = f"projects/{project_id}/locations/{region}"
96+
97+
return client.create_job(create_request)
98+
99+
100+
# [END batch_labels_job]
101+
102+
103+
if __name__ == "__main__":
104+
PROJECT_ID = google.auth.default()[1]
105+
REGION = "us-central1"
106+
job_name = "your-job-name"
107+
labels = {"label_key1": "label_value2", "label_key2": "label_value2"}
108+
create_job_with_custom_job_labels(PROJECT_ID, REGION, job_name, labels)
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# Copyright 2024 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+
import google.auth
16+
17+
# [START batch_labels_runnable]
18+
from google.cloud import batch_v1
19+
20+
21+
def create_job_with_custom_runnables_labels(
22+
project_id: str,
23+
region: str,
24+
job_name: str,
25+
labels: dict,
26+
) -> batch_v1.Job:
27+
"""
28+
This method creates a Batch job with custom labels for runnable.
29+
Args:
30+
project_id (str): project ID or project number of the Cloud project you want to use.
31+
region (str): name of the region you want to use to run the job. Regions that are
32+
available for Batch are listed on: https://cloud.google.com/batch/docs/locations
33+
job_name (str): the name of the job that will be created.
34+
labels (dict): a dictionary of key-value pairs that will be used as labels
35+
E.g., {"label_key1": "label_value2"}
36+
Returns:
37+
batch_v1.Job: The created Batch job object containing configuration details.
38+
"""
39+
client = batch_v1.BatchServiceClient()
40+
41+
runnable = batch_v1.Runnable()
42+
runnable.display_name = "Script 1"
43+
runnable.script = batch_v1.Runnable.Script()
44+
runnable.script.text = "echo Hello world from Script 1 for task ${BATCH_TASK_INDEX}"
45+
# Add custom labels to the first runnable
46+
runnable.labels = labels
47+
48+
# Create a task specification and assign the runnable and volume to it
49+
task = batch_v1.TaskSpec()
50+
task.runnables = [runnable]
51+
52+
# Specify what resources are requested by each task.
53+
resources = batch_v1.ComputeResource()
54+
resources.cpu_milli = 2000 # in milliseconds per cpu-second. This means the task requires 2 whole CPUs.
55+
resources.memory_mib = 16 # in MiB
56+
task.compute_resource = resources
57+
58+
task.max_retry_count = 2
59+
task.max_run_duration = "3600s"
60+
61+
# Create a task group and assign the task specification to it
62+
group = batch_v1.TaskGroup()
63+
group.task_count = 3
64+
group.task_spec = task
65+
66+
# Policies are used to define on what kind of virtual machines the tasks will run on.
67+
# In this case, we tell the system to use "e2-standard-4" machine type.
68+
# Read more about machine types here: https://cloud.google.com/compute/docs/machine-types
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 = batch_v1.AllocationPolicy()
74+
allocation_policy.instances = [instances]
75+
76+
# Create the job and assign the task group and allocation policy to it
77+
job = batch_v1.Job()
78+
job.task_groups = [group]
79+
job.allocation_policy = allocation_policy
80+
81+
# We use Cloud Logging as it's an out of the box available option
82+
job.logs_policy = batch_v1.LogsPolicy()
83+
job.logs_policy.destination = batch_v1.LogsPolicy.Destination.CLOUD_LOGGING
84+
85+
# Create the job request and set the job and job ID
86+
create_request = batch_v1.CreateJobRequest()
87+
create_request.job = job
88+
create_request.job_id = job_name
89+
# The job's parent is the region in which the job will run
90+
create_request.parent = f"projects/{project_id}/locations/{region}"
91+
92+
return client.create_job(create_request)
93+
94+
95+
# [END batch_labels_runnable]
96+
97+
98+
if __name__ == "__main__":
99+
PROJECT_ID = google.auth.default()[1]
100+
REGION = "us-central1"
101+
job_name = "your-job-name"
102+
labels = {"label1": "value1"}
103+
create_job_with_custom_runnables_labels(PROJECT_ID, REGION, job_name, labels)

0 commit comments

Comments
 (0)