Skip to content

Commit ce5dee4

Browse files
vertex-sdk-botcopybara-github
authored andcommitted
feat: Add scheduled pipelines client list/pause/resume methods and unit tests.
PiperOrigin-RevId: 537995661
1 parent 74c2066 commit ce5dee4

File tree

3 files changed

+560
-7
lines changed

3 files changed

+560
-7
lines changed

google/cloud/aiplatform/preview/pipelinejobschedule/pipeline_job_schedules.py

+131-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# limitations under the License.
1616
#
1717

18-
from typing import Optional
18+
from typing import List, Optional
1919

2020
from google.auth import credentials as auth_credentials
2121
from google.cloud.aiplatform import base
@@ -39,6 +39,7 @@
3939
from google.cloud.aiplatform_v1beta1.types import (
4040
pipeline_job as gca_pipeline_job_v1beta1,
4141
)
42+
from google.protobuf import field_mask_pb2 as field_mask
4243

4344

4445
_LOGGER = base.Logger(__name__)
@@ -68,6 +69,7 @@ def __init__(
6869
):
6970
"""Retrieves a PipelineJobSchedule resource and instantiates its
7071
representation.
72+
7173
Args:
7274
pipeline_job (PipelineJob):
7375
Required. PipelineJob used to init the schedule.
@@ -255,3 +257,131 @@ def _create(
255257
)
256258

257259
_LOGGER.info("View Schedule:\n%s" % self._dashboard_uri())
260+
261+
@classmethod
262+
def list(
263+
cls,
264+
filter: Optional[str] = None,
265+
order_by: Optional[str] = None,
266+
enable_simple_view: bool = True,
267+
project: Optional[str] = None,
268+
location: Optional[str] = None,
269+
credentials: Optional[auth_credentials.Credentials] = None,
270+
) -> List["PipelineJobSchedule"]:
271+
"""List all instances of this PipelineJobSchedule resource.
272+
273+
Example Usage:
274+
275+
aiplatform.PipelineJobSchedule.list(
276+
filter='display_name="experiment_a27"',
277+
order_by='create_time desc'
278+
)
279+
280+
Args:
281+
filter (str):
282+
Optional. An expression for filtering the results of the request.
283+
For field names both snake_case and camelCase are supported.
284+
order_by (str):
285+
Optional. A comma-separated list of fields to order by, sorted in
286+
ascending order. Use "desc" after a field name for descending.
287+
Supported fields: `display_name`, `create_time`, `update_time`
288+
enable_simple_view (bool):
289+
Optional. Whether to pass the `read_mask` parameter to the list call.
290+
Defaults to False if not provided. This will improve the performance of calling
291+
list(). However, the returned PipelineJobSchedule list will not include all fields for
292+
each PipelineJobSchedule. Setting this to True will exclude the following fields in your
293+
response: 'create_pipeline_job_request', 'next_run_time', 'last_pause_time',
294+
'last_resume_time', 'max_concurrent_run_count', 'allow_queueing','last_scheduled_run_response'.
295+
The following fields will be included in each PipelineJobSchedule resource in your
296+
response: 'name', 'display_name', 'start_time', 'end_time', 'max_run_count',
297+
'started_run_count', 'state', 'create_time', 'update_time', 'cron', 'catch_up'.
298+
project (str):
299+
Optional. Project to retrieve list from. If not set, project
300+
set in aiplatform.init will be used.
301+
location (str):
302+
Optional. Location to retrieve list from. If not set, location
303+
set in aiplatform.init will be used.
304+
credentials (auth_credentials.Credentials):
305+
Optional. Custom credentials to use to retrieve list. Overrides
306+
credentials set in aiplatform.init.
307+
308+
Returns:
309+
List[PipelineJobSchedule] - A list of PipelineJobSchedule resource objects.
310+
"""
311+
312+
read_mask_fields = None
313+
314+
if enable_simple_view:
315+
read_mask_fields = field_mask.FieldMask(paths=_READ_MASK_FIELDS)
316+
_LOGGER.warn(
317+
"By enabling simple view, the PipelineJobSchedule resources returned from this method will not contain all fields."
318+
)
319+
320+
return cls._list_with_local_order(
321+
filter=filter,
322+
order_by=order_by,
323+
read_mask=read_mask_fields,
324+
project=project,
325+
location=location,
326+
credentials=credentials,
327+
)
328+
329+
def list_jobs(
330+
self,
331+
filter: Optional[str] = None,
332+
order_by: Optional[str] = None,
333+
enable_simple_view: bool = False,
334+
project: Optional[str] = None,
335+
location: Optional[str] = None,
336+
credentials: Optional[auth_credentials.Credentials] = None,
337+
) -> List[PipelineJob]:
338+
"""List all PipelineJob 's created by this PipelineJobSchedule.
339+
340+
Example usage:
341+
342+
pipeline_job_schedule.list_jobs(order_by='create_time_desc')
343+
344+
Args:
345+
filter (str):
346+
Optional. An expression for filtering the results of the request.
347+
For field names both snake_case and camelCase are supported.
348+
order_by (str):
349+
Optional. A comma-separated list of fields to order by, sorted in
350+
ascending order. Use "desc" after a field name for descending.
351+
Supported fields: `display_name`, `create_time`, `update_time`
352+
enable_simple_view (bool):
353+
Optional. Whether to pass the `read_mask` parameter to the list call.
354+
Defaults to False if not provided. This will improve the performance of calling
355+
list(). However, the returned PipelineJob list will not include all fields for
356+
each PipelineJob. Setting this to True will exclude the following fields in your
357+
response: `runtime_config`, `service_account`, `network`, and some subfields of
358+
`pipeline_spec` and `job_detail`. The following fields will be included in
359+
each PipelineJob resource in your response: `state`, `display_name`,
360+
`pipeline_spec.pipeline_info`, `create_time`, `start_time`, `end_time`,
361+
`update_time`, `labels`, `template_uri`, `template_metadata.version`,
362+
`job_detail.pipeline_run_context`, `job_detail.pipeline_context`.
363+
project (str):
364+
Optional. Project to retrieve list from. If not set, project
365+
set in aiplatform.init will be used.
366+
location (str):
367+
Optional. Location to retrieve list from. If not set, location
368+
set in aiplatform.init will be used.
369+
credentials (auth_credentials.Credentials):
370+
Optional. Custom credentials to use to retrieve list. Overrides
371+
credentials set in aiplatform.init.
372+
373+
Returns:
374+
List[PipelineJob] - A list of PipelineJob resource objects.
375+
"""
376+
list_filter = f"schedule_name={self._gca_resource.name}"
377+
if filter:
378+
list_filter = list_filter + f" AND {filter}"
379+
380+
return PipelineJob.list(
381+
filter=list_filter,
382+
order_by=order_by,
383+
enable_simple_view=enable_simple_view,
384+
project=project,
385+
location=location,
386+
credentials=credentials,
387+
)

google/cloud/aiplatform/preview/schedule/schedules.py

+29-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ def __init__(
6060
location: str,
6161
):
6262
"""Retrieves a Schedule resource and instantiates its representation.
63-
6463
Args:
6564
credentials (auth_credentials.Credentials):
6665
Optional. Custom credentials to use to create this Schedule.
@@ -111,6 +110,35 @@ def get(
111110

112111
return self
113112

113+
def pause(self) -> None:
114+
"""Starts asynchronous pause on the Schedule.
115+
116+
Changes Schedule state from State.ACTIVE to State.PAUSED.
117+
"""
118+
self.api_client.pause_schedule(name=self.resource_name)
119+
120+
def resume(
121+
self,
122+
catch_up: bool = True,
123+
) -> None:
124+
"""Starts asynchronous resume on the Schedule.
125+
126+
Changes Schedule state from State.PAUSED to State.ACTIVE.
127+
128+
Args:
129+
catch_up (bool):
130+
Optional. Whether to backfill missed runs when the Schedule is
131+
resumed from State.PAUSED.
132+
"""
133+
self.api_client.resume_schedule(name=self.resource_name)
134+
135+
def done(self) -> bool:
136+
"""Helper method that return True is Schedule is done. False otherwise."""
137+
if not self._gca_resource:
138+
return False
139+
140+
return self.state in _SCHEDULE_COMPLETE_STATES
141+
114142
def wait(self) -> None:
115143
"""Wait for this Schedule to complete."""
116144
if self._latest_future is None:

0 commit comments

Comments
 (0)