Source code for airflow.providers.google.cloud.triggers.bigquery_dts
# 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__importannotationsimportasynciofromtypingimportAny,AsyncIterator,Sequencefromgoogle.cloud.bigquery_datatransfer_v1importTransferRun,TransferStatefromairflow.providers.google.cloud.hooks.bigquery_dtsimportAsyncBiqQueryDataTransferServiceHookfromairflow.triggers.baseimportBaseTrigger,TriggerEvent
[docs]classBigQueryDataTransferRunTrigger(BaseTrigger):""" Triggers class to watch the Transfer Run state to define when the job is done. :param project_id: The BigQuery project id where the transfer configuration should be :param config_id: ID of the config of the Transfer Run which should be watched. :param run_id: ID of the Transfer Run which should be watched. :param poll_interval: Optional. Interval which defines how often triggers check status of the job. :param gcp_conn_id: The connection ID used to connect to Google Cloud. :param location: BigQuery Transfer Service location for regional transfers. :param impersonation_chain: Optional service account to impersonate using short-term credentials, or chained list of accounts required to get the access_token of the last account in the list, which will be impersonated in the request. If set as a string, the account must grant the originating account the Service Account Token Creator IAM role. If set as a sequence, the identities from the list must grant Service Account Token Creator IAM role to the directly preceding identity, with first account from the list granting this role to the originating account (templated). """def__init__(self,project_id:str|None,config_id:str,run_id:str,poll_interval:int=10,gcp_conn_id:str="google_cloud_default",location:str|None=None,impersonation_chain:str|Sequence[str]|None=None,):super().__init__()self.project_id=project_idself.config_id=config_idself.run_id=run_idself.poll_interval=poll_intervalself.gcp_conn_id=gcp_conn_idself.location=locationself.impersonation_chain=impersonation_chain
[docs]defserialize(self)->tuple[str,dict[str,Any]]:"""Serialize class arguments and classpath."""return("airflow.providers.google.cloud.triggers.bigquery_dts.BigQueryDataTransferRunTrigger",{"project_id":self.project_id,"config_id":self.config_id,"run_id":self.run_id,"poll_interval":self.poll_interval,"gcp_conn_id":self.gcp_conn_id,"location":self.location,"impersonation_chain":self.impersonation_chain,},)
[docs]asyncdefrun(self)->AsyncIterator[TriggerEvent]:"""If the Transfer Run is in a terminal state, then yield TriggerEvent object."""hook=self._get_async_hook()try:whileTrue:transfer_run:TransferRun=awaithook.get_transfer_run(project_id=self.project_id,config_id=self.config_id,run_id=self.run_id,location=self.location,)state=transfer_run.stateself.log.info("Current state is %s",state)ifstate==TransferState.SUCCEEDED:self.log.info("Job has completed its work.")yieldTriggerEvent({"status":"success","run_id":self.run_id,"message":"Job completed","config_id":self.config_id,})returnelifstate==TransferState.FAILED:self.log.info("Job has failed")yieldTriggerEvent({"status":"failed","run_id":self.run_id,"message":"Job has failed",})returnifstate==TransferState.CANCELLED:self.log.info("Job has been cancelled.")yieldTriggerEvent({"status":"cancelled","run_id":self.run_id,"message":"Job was cancelled",})returnelse:self.log.info("Job is still working...")self.log.info("Waiting for %s seconds",self.poll_interval)awaitasyncio.sleep(self.poll_interval)exceptExceptionase:yieldTriggerEvent({"status":"failed","message":f"Trigger failed with exception: {e}",})