@@ -38,44 +38,52 @@ def unique(seq, keepstr=True):
38
38
seen = []
39
39
return t (c for c in seq if not (c in seen or seen .append (c )))
40
40
41
+ #note: this exception seems unused
41
42
class ResponseError ( Tier0Error ):
42
43
43
- def __init__ ( self , curl , response , proxy , timeout ):
44
+ def __init__ ( self , curl , response , proxy , timeout , maxTime ):
44
45
super ( ResponseError , self ).__init__ ( response )
45
46
self .args += ( curl , proxy )
46
47
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 } ".'
55
60
else :
56
- errStr += """\n Full response: \" %s\" .""" % ( self .args [0 ], )
61
+ errStr += f'\n Full response: "{ self .args [0 ]} ".'
62
+
57
63
return errStr
58
64
59
65
#TODO: Add exceptions for each category of HTTP error codes
60
66
#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 )
64
70
65
71
class Tier0Handler ( object ):
66
72
67
- def __init__ ( self , uri , timeOut , retries , retryPeriod , proxy , debug ):
73
+ def __init__ ( self , uri , timeOut , maxTime , retries , retryPeriod , proxy , debug ):
68
74
"""
69
75
Parameters:
70
76
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];
72
79
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] ;
74
81
proxy: HTTP proxy for accessing Tier0DataSvc HTTPS calls;
75
82
debug: if set to True, enables debug information.
76
83
"""
77
84
self ._uri = uri
78
85
self ._timeOut = timeOut
86
+ self ._maxTime = maxTime
79
87
self ._retries = retries
80
88
self ._retryPeriod = retryPeriod
81
89
self ._proxy = proxy
@@ -98,7 +106,9 @@ def _getCerts( self ) -> str:
98
106
if cert_path :
99
107
certs += f' --cert { cert_path } '
100
108
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)" )
102
112
if key_path :
103
113
certs += f' --key { key_path } '
104
114
return certs
@@ -110,9 +120,10 @@ def _curlQueryTier0( self, url:str, force_debug:bool = False, force_cert:bool =
110
120
111
121
proxy = f"--proxy { self ._proxy } " if self ._proxy else ""
112
122
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 } '
116
127
117
128
# time the curl to understand if re-tries have been carried out
118
129
start = time .time ()
@@ -140,7 +151,7 @@ def _queryTier0DataSvc( self, url ):
140
151
if self ._proxy :
141
152
logging .info ("before assumed proxy provides authentication, now trying with both proxy and certificate" )
142
153
143
- time .sleep (10 )
154
+ time .sleep (self . _retryPeriod )
144
155
retcode , stdoutdata , stderrdata , query_time = self ._curlQueryTier0 (url , force_debug = True , force_cert = True )
145
156
if retcode != 0 :
146
157
msg = "looks like curl returned an error for the second time: retcode=%s" % (retcode ,)
@@ -164,7 +175,8 @@ def getFirstSafeRun( self ):
164
175
firstConditionSafeRunAPI = "firstconditionsaferun"
165
176
safeRunDict = self ._queryTier0DataSvc ( os .path .join ( self ._uri , firstConditionSafeRunAPI ) )
166
177
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 ), )
168
180
if self ._proxy :
169
181
errStr += """ using proxy \" %s\" .""" % ( str ( self ._proxy ), )
170
182
raise Tier0Error ( errStr )
@@ -179,19 +191,20 @@ def getGlobalTag( self, config ):
179
191
Raises if connection error, bad response, timeout after retries occur, or if no Global Tags are available.
180
192
"""
181
193
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 ] ))
183
195
try :
184
196
recentGT = gtnames [- 1 ]
185
197
return recentGT
186
198
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 ) )
188
201
if self ._proxy :
189
202
errStr += """ using proxy \" %s\" .""" % ( str ( self ._proxy ), )
190
203
raise Tier0Error ( errStr )
191
204
192
205
193
206
def test ( url ):
194
- t0 = Tier0Handler ( url , 1 , 1 , 1 , None , debug = False )
207
+ t0 = Tier0Handler ( url , 1 , 5 , 1 , 10 , None , debug = False )
195
208
196
209
print (' fcsr = %s (%s)' % (t0 .getFirstSafeRun (), type (t0 .getFirstSafeRun ()) ))
197
210
print (' reco_config = %s' % t0 .getGlobalTag ('reco_config' ))
@@ -201,4 +214,3 @@ def test( url ):
201
214
202
215
if __name__ == '__main__' :
203
216
test ( tier0Url )
204
-
0 commit comments