1111
1212import logging
1313import sys
14+ import time
1415from pyomo .common .download import FileDownloader , DownloadFactory
1516from pyomo .scripting .pyomo_parser import add_subparser
1617
18+ MAX_RETRIES = 3
19+ RETRY_SLEEP_SECONDS = 5
20+
1721
1822class GroupDownloader (object ):
1923 def __init__ (self ):
@@ -35,43 +39,62 @@ def _call_impl(self, args, unparsed, logger):
3539 results = []
3640 result_fmt = "[%s] %s"
3741 returncode = 0
42+
3843 self .downloader .cacert = args .cacert
3944 self .downloader .insecure = args .insecure
45+
4046 logger .info (
41- "As of February 9, 2023, AMPL GSL can no longer be downloaded \
42- through download-extensions. Visit https://portal.ampl.com/ \
43- to download the AMPL GSL binaries."
47+ "As of February 9, 2023, AMPL GSL can no longer be downloaded "
48+ " through download-extensions. Visit https://portal.ampl.com/ "
49+ " to download the AMPL GSL binaries."
4450 )
51+
4552 for target in DownloadFactory :
46- try :
47- ext = DownloadFactory (target , downloader = self .downloader )
48- if hasattr (ext , 'skip' ) and ext .skip ():
49- result = 'SKIP'
50- elif hasattr (ext , '__call__' ):
51- ext ()
52- result = ' OK '
53- else :
54- # Extension was a simple function and already ran
55- result = ' OK '
56- except SystemExit :
57- _info = sys .exc_info ()
58- _cls = (
59- str (_info [0 ].__name__ if _info [0 ] is not None else "NoneType" )
60- + ": "
61- )
62- logger .error (_cls + str (_info [1 ]))
63- result = 'FAIL'
64- returncode |= 2
65- except :
66- _info = sys .exc_info ()
67- _cls = (
68- str (_info [0 ].__name__ if _info [0 ] is not None else "NoneType" )
69- + ": "
70- )
71- logger .error (_cls + str (_info [1 ]))
72- result = 'FAIL'
73- returncode |= 1
53+ attempt = 0
54+ result = "FAIL"
55+
56+ while attempt < MAX_RETRIES :
57+ try :
58+ ext = DownloadFactory (target , downloader = self .downloader )
59+
60+ if hasattr (ext , "skip" ) and ext .skip ():
61+ result = "SKIP"
62+ elif hasattr (ext , "__call__" ):
63+ ext ()
64+ result = " OK "
65+ else :
66+ result = " OK "
67+
68+ break
69+
70+ except SystemExit :
71+ _info = sys .exc_info ()
72+ _cls = (
73+ f"{ _info [0 ].__name__ if _info [0 ] is not None else 'NoneType' } : "
74+ )
75+ logger .error (_cls + str (_info [1 ]))
76+ if attempt + 1 == MAX_RETRIES :
77+ returncode |= 2
78+ except Exception :
79+ _info = sys .exc_info ()
80+ _cls = (
81+ f"{ _info [0 ].__name__ if _info [0 ] is not None else 'NoneType' } : "
82+ )
83+ logger .error (_cls + str (_info [1 ]))
84+ if attempt + 1 == MAX_RETRIES :
85+ returncode |= 1
86+ finally :
87+ if result != " OK " :
88+ attempt += 1
89+ if attempt < MAX_RETRIES :
90+ logger .info (
91+ f"Retrying download of '{ target } ' "
92+ f"(attempt { attempt + 1 } of { MAX_RETRIES } )"
93+ )
94+ time .sleep (RETRY_SLEEP_SECONDS )
95+
7496 results .append (result_fmt % (result , target ))
97+
7598 logger .info ("Finished downloading Pyomo extensions." )
7699 logger .info (
77100 "The following extensions were downloaded:\n " + "\n " .join (results )
0 commit comments