|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +# Copyright 2023 Google LLC |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +# |
| 17 | + |
| 18 | +from typing import Optional |
| 19 | + |
| 20 | +from google.cloud.aiplatform import base |
| 21 | +from google.cloud.aiplatform import pipeline_jobs |
| 22 | +from google.cloud.aiplatform import utils |
| 23 | +from google.cloud.aiplatform.constants import pipeline as pipeline_constants |
| 24 | +from google.cloud.aiplatform.metadata import constants as metadata_constants |
| 25 | +from google.cloud.aiplatform.metadata import experiment_resources |
| 26 | + |
| 27 | +_LOGGER = base.Logger(__name__) |
| 28 | + |
| 29 | +_PIPELINE_COMPLETE_STATES = pipeline_constants._PIPELINE_COMPLETE_STATES |
| 30 | + |
| 31 | +_PIPELINE_ERROR_STATES = pipeline_constants._PIPELINE_ERROR_STATES |
| 32 | + |
| 33 | +# Pattern for valid names used as a Vertex resource name. |
| 34 | +_VALID_NAME_PATTERN = pipeline_constants._VALID_NAME_PATTERN |
| 35 | + |
| 36 | +# Pattern for an Artifact Registry URL. |
| 37 | +_VALID_AR_URL = pipeline_constants._VALID_AR_URL |
| 38 | + |
| 39 | +# Pattern for any JSON or YAML file over HTTPS. |
| 40 | +_VALID_HTTPS_URL = pipeline_constants._VALID_HTTPS_URL |
| 41 | + |
| 42 | +_READ_MASK_FIELDS = pipeline_constants._READ_MASK_FIELDS |
| 43 | + |
| 44 | + |
| 45 | +class _PipelineJob( |
| 46 | + pipeline_jobs.PipelineJob, |
| 47 | + experiment_loggable_schemas=( |
| 48 | + experiment_resources._ExperimentLoggableSchema( |
| 49 | + title=metadata_constants.SYSTEM_PIPELINE_RUN |
| 50 | + ), |
| 51 | + ), |
| 52 | +): |
| 53 | + """Preview PipelineJob resource for Vertex AI.""" |
| 54 | + |
| 55 | + def create_schedule( |
| 56 | + self, |
| 57 | + cron_expression: str, |
| 58 | + display_name: str, |
| 59 | + start_time: Optional[str] = None, |
| 60 | + end_time: Optional[str] = None, |
| 61 | + allow_queueing: bool = False, |
| 62 | + max_run_count: Optional[int] = None, |
| 63 | + max_concurrent_run_count: int = 1, |
| 64 | + service_account: Optional[str] = None, |
| 65 | + network: Optional[str] = None, |
| 66 | + create_request_timeout: Optional[float] = None, |
| 67 | + ) -> "pipeline_job_schedules.PipelineJobSchedule": # noqa: F821 |
| 68 | + """Creates a PipelineJobSchedule directly from a PipelineJob. |
| 69 | +
|
| 70 | + Example Usage: |
| 71 | +
|
| 72 | + pipeline_job = aiplatform.PipelineJob( |
| 73 | + display_name='job_display_name', |
| 74 | + template_path='your_pipeline.yaml', |
| 75 | + ) |
| 76 | + pipeline_job.run() |
| 77 | + pipeline_job_schedule = pipeline_job.create_schedule( |
| 78 | + cron_expression='* * * * *', |
| 79 | + display_name='schedule_display_name', |
| 80 | + ) |
| 81 | +
|
| 82 | + Args: |
| 83 | + cron_expression (str): |
| 84 | + Required. Time specification (cron schedule expression) to launch scheduled runs. |
| 85 | + To explicitly set a timezone to the cron tab, apply a prefix: "CRON_TZ=${IANA_TIME_ZONE}" or "TZ=${IANA_TIME_ZONE}". |
| 86 | + The ${IANA_TIME_ZONE} may only be a valid string from IANA time zone database. |
| 87 | + For example, "CRON_TZ=America/New_York 1 * * * *", or "TZ=America/New_York 1 * * * *". |
| 88 | + display_name (str): |
| 89 | + Required. The user-defined name of this PipelineJobSchedule. |
| 90 | + start_time (str): |
| 91 | + Optional. Timestamp after which the first run can be scheduled. |
| 92 | + If unspecified, it defaults to the schedule creation timestamp. |
| 93 | + end_time (str): |
| 94 | + Optional. Timestamp after which no more runs will be scheduled. |
| 95 | + If unspecified, then runs will be scheduled indefinitely. |
| 96 | + allow_queueing (bool): |
| 97 | + Optional. Whether new scheduled runs can be queued when max_concurrent_runs limit is reached. |
| 98 | + max_run_count (int): |
| 99 | + Optional. Maximum run count of the schedule. |
| 100 | + If specified, The schedule will be completed when either started_run_count >= max_run_count or when end_time is reached. |
| 101 | + max_concurrent_run_count (int): |
| 102 | + Optional. Maximum number of runs that can be started concurrently for this PipelineJobSchedule. |
| 103 | + service_account (str): |
| 104 | + Optional. Specifies the service account for workload run-as account. |
| 105 | + Users submitting jobs must have act-as permission on this run-as account. |
| 106 | + network (str): |
| 107 | + Optional. The full name of the Compute Engine network to which the job |
| 108 | + should be peered. For example, projects/12345/global/networks/myVPC. |
| 109 | + Private services access must already be configured for the network. |
| 110 | + If left unspecified, the network set in aiplatform.init will be used. |
| 111 | + Otherwise, the job is not peered with any network. |
| 112 | + create_request_timeout (float): |
| 113 | + Optional. The timeout for the create request in seconds. |
| 114 | +
|
| 115 | + Returns: |
| 116 | + A Vertex AI PipelineJobSchedule. |
| 117 | + """ |
| 118 | + from google.cloud.aiplatform.preview.pipelinejobschedule import ( |
| 119 | + pipeline_job_schedules, |
| 120 | + ) |
| 121 | + |
| 122 | + if not display_name: |
| 123 | + display_name = self._generate_display_name(prefix="PipelineJobSchedule") |
| 124 | + utils.validate_display_name(display_name) |
| 125 | + |
| 126 | + pipeline_job_schedule = pipeline_job_schedules.PipelineJobSchedule( |
| 127 | + pipeline_job=self, |
| 128 | + display_name=display_name, |
| 129 | + ) |
| 130 | + |
| 131 | + pipeline_job_schedule.create( |
| 132 | + cron_expression=cron_expression, |
| 133 | + start_time=start_time, |
| 134 | + end_time=end_time, |
| 135 | + allow_queueing=allow_queueing, |
| 136 | + max_run_count=max_run_count, |
| 137 | + max_concurrent_run_count=max_concurrent_run_count, |
| 138 | + service_account=service_account, |
| 139 | + network=network, |
| 140 | + create_request_timeout=create_request_timeout, |
| 141 | + ) |
| 142 | + return pipeline_job_schedule |
0 commit comments