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__importannotationsimportjsonfromdatetimeimportdatetime,timedeltafromtypingimportTYPE_CHECKING,Any,Iterable,Sequencefromdateutilimportparserfromdeprecatedimportdeprecatedfromgoogle.cloud.orchestration.airflow.service_v1.typesimportExecuteAirflowCommandResponsefromairflow.configurationimportconffromairflow.exceptionsimportAirflowException,AirflowProviderDeprecationWarning,AirflowSkipExceptionfromairflow.providers.google.cloud.hooks.cloud_composerimportCloudComposerHookfromairflow.providers.google.cloud.triggers.cloud_composerimport(CloudComposerDAGRunTrigger,CloudComposerExecutionTrigger,)fromairflow.providers.google.common.constsimportGOOGLE_DEFAULT_DEFERRABLE_METHOD_NAMEfromairflow.sensors.baseimportBaseSensorOperatorfromairflow.utils.stateimportTaskInstanceStateifTYPE_CHECKING:fromairflow.utils.contextimportContext@deprecated(reason=("The `CloudComposerEnvironmentSensor` operator is deprecated. ""You can achieve the same functionality ""by using operators in deferrable or non-deferrable mode, since every operator for Cloud ""Composer will wait for the operation to complete."),category=AirflowProviderDeprecationWarning,)
[docs]classCloudComposerEnvironmentSensor(BaseSensorOperator):""" Check the status of the Cloud Composer Environment task. This Sensor is deprecated. You can achieve the same functionality by using Cloud Composer Operators CloudComposerCreateEnvironmentOperator, CloudComposerDeleteEnvironmentOperator and CloudComposerUpdateEnvironmentOperator in deferrable or non-deferrable mode, since every operator gives user a possibility to wait (asynchronously or synchronously) until Operation will be finished. :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 operation_name: The name of the operation resource :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 pooling_period_seconds: Optional: Control the rate of the poll for the result of deferrable run. """def__init__(self,*,project_id:str,region:str,operation_name:str,gcp_conn_id:str="google_cloud_default",impersonation_chain:str|Sequence[str]|None=None,pooling_period_seconds:int=30,**kwargs,):super().__init__(**kwargs)self.project_id=project_idself.region=regionself.operation_name=operation_nameself.pooling_period_seconds=pooling_period_secondsself.gcp_conn_id=gcp_conn_idself.impersonation_chain=impersonation_chain
[docs]defexecute(self,context:Context)->None:"""Airflow runs this method on the worker and defers using the trigger."""self.defer(trigger=CloudComposerExecutionTrigger(project_id=self.project_id,region=self.region,operation_name=self.operation_name,gcp_conn_id=self.gcp_conn_id,impersonation_chain=self.impersonation_chain,pooling_period_seconds=self.pooling_period_seconds,),method_name="execute_complete",)
[docs]defexecute_complete(self,context:dict[str,Any],event:dict[str,str]|None=None)->str:""" Act as a callback for when the trigger fires - returns immediately. Relies on trigger to throw an exception, otherwise it assumes execution was successful. """ifevent:ifevent.get("operation_done"):returnevent["operation_done"]# TODO: remove this if check when min_airflow_version is set to higher than 2.7.1ifself.soft_fail:raiseAirflowSkipException(event["message"])raiseAirflowException(event["message"])# TODO: remove this if check when min_airflow_version is set to higher than 2.7.1message="No event received in trigger callback"ifself.soft_fail:raiseAirflowSkipException(message)raiseAirflowException(message)
[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_execution_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["execution_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)