1111import urllib .parse
1212from concurrent .futures import ThreadPoolExecutor
1313from pathlib import Path
14- from time import sleep
14+
15+ import socket
16+ from time import sleep , time
17+ socket .setdefaulttimeout (30 )
1518
1619########################################################################################
1720# Call this class with three arguments: trace enabled, name to store logs, config json #
2629 level = log .DEBUG ,
2730 handlers = [
2831 log .FileHandler (".command.init." + sys .argv [2 ] + ".log" ),
29- log .StreamHandler ()
32+ log .StreamHandler (sys . stdout )
3033 ]
3134)
3235trace = {}
@@ -146,6 +149,10 @@ def callback(data):
146149 delta = (end - start )
147150 log .info ("Speed: %.3f Mb/s" , sizeInMB / delta )
148151 return sizeInMB , delta
152+ except socket .timeout :
153+ errors += 1
154+ log .warning ("Timeout or EOFError while downloading %s" , filename )
155+ return None
149156 except ftplib .error_perm as err :
150157 errors += 1
151158 if str (err ) == "550 Failed to open file." :
@@ -174,6 +181,7 @@ def download(node, currentIP, files, dns, execution, syncFile, speed):
174181 global CLOSE
175182 sizeInMB = 0
176183 downloadTime = 0
184+ attempt = 0
177185 while not CLOSE and len (files ) > 0 :
178186 if ftp is None :
179187 ftp = getFTP (node , currentIP , dns , execution , syncFile )
@@ -183,13 +191,18 @@ def download(node, currentIP, files, dns, execution, syncFile, speed):
183191 result = downloadFile (ftp , filename , size , index , node , syncFile , speed )
184192 if result is None :
185193 ftp = None
194+ attempt += 1
195+ if attempt > 3 :
196+ log .error ("Too many attempts to download %s" , filename )
197+ closeWithWarning (42 , syncFile )
186198 continue
199+ attempt = 0
187200 sizeInMB += result [0 ]
188201 downloadTime += result [1 ]
189202 files .pop (0 )
190203 syncFile .write ("F-" + filename + '\n ' )
191204 closeFTP (ftp )
192- return node , sizeInMB / downloadTime
205+ return node , sizeInMB / downloadTime if downloadTime > 0 else 0
193206
194207
195208def waitForFiles (syncFilePath , files , startTime ):
@@ -360,3 +373,4 @@ def run():
360373
361374if __name__ == '__main__' :
362375 run ()
376+ sys .exit (EXIT )
0 commit comments