Guest User

Untitled

a guest
Jun 18th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.40 KB | None | 0 0
  1.         POLLING_TIMEOUT = 3600 # 1 hour
  2.         POLLING_INTERVAL = 180 # 3 minutes
  3.  
  4.         def setInterval(func, tyme, on_success):
  5.             """
  6.            Run [func] every [tyme] seconds.. invoking [on_success] if it returns True.
  7.            """
  8.             t0 = time()
  9.             while True:
  10.                 t1 = time()
  11.                 if t1 - t0 >= POLLING_TIMEOUT:
  12.                     raise PollingTimeoutError
  13.                 success = func()
  14.                 if success:
  15.                     logger.info('Target Acquired!')
  16.                     break
  17.                 logger.info('Waiting for {} seconds before retry!'.format(tyme))
  18.                 sleep(tyme)
  19.  
  20.             on_success()
  21.  
  22.         def poll_sftp_server():
  23.             target_file = 'xyz.txt'
  24.  
  25.             logger.info('Connecting to SFTP...')
  26.             with pysftp.Connection(**settings.conn_details) as sftp:
  27.                 with sftp.cd('/SomeFolder'):
  28.                     list_of_remote_files = sftp.listdir()
  29.                     logger.info('Files found on the server: {}'.format(list_of_remote_files))
  30.                     is_file_found = True if target_file in list_of_remote_files else False
  31.             logger.info('Disconnecting from SFTP...')
  32.             return is_file_found
  33.  
  34.         def trigger_func():
  35.             some_func()
  36.  
  37.         setInterval(poll_sftp_server, POLLING_INTERVAL, on_success=trigger_func)
Advertisement
Add Comment
Please, Sign In to add comment