Source code for airflow.providers.google.cloud.sensors.cloud_composer
## 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."""This module contains a Cloud Composer sensor."""from__future__importannotationsimportjsonfromcollections.abcimportIterable,Sequencefromdatetimeimportdatetime,timedeltafromtypingimportTYPE_CHECKINGfromdateutilimportparserfromgoogle.cloud.orchestration.airflow.service_v1.typesimportExecuteAirflowCommandResponsefromairflow.configurationimportconffromairflow.exceptionsimportAirflowExceptionfromairflow.providers.google.cloud.hooks.cloud_composerimportCloudComposerHookfromairflow.providers.google.cloud.triggers.cloud_composerimportCloudComposerDAGRunTriggerfromairflow.providers.google.common.constsimportGOOGLE_DEFAULT_DEFERRABLE_METHOD_NAMEfromairflow.sensors.baseimportBaseSensorOperatorfromairflow.utils.stateimportTaskInstanceStateifTYPE_CHECKING:fromairflow.utils.contextimportContext
[docs]classCloudComposerDAGRunSensor(BaseSensorOperator):""" Check if a DAG run has completed. :param project_id: Required. The ID of the Google Cloud project that the service belongs to. :param region: Required. The ID of the Google Cloud region that the service belongs to. :param environment_id: The name of the Composer environment. :param composer_dag_id: The ID of executable DAG. :param allowed_states: Iterable of allowed states, default is ``['success']``. :param execution_range: execution DAGs time range. Sensor checks DAGs states only for DAGs which were started in this time range. For yesterday, use [positive!] datetime.timedelta(days=1). For future, use [negative!] datetime.timedelta(days=-1). For specific time, use list of datetimes [datetime(2024,3,22,11,0,0), datetime(2024,3,22,12,0,0)]. Or [datetime(2024,3,22,0,0,0)] in this case sensor will check for states from specific time in the past till current time execution. Default value datetime.timedelta(days=1). :param gcp_conn_id: The connection ID to use when fetching connection info. :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). :param poll_interval: Optional: Control the rate of the poll for the result of deferrable run. :param deferrable: Run sensor in deferrable mode. """
[docs]defpoke(self,context:Context)->bool:start_date,end_date=self._get_logical_dates(context)ifdatetime.now(end_date.tzinfo)<end_date:returnFalsedag_runs=self._pull_dag_runs()self.log.info("Sensor waits for allowed states: %s",self.allowed_states)allowed_states_status=self._check_dag_runs_states(dag_runs=dag_runs,start_date=start_date,end_date=end_date,)returnallowed_states_status
def_pull_dag_runs(self)->list[dict]:"""Pull the list of dag runs."""hook=CloudComposerHook(gcp_conn_id=self.gcp_conn_id,impersonation_chain=self.impersonation_chain,)dag_runs_cmd=hook.execute_airflow_command(project_id=self.project_id,region=self.region,environment_id=self.environment_id,command="dags",subcommand="list-runs",parameters=["-d",self.composer_dag_id,"-o","json"],)cmd_result=hook.wait_command_execution_result(project_id=self.project_id,region=self.region,environment_id=self.environment_id,execution_cmd_info=ExecuteAirflowCommandResponse.to_dict(dag_runs_cmd),)dag_runs=json.loads(cmd_result["output"][0]["content"])returndag_runsdef_check_dag_runs_states(self,dag_runs:list[dict],start_date:datetime,end_date:datetime,)->bool:fordag_runindag_runs:if(start_date.timestamp()<parser.parse(dag_run["logical_date"]).timestamp()<end_date.timestamp())anddag_run["state"]notinself.allowed_states:returnFalsereturnTrue
[docs]defexecute_complete(self,context:Context,event:dict):ifeventandevent["status"]=="error":raiseAirflowException(event["message"])self.log.info("DAG %s has executed successfully.",self.composer_dag_id)