Source code for airflow.providers.amazon.aws.triggers.glue_crawler
# 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__importannotationsimportasynciofromfunctoolsimportcached_propertyfromtypingimportAsyncIteratorfrombotocore.exceptionsimportWaiterErrorfromairflow.providers.amazon.aws.hooks.glue_crawlerimportGlueCrawlerHookfromairflow.triggers.baseimportBaseTrigger,TriggerEvent
[docs]classGlueCrawlerCompleteTrigger(BaseTrigger):""" Watches for a glue crawl, triggers when it finishes. :param crawler_name: name of the crawler to watch :param poll_interval: The amount of time in seconds to wait between attempts. :param aws_conn_id: The Airflow connection used for AWS credentials. """def__init__(self,crawler_name:str,poll_interval:int,aws_conn_id:str):super().__init__()self.crawler_name=crawler_nameself.poll_interval=poll_intervalself.aws_conn_id=aws_conn_id
[docs]defserialize(self)->tuple[str,dict]:return(# dynamically generate the fully qualified name of the classself.__class__.__module__+"."+self.__class__.__qualname__,{"crawler_name":self.crawler_name,"poll_interval":self.poll_interval,"aws_conn_id":self.aws_conn_id,},)
[docs]asyncdefrun(self)->AsyncIterator[TriggerEvent]:asyncwithself.hook.async_connasclient:waiter=self.hook.get_waiter("crawler_ready",deferrable=True,client=client)whileTrue:try:awaitwaiter.wait(Name=self.crawler_name,WaiterConfig={"Delay":self.poll_interval,"MaxAttempts":1},)break# we reach this point only if the waiter met a success criteriaexceptWaiterErroraserror:if"terminal failure"instr(error):raiseself.log.info("Status of glue crawl is %s",error.last_response["Crawler"]["State"])awaitasyncio.sleep(int(self.poll_interval))yieldTriggerEvent({"status":"success","message":"Crawl Complete"})