Source code for airflow.providers.sftp.triggers.sftp
# 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__importannotationsimportasynciofromcollections.abcimportAsyncIteratorfromdatetimeimportdatetimefromtypingimportAnyfromdateutil.parserimportparseasparse_datefromairflow.exceptionsimportAirflowExceptionfromairflow.providers.sftp.hooks.sftpimportSFTPHookAsyncfromairflow.triggers.baseimportBaseTrigger,TriggerEventfromairflow.utils.timezoneimportconvert_to_utc
[docs]classSFTPTrigger(BaseTrigger):""" SFTPTrigger that fires in below listed scenarios. 1. The path on the SFTP server does not exist 2. The pattern do not match :param path: The path on the SFTP server to search for a file matching the file pattern. Authentication method used in the SFTP connection must have access to this path :param file_pattern: Pattern to be used for matching against the list of files at the path above. Uses the fnmatch module from std library to perform the matching. :param sftp_conn_id: SFTP connection ID to be used for connecting to SFTP server :param poke_interval: How often, in seconds, to check for the existence of the file on the SFTP server """def__init__(self,path:str,file_pattern:str="",sftp_conn_id:str="sftp_default",newer_than:datetime|str|None=None,poke_interval:float=5,)->None:super().__init__()
[docs]defserialize(self)->tuple[str,dict[str,Any]]:"""Serialize SFTPTrigger arguments and classpath."""return("airflow.providers.sftp.triggers.sftp.SFTPTrigger",{"path":self.path,"file_pattern":self.file_pattern,"sftp_conn_id":self.sftp_conn_id,"newer_than":self.newer_than,"poke_interval":self.poke_interval,},)
[docs]asyncdefrun(self)->AsyncIterator[TriggerEvent]:""" Make a series of asynchronous calls to sftp servers via async sftp hook. It yields a Trigger. - If file matching file pattern exists at the specified path return it, - If file pattern was not provided, it looks directly into the specific path which was provided. - If newer then datetime was provided it looks for the file path last modified time and check whether the last modified time is greater, if true return file if false it polls again. """hook=self._get_async_hook()exc=Noneifisinstance(self.newer_than,str):self.newer_than=parse_date(self.newer_than)_newer_than=convert_to_utc(self.newer_than)ifself.newer_thanelseNonewhileTrue:try:ifself.file_pattern:files_returned_by_hook=awaithook.get_files_and_attrs_by_pattern(path=self.path,fnmatch_pattern=self.file_pattern)files_sensed=[]forfileinfiles_returned_by_hook:if_newer_than:iffile.attrs.mtimeisNone:continuemod_time=datetime.fromtimestamp(float(file.attrs.mtime)).strftime("%Y%m%d%H%M%S")mod_time_utc=convert_to_utc(datetime.strptime(mod_time,"%Y%m%d%H%M%S"))if_newer_than<=mod_time_utc:files_sensed.append(file.filename)else:files_sensed.append(file.filename)iffiles_sensed:yieldTriggerEvent({"status":"success","message":f"Sensed {len(files_sensed)} files: {files_sensed}",})returnelse:mod_time=awaithook.get_mod_time(self.path)if_newer_than:mod_time_utc=convert_to_utc(datetime.strptime(mod_time,"%Y%m%d%H%M%S"))if_newer_than<=mod_time_utc:yieldTriggerEvent({"status":"success","message":f"Sensed file: {self.path}"})returnelse:yieldTriggerEvent({"status":"success","message":f"Sensed file: {self.path}"})returnawaitasyncio.sleep(self.poke_interval)exceptAirflowException:awaitasyncio.sleep(self.poke_interval)exceptFileNotFoundError:awaitasyncio.sleep(self.poke_interval)exceptExceptionase:exc=e# Break loop to avoid infinite retries on terminal failurebreakyieldTriggerEvent({"status":"error","message":str(exc)})