Skip to content

Commit f8528c2

Browse files
m-strzelczykparthea
authored andcommitted
docs(samples): Adding samples for list and get tasks (#50)
* Fixing tests * docs(samples): Adding samples for list tasks and get task Co-authored-by: Anthonios Partheniou <[email protected]>
1 parent 258d611 commit f8528c2

8 files changed

+94
-4
lines changed

compute/batch/snippets/create/create_with_container_no_mounting.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ def create_container_job(project_id: str, region: str, job_name: str) -> batch_v
5454
task.max_run_duration = "3600s"
5555

5656
# Tasks are grouped inside a job using TaskGroups.
57+
# Currently, it's possible to have only one task group.
5758
group = batch_v1.TaskGroup()
5859
group.task_count = 4
5960
group.task_spec = task

compute/batch/snippets/create/create_with_mounted_bucket.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ def create_script_job_with_bucket(project_id: str, region: str, job_name: str, b
5858
task.max_run_duration = "3600s"
5959

6060
# Tasks are grouped inside a job using TaskGroups.
61+
# Currently, it's possible to have only one task group.
6162
group = batch_v1.TaskGroup()
6263
group.task_count = 4
6364
group.task_spec = task

compute/batch/snippets/create/create_with_script_no_mounting.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ def create_script_job(project_id: str, region: str, job_name: str) -> batch_v1.J
5454
task.max_run_duration = "3600s"
5555

5656
# Tasks are grouped inside a job using TaskGroups.
57+
# Currently, it's possible to have only one task group.
5758
group = batch_v1.TaskGroup()
5859
group.task_count = 4
5960
group.task_spec = task

compute/batch/snippets/create/create_with_template.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ def create_script_job_with_template(project_id: str, region: str, job_name: str,
5757
task.max_run_duration = "3600s"
5858

5959
# Tasks are grouped inside a job using TaskGroups.
60+
# Currently, it's possible to have only one task group.
6061
group = batch_v1.TaskGroup()
6162
group.task_count = 4
6263
group.task_spec = task
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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_task]
16+
17+
from google.cloud import batch_v1
18+
19+
20+
def get_task(project_id: str, region: str, job_name: str, group_name: str, task_number: int) -> batch_v1.Task:
21+
"""
22+
Retrieve information about a Task.
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 hosts the job.
27+
job_name: the name of the job you want to retrieve information about.
28+
group_name: the name of the group that owns the task you want to check. Usually it's `group0`.
29+
task_number: number of the task you want to look up.
30+
31+
Returns:
32+
A Task object representing the specified task.
33+
"""
34+
client = batch_v1.BatchServiceClient()
35+
36+
return client.get_task(name=f"projects/{project_id}/locations/{region}/jobs/{job_name}"
37+
f"/taskGroups/{group_name}/tasks/{task_number}")
38+
# [END batch_get_task]

compute/batch/snippets/list/list_jobs.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14-
from typing import Iterable
1514

1615
# [START batch_list_jobs]
16+
from typing import Iterable
17+
1718
from google.cloud import batch_v1
1819

1920

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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_list_tasks]
16+
from typing import Iterable
17+
18+
from google.cloud import batch_v1
19+
20+
21+
def list_tasks(project_id: str, region: str, job_name: str, group_name: str) -> Iterable[batch_v1.Task]:
22+
"""
23+
Get a list of all jobs defined in given region.
24+
25+
Args:
26+
project_id: project ID or project number of the Cloud project you want to use.
27+
region: name of the region hosting the jobs.
28+
job_name: name of the job which tasks you want to list.
29+
group_name: name of the group of tasks. Usually it's `group0`.
30+
31+
Returns:
32+
An iterable collection of Task objects.
33+
"""
34+
client = batch_v1.BatchServiceClient()
35+
36+
return client.list_tasks(parent=f"projects/{project_id}/locations/{region}/jobs/{job_name}/taskGroups/{group_name}")
37+
# [END batch_list_tasks]

compute/batch/snippets/tests/test_basics.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424

2525
from ..delete.delete_job import delete_job
2626
from ..get.get_job import get_job
27+
from ..get.get_task import get_task
2728
from ..list.list_jobs import list_jobs
29+
from ..list.list_tasks import list_tasks
2830

2931
PROJECT = google.auth.default()[1]
3032
REGION = 'europe-north1'
@@ -36,6 +38,7 @@
3638
batch_v1.JobStatus.State.QUEUED,
3739
batch_v1.JobStatus.State.RUNNING,
3840
batch_v1.JobStatus.State.SCHEDULED,
41+
batch_v1.JobStatus.State.DELETION_IN_PROGRESS
3942
}
4043

4144

@@ -53,8 +56,7 @@ def _test_body(test_job: batch_v1.Job, additional_test: Callable = None):
5356
test_job = get_job(PROJECT, REGION, test_job.name.rsplit('/', maxsplit=1)[1])
5457
time.sleep(5)
5558

56-
assert test_job.status.state in (batch_v1.JobStatus.State.SUCCEEDED,
57-
batch_v1.JobStatus.State.DELETION_IN_PROGRESS)
59+
assert test_job.status.state == batch_v1.JobStatus.State.SUCCEEDED
5860

5961
for job in list_jobs(PROJECT, REGION):
6062
if test_job.uid == job.uid:
@@ -72,9 +74,17 @@ def _test_body(test_job: batch_v1.Job, additional_test: Callable = None):
7274
pytest.fail("The test job should be deleted at this point!")
7375

7476

77+
def _check_tasks(job_name):
78+
tasks = list_tasks(PROJECT, REGION, job_name, 'group0')
79+
assert len(list(tasks)) == 4
80+
for i in range(4):
81+
assert get_task(PROJECT, REGION, job_name, 'group0', i) is not None
82+
print('Tasks tested')
83+
84+
7585
def test_script_job(job_name):
7686
job = create_script_job(PROJECT, REGION, job_name)
77-
_test_body(job)
87+
_test_body(job, additional_test=lambda: _check_tasks(job_name))
7888

7989

8090
def test_container_job(job_name):

0 commit comments

Comments
 (0)