Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- POLLING_TIMEOUT = 3600 # 1 hour
- POLLING_INTERVAL = 180 # 3 minutes
- def setInterval(func, tyme, on_success):
- """
- Run [func] every [tyme] seconds.. invoking [on_success] if it returns True.
- """
- t0 = time()
- while True:
- t1 = time()
- if t1 - t0 >= POLLING_TIMEOUT:
- raise PollingTimeoutError
- success = func()
- if success:
- logger.info('Target Acquired!')
- break
- logger.info('Waiting for {} seconds before retry!'.format(tyme))
- sleep(tyme)
- on_success()
- def poll_sftp_server():
- target_file = 'xyz.txt'
- logger.info('Connecting to SFTP...')
- with pysftp.Connection(**settings.conn_details) as sftp:
- with sftp.cd('/SomeFolder'):
- list_of_remote_files = sftp.listdir()
- logger.info('Files found on the server: {}'.format(list_of_remote_files))
- is_file_found = True if target_file in list_of_remote_files else False
- logger.info('Disconnecting from SFTP...')
- return is_file_found
- def trigger_func():
- some_func()
- setInterval(poll_sftp_server, POLLING_INTERVAL, on_success=trigger_func)
Advertisement
Add Comment
Please, Sign In to add comment