@@ -38,44 +38,52 @@ def unique(seq, keepstr=True):
3838 seen = []
3939 return t (c for c in seq if not (c in seen or seen .append (c )))
4040
41+ #note: this exception seems unused
4142class ResponseError ( Tier0Error ):
4243
43- def __init__ ( self , curl , response , proxy , timeout ):
44+ def __init__ ( self , curl , response , proxy , timeout , maxTime ):
4445 super ( ResponseError , self ).__init__ ( response )
4546 self .args += ( curl , proxy )
4647 self .timeout = timeout
47-
48- def __str__ ( self ):
49- errStr = """Wrong response for curl connection to Tier0DataSvc from URL \" %s\" """ % ( self .args [1 ].getinfo ( self .args [1 ].EFFECTIVE_URL ), )
50- if self .args [ - 1 ]:
51- errStr += """ using proxy \" %s\" """ % ( str ( self .args [ - 1 ] ), )
52- errStr += """ with timeout \" %d\" with error code \" %d\" .""" % ( self .timeout , self .args [1 ].getinfo ( self .args [1 ].RESPONSE_CODE ) )
53- if self .args [0 ].find ( '<p>' ) != - 1 :
54- errStr += """\n Full response: \" %s\" .""" % ( self .args [0 ].partition ('<p>' )[- 1 ].rpartition ('</p>' )[0 ], )
48+ self .maxTime = maxTime
49+
50+ def __str__ (self ):
51+ errStr = f'Wrong response for curl connection to Tier0DataSvc' \
52+ f' from URL "{ self .args [1 ].getinfo (self .args [1 ].EFFECTIVE_URL )} "'
53+ if self .args [- 1 ]:
54+ errStr += f' using proxy "{ str (self .args [- 1 ])} "'
55+ errStr += f' with connection-timeout "{ self .timeout } ", max-time "{ self .maxtime } "' \
56+ f' with error code "{ self .args [1 ].getinfo (self .args [1 ].RESPONSE_CODE )} ".'
57+ if '<p>' in self .args [0 ]:
58+ full_response = self .args [0 ].partition ('<p>' )[- 1 ].rpartition ('</p>' )[0 ]
59+ errStr += f'\n Full response: "{ full_response } ".'
5560 else :
56- errStr += """\n Full response: \" %s\" .""" % ( self .args [0 ], )
61+ errStr += f'\n Full response: "{ self .args [0 ]} ".'
62+
5763 return errStr
5864
5965#TODO: Add exceptions for each category of HTTP error codes
6066#TODO: check response code and raise corresponding exceptions
61-
62- def _raise_http_error ( curl , response , proxy , timeout ):
63- raise ResponseError ( curl , response , proxy , timeout )
67+ #note: this function seems to be unused
68+ def _raise_http_error ( curl , response , proxy , timeout , maxTime ):
69+ raise ResponseError ( curl , response , proxy , timeout , maxTime )
6470
6571class Tier0Handler ( object ):
6672
67- def __init__ ( self , uri , timeOut , retries , retryPeriod , proxy , debug ):
73+ def __init__ ( self , uri , timeOut , maxTime , retries , retryPeriod , proxy , debug ):
6874 """
6975 Parameters:
7076 uri: Tier0DataSvc URI;
71- timeOut: time out for Tier0DataSvc HTTPS calls;
77+ timeOut: time out for connection of Tier0DataSvc HTTPS calls [seconds];
78+ maxTime: maximum time for Tier0DataSvc HTTPS calls (including data transfer) [seconds];
7279 retries: maximum retries for Tier0DataSvc HTTPS calls;
73- retryPeriod: sleep time between two Tier0DataSvc HTTPS calls;
80+ retryPeriod: sleep time between two Tier0DataSvc HTTPS calls [seconds] ;
7481 proxy: HTTP proxy for accessing Tier0DataSvc HTTPS calls;
7582 debug: if set to True, enables debug information.
7683 """
7784 self ._uri = uri
7885 self ._timeOut = timeOut
86+ self ._maxTime = maxTime
7987 self ._retries = retries
8088 self ._retryPeriod = retryPeriod
8189 self ._proxy = proxy
@@ -98,7 +106,9 @@ def _getCerts( self ) -> str:
98106 if cert_path :
99107 certs += f' --cert { cert_path } '
100108 else :
101- logging .warning ("No certificate provided for Tier0 access" )
109+ logging .warning ("No certificate provided for Tier0 access, use X509_USER_CERT and"
110+ " optionally X509_USER_KEY env variables to specify the path to the cert"
111+ " (and the key unless included in the cert file)" )
102112 if key_path :
103113 certs += f' --key { key_path } '
104114 return certs
@@ -110,9 +120,10 @@ def _curlQueryTier0( self, url:str, force_debug:bool = False, force_cert:bool =
110120
111121 proxy = f"--proxy { self ._proxy } " if self ._proxy else ""
112122 certs = self ._getCerts () if not self ._proxy or force_cert else ""
113-
114- cmd = '/usr/bin/curl -k -L --user-agent "%s" %s --connect-timeout %i --retry %i %s %s %s' \
115- % (userAgent , proxy , self ._timeOut , self ._retries , debug , url , certs )
123+
124+ cmd = f'/usr/bin/curl -k -L --user-agent "{ userAgent } " { proxy } ' \
125+ f' --connect-timeout { self ._timeOut } --max-time { self ._maxTime } --retry { self ._retries } ' \
126+ f' { debug } { url } { certs } '
116127
117128 # time the curl to understand if re-tries have been carried out
118129 start = time .time ()
@@ -140,7 +151,7 @@ def _queryTier0DataSvc( self, url ):
140151 if self ._proxy :
141152 logging .info ("before assumed proxy provides authentication, now trying with both proxy and certificate" )
142153
143- time .sleep (10 )
154+ time .sleep (self . _retryPeriod )
144155 retcode , stdoutdata , stderrdata , query_time = self ._curlQueryTier0 (url , force_debug = True , force_cert = True )
145156 if retcode != 0 :
146157 msg = "looks like curl returned an error for the second time: retcode=%s" % (retcode ,)
@@ -164,7 +175,8 @@ def getFirstSafeRun( self ):
164175 firstConditionSafeRunAPI = "firstconditionsaferun"
165176 safeRunDict = self ._queryTier0DataSvc ( os .path .join ( self ._uri , firstConditionSafeRunAPI ) )
166177 if safeRunDict is None :
167- errStr = """First condition safe run is not available in Tier0DataSvc from URL \" %s\" """ % ( os .path .join ( self ._uri , firstConditionSafeRunAPI ), )
178+ errStr = """First condition safe run is not available in Tier0DataSvc from URL \" %s\" """ \
179+ % ( os .path .join ( self ._uri , firstConditionSafeRunAPI ), )
168180 if self ._proxy :
169181 errStr += """ using proxy \" %s\" .""" % ( str ( self ._proxy ), )
170182 raise Tier0Error ( errStr )
@@ -179,19 +191,20 @@ def getGlobalTag( self, config ):
179191 Raises if connection error, bad response, timeout after retries occur, or if no Global Tags are available.
180192 """
181193 data = self ._queryTier0DataSvc ( os .path .join ( self ._uri , config ) )
182- gtnames = sorted (unique ( [ str ( di [ 'global_tag' ] ) for di in data ['result' ] if di [ 'global_tag' ] is not None ] ))
194+ gtnames = sorted (unique ( [ str ( di ['global_tag' ] ) for di in data ['result' ] if di ['global_tag' ] is not None ] ))
183195 try :
184196 recentGT = gtnames [- 1 ]
185197 return recentGT
186198 except IndexError :
187- errStr = """No Global Tags for \" %s\" are available in Tier0DataSvc from URL \" %s\" """ % ( config , os .path .join ( self ._uri , config ) )
199+ errStr = """No Global Tags for \" %s\" are available in Tier0DataSvc from URL \" %s\" """ \
200+ % ( config , os .path .join ( self ._uri , config ) )
188201 if self ._proxy :
189202 errStr += """ using proxy \" %s\" .""" % ( str ( self ._proxy ), )
190203 raise Tier0Error ( errStr )
191204
192205
193206def test ( url ):
194- t0 = Tier0Handler ( url , 1 , 1 , 1 , None , debug = False )
207+ t0 = Tier0Handler ( url , 1 , 5 , 1 , 10 , None , debug = False )
195208
196209 print (' fcsr = %s (%s)' % (t0 .getFirstSafeRun (), type (t0 .getFirstSafeRun ()) ))
197210 print (' reco_config = %s' % t0 .getGlobalTag ('reco_config' ))
@@ -201,4 +214,3 @@ def test( url ):
201214
202215if __name__ == '__main__' :
203216 test ( tier0Url )
204-
0 commit comments