Source code for airflow.providers.google.cloud.sensors.dataproc
## 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 Dataproc Job sensor."""# pylint: disable=C0302importtimeimportwarningsfromtypingimportTYPE_CHECKING,Optional,Sequencefromgoogle.api_core.exceptionsimportServerErrorfromgoogle.cloud.dataproc_v1.typesimportJobStatusfromairflow.exceptionsimportAirflowExceptionfromairflow.providers.google.cloud.hooks.dataprocimportDataprocHookfromairflow.sensors.baseimportBaseSensorOperatorifTYPE_CHECKING:fromairflow.utils.contextimportContext
[docs]classDataprocJobSensor(BaseSensorOperator):""" Check for the state of a previously submitted Dataproc job. :param dataproc_job_id: The Dataproc job ID to poll. (templated) :param region: Required. The Cloud Dataproc region in which to handle the request. (templated) :param project_id: The ID of the google cloud project in which to create the cluster. (templated) :param location: (To be deprecated). The Cloud Dataproc region in which to handle the request. (templated) :param gcp_conn_id: The connection ID to use connecting to Google Cloud Platform. :param wait_timeout: How many seconds wait for job to be ready. """
def__init__(self,*,dataproc_job_id:str,region:Optional[str]=None,project_id:Optional[str]=None,location:Optional[str]=None,gcp_conn_id:str='google_cloud_default',wait_timeout:Optional[int]=None,**kwargs,)->None:ifregionisNone:iflocationisnotNone:warnings.warn("Parameter `location` will be deprecated. ""Please provide value through `region` parameter instead.",DeprecationWarning,stacklevel=2,)region=locationelse:raiseTypeError("missing 1 required keyword argument: 'region'")super().__init__(**kwargs)self.project_id=project_idself.gcp_conn_id=gcp_conn_idself.dataproc_job_id=dataproc_job_idself.region=regionself.wait_timeout=wait_timeoutself.start_sensor_time:Optional[float]=None
[docs]defpoke(self,context:"Context")->bool:hook=DataprocHook(gcp_conn_id=self.gcp_conn_id)ifself.wait_timeout:try:job=hook.get_job(job_id=self.dataproc_job_id,region=self.region,project_id=self.project_id)exceptServerErroraserr:self.log.info(f"DURATION RUN: {self._duration()}")ifself._duration()>self.wait_timeout:raiseAirflowException(f"Timeout: dataproc job {self.dataproc_job_id} "f"is not ready after {self.wait_timeout}s")self.log.info("Retrying. Dataproc API returned server error when waiting for job: %s",err)returnFalseelse:job=hook.get_job(job_id=self.dataproc_job_id,region=self.region,project_id=self.project_id)state=job.status.stateifstate==JobStatus.State.ERROR:raiseAirflowException(f'Job failed:\n{job}')elifstatein{JobStatus.State.CANCELLED,JobStatus.State.CANCEL_PENDING,JobStatus.State.CANCEL_STARTED,}:raiseAirflowException(f'Job was cancelled:\n{job}')elifJobStatus.State.DONE==state:self.log.debug("Job %s completed successfully.",self.dataproc_job_id)returnTrueelifJobStatus.State.ATTEMPT_FAILURE==state:self.log.debug("Job %s attempt has failed.",self.dataproc_job_id)self.log.info("Waiting for job %s to complete.",self.dataproc_job_id)returnFalse