Skip to content

Commit 030e22d

Browse files
committed
Add logic and tests for ListJobs
1 parent 998a117 commit 030e22d

File tree

2 files changed

+406
-5
lines changed

2 files changed

+406
-5
lines changed

databricks/sdk/mixins/jobs.py

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,64 @@
1-
from typing import Optional
1+
from typing import Iterator, Optional
22

33
from databricks.sdk.service import jobs
4-
from databricks.sdk.service.jobs import Job
4+
from databricks.sdk.service.jobs import BaseJob, Job
55

66

77
class JobsExt(jobs.JobsAPI):
88

9+
def list(self,
10+
*,
11+
expand_tasks: Optional[bool] = None,
12+
limit: Optional[int] = None,
13+
name: Optional[str] = None,
14+
offset: Optional[int] = None,
15+
page_token: Optional[str] = None) -> Iterator[BaseJob]:
16+
"""List jobs.
17+
18+
Retrieves a list of jobs. If the job has multiple pages of tasks, job_clusters, parameters or environments,
19+
it will paginate through all pages and aggregate the results.
20+
21+
:param expand_tasks: bool (optional)
22+
Whether to include task and cluster details in the response. Note that in API 2.2, only the first
23+
100 elements will be shown. Use :method:jobs/get to paginate through all tasks and clusters.
24+
:param limit: int (optional)
25+
The number of jobs to return. This value must be greater than 0 and less or equal to 100. The
26+
default value is 20.
27+
:param name: str (optional)
28+
A filter on the list based on the exact (case insensitive) job name.
29+
:param offset: int (optional)
30+
The offset of the first job to return, relative to the most recently created job. Deprecated since
31+
June 2023. Use `page_token` to iterate through the pages instead.
32+
:param page_token: str (optional)
33+
Use `next_page_token` or `prev_page_token` returned from the previous request to list the next or
34+
previous page of jobs respectively.
35+
36+
:returns: Iterator over :class:`BaseJob`
37+
"""
38+
# fetch jobs with limited elements in top level arrays
39+
jobs_list = super().list(expand_tasks=expand_tasks,
40+
limit=limit,
41+
name=name,
42+
offset=offset,
43+
page_token=page_token)
44+
if not expand_tasks:
45+
yield from jobs_list
46+
47+
# fully fetch all top level arrays for each job in the list
48+
for job in jobs_list:
49+
if job.has_more:
50+
job_from_get_call = self.get(job.job_id)
51+
job.settings.tasks = job_from_get_call.settings.tasks
52+
job.settings.job_clusters = job_from_get_call.settings.job_clusters
53+
job.settings.parameters = job_from_get_call.settings.parameters
54+
job.settings.environments = job_from_get_call.settings.environments
55+
# Remove has_more fields for each job in the list.
56+
# This field in Jobs API 2.2 is useful for pagination. It indicates if there are more than 100 tasks or job_clusters in the job.
57+
# This function hides pagination details from the user. So the field does not play useful role here.
58+
if hasattr(job, 'has_more'):
59+
delattr(job, 'has_more')
60+
yield job
61+
962
def get_run(self,
1063
run_id: int,
1164
*,

0 commit comments

Comments
 (0)