def waiting_for_file(filepath): retry_count = 0 sleep_time_in_minute = 5 max_age = 12 * 60 * 60 # 12 hour in seconds timeout_hours = 8 alert_hours = 2 remaining_hours = timeout_hours - alert_hours retry_count_before_alert = alert_hours * 60 / sleep_time_in_minute retry_count_before_timeout = timeout_hours * 60 / sleep_time_in_minute await_done_files = get_flag_value_nullable('await_done_files') or 'yes' if await_done_files: while True: is_file_new = ensure_file_not_stale(filepath, max_age) if is_file_new: mtime = os.path.getmtime(filepath) mtime_str = datetime.fromtimestamp( mtime).strftime("%Y-%m-%d %H:%M:%S %Z") print(f"File '{filepath}' arrived at {mtime_str}") send_success_mail(filepath, mtime_str) break if retry_count == retry_count_before_alert: print(f"File '{filepath}' is delayed. Sending alert mail...") send_alert_mail(filepath, remaining_hours) if retry_count == retry_count_before_timeout: print( f"File '{filepath}' is either missing or too old. The script has been waiting for {timeout_hours}. Terminated!") sys.exit(1) retry_count += 1 time.sleep(sleep_time_in_minute * 60)