Source code for airflow.providers.google.cloud.triggers.vertex_ai
# Licensed to the Apache Software Foundation (ASF) under one# or more contributor license agreements. See the NOTICE file# distributed with this work for additional information# regarding copyright ownership. The ASF licenses this file# to you under the Apache License, Version 2.0 (the# "License"); you may not use this file except in compliance# with the License. You may obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing,# software distributed under the License is distributed on an# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY# KIND, either express or implied. See the License for the# specific language governing permissions and limitations# under the License.from__future__importannotationsfromtypingimportAny,AsyncIterator,Sequencefromgoogle.cloud.aiplatform_v1importHyperparameterTuningJob,JobStatefromairflow.exceptionsimportAirflowExceptionfromairflow.providers.google.cloud.hooks.vertex_ai.hyperparameter_tuning_jobimport(HyperparameterTuningJobAsyncHook,)fromairflow.triggers.baseimportBaseTrigger,TriggerEvent
[docs]classCreateHyperparameterTuningJobTrigger(BaseTrigger):"""CreateHyperparameterTuningJobTrigger run on the trigger worker to perform create operation."""
[docs]asyncdefrun(self)->AsyncIterator[TriggerEvent]:hook=self._get_async_hook()try:job=awaithook.wait_hyperparameter_tuning_job(project_id=self.project_id,location=self.location,job_id=self.job_id,poll_interval=self.poll_interval,)exceptAirflowExceptionasex:yieldTriggerEvent({"status":"error","message":str(ex),})returnstatus="success"ifjob.stateinself.statuses_successelse"error"message=f"Hyperparameter tuning job {job.name} completed with status {job.state.name}"yieldTriggerEvent({"status":status,"message":message,"job":HyperparameterTuningJob.to_dict(job),})