20
20
from DIRAC .Core .Utilities .ClassAd .ClassAdLight import ClassAd
21
21
from DIRAC .Core .Utilities .Decorators import deprecated
22
22
from DIRAC .Core .Utilities .DErrno import EWMSJMAN , EWMSSUBM , cmpError
23
- from DIRAC .Core .Utilities .ReturnValues import S_ERROR , S_OK
23
+ from DIRAC .Core .Utilities .ReturnValues import S_ERROR , S_OK , convertToReturnValue , returnValueOrRaise , SErrorException
24
24
from DIRAC .FrameworkSystem .Client .Logger import contextLogger
25
25
from DIRAC .ResourceStatusSystem .Client .SiteStatus import SiteStatus
26
26
from DIRAC .WorkloadManagementSystem .Client import JobMinorStatus , JobStatus
@@ -106,18 +106,7 @@ def getJobParameters(self, jobID, paramList=None):
106
106
Returns a dictionary with the Job Parameters.
107
107
If parameterList is empty - all the parameters are returned.
108
108
"""
109
-
110
- if isinstance (jobID , (str , int )):
111
- jobID = [jobID ]
112
-
113
- jobIDList = []
114
- for jID in jobID :
115
- ret = self ._escapeString (str (jID ))
116
- if not ret ["OK" ]:
117
- return ret
118
- jobIDList .append (ret ["Value" ])
119
-
120
- # self.log.debug('JobDB.getParameters: Getting Parameters for jobs %s' % ','.join(jobIDList))
109
+ jobIDList = [jobID ] if isinstance (jobID , (str , int )) else jobID
121
110
122
111
resultDict = {}
123
112
if paramList :
@@ -130,7 +119,7 @@ def getJobParameters(self, jobID, paramList=None):
130
119
return ret
131
120
paramNameList .append (ret ["Value" ])
132
121
cmd = "SELECT JobID, Name, Value FROM JobParameters WHERE JobID IN ({}) AND Name IN ({})" .format (
133
- "," .join (jobIDList ),
122
+ "," .join (str ( int ( j )) for j in jobIDList ),
134
123
"," .join (paramNameList ),
135
124
)
136
125
result = self ._query (cmd )
@@ -207,13 +196,13 @@ def getAtticJobParameters(self, jobID, paramList=None, rescheduleCounter=-1):
207
196
return S_ERROR ("JobDB.getAtticJobParameters: failed to retrieve parameters" )
208
197
209
198
#############################################################################
199
+ @convertToReturnValue
210
200
def getJobsAttributes (self , jobIDs , attrList = None ):
211
201
"""Get all Job(s) Attributes for a given list of jobIDs.
212
202
Return a dictionary with all Job Attributes as value pairs
213
203
"""
214
-
215
204
if not jobIDs :
216
- return S_OK ({})
205
+ return {}
217
206
218
207
# If no list of attributes is given, return all attributes
219
208
if not attrList :
@@ -229,28 +218,29 @@ def getJobsAttributes(self, jobIDs, attrList=None):
229
218
230
219
attrNameListS = []
231
220
for x in attrList :
232
- ret = self ._escapeString (x )
233
- if not ret ["OK" ]:
234
- return ret
235
- x = "`" + ret ["Value" ][1 :- 1 ] + "`"
221
+ x = "`" + returnValueOrRaise (self ._escapeString (x ))[1 :- 1 ] + "`"
236
222
attrNameListS .append (x )
237
223
attrNames = "JobID," + "," .join (attrNameListS )
238
224
239
- cmd = f"SELECT { attrNames } FROM Jobs WHERE JobID IN ({ ',' .join (str (jobID ) for jobID in jobIDs )} )"
240
- res = self ._query (cmd )
241
- if not res ["OK" ]:
242
- return res
243
- if not res ["Value" ]:
244
- return S_OK ({})
225
+ sqlCmd = "CREATE TEMPORARY TABLE to_select_Jobs (JobID INTEGER NOT NULL, PRIMARY KEY (JobID)) ENGINE=MEMORY;"
226
+ returnValueOrRaise (self ._update (sqlCmd ))
227
+ try :
228
+ sqlCmd = "INSERT INTO to_select_Jobs (JobID) VALUES ( %s )"
229
+ returnValueOrRaise (self ._updatemany (sqlCmd , [(int (j ),) for j in jobIDs ]))
230
+ sqlCmd = f"SELECT { attrNames } FROM Jobs JOIN to_select_Jobs USING (JobID)"
231
+ result = returnValueOrRaise (self ._query (sqlCmd ))
232
+ finally :
233
+ sqlCmd = "DROP TEMPORARY TABLE to_select_Jobs"
234
+ returnValueOrRaise (self ._update (sqlCmd ))
245
235
246
236
attributes = {}
247
- for t_att in res [ "Value" ] :
237
+ for t_att in result :
248
238
jobID = int (t_att [0 ])
249
239
attributes .setdefault (jobID , {})
250
240
for tx , ax in zip (t_att [1 :], attrList ):
251
241
attributes [jobID ].setdefault (ax , tx )
252
242
253
- return S_OK ( attributes )
243
+ return attributes
254
244
255
245
#############################################################################
256
246
def getJobAttributes (self , jobID , attrList = None ):
@@ -527,12 +517,10 @@ def setJobAttributes(self, jobID, attrNames, attrValues, update=False, myDate=No
527
517
if not isinstance (jobID , (list , tuple )):
528
518
jobIDList = [jobID ]
529
519
530
- jIDList = []
531
- for jID in jobIDList :
532
- ret = self ._escapeString (jID )
533
- if not ret ["OK" ]:
534
- return ret
535
- jIDList .append (ret ["Value" ])
520
+ try :
521
+ jIDList = [int (jID ) for jID in jobIDList ]
522
+ except ValueError as e :
523
+ return S_ERROR (f"JobDB.setAttributes: { e } " )
536
524
537
525
if len (attrNames ) != len (attrValues ):
538
526
return S_ERROR ("JobDB.setAttributes: incompatible Argument length" )
@@ -561,7 +549,7 @@ def setJobAttributes(self, jobID, attrNames, attrValues, update=False, myDate=No
561
549
if not attr :
562
550
return S_ERROR ("JobDB.setAttributes: Nothing to do" )
563
551
564
- cmd = f"UPDATE Jobs SET { ', ' .join (attr )} WHERE JobID in ( { ', ' .join (jIDList )} )"
552
+ cmd = f"UPDATE Jobs SET { ', ' .join (attr )} WHERE JobID in ( { ', ' .join (str ( int ( j )) for j in jIDList )} )"
565
553
566
554
if myDate :
567
555
cmd += f" AND LastUpdateTime < { myDate } "
@@ -987,44 +975,42 @@ def __checkAndPrepareJob(self, jobID, classAdJob, classAdReq, owner, ownerGroup,
987
975
return S_OK ()
988
976
989
977
#############################################################################
978
+ @convertToReturnValue
990
979
def removeJobFromDB (self , jobIDs ):
991
980
"""
992
981
Remove jobs from the Job DB and clean up all the job related data in various tables
993
982
"""
994
-
995
- # ret = self._escapeString(jobID)
996
- # if not ret['OK']:
997
- # return ret
998
- # e_jobID = ret['Value']
999
-
1000
983
if not jobIDs :
1001
- return S_OK ()
1002
-
1003
- if not isinstance (jobIDs , list ):
1004
- jobIDList = [jobIDs ]
1005
- else :
1006
- jobIDList = jobIDs
984
+ return None
985
+ jobIDList = jobIDs if isinstance (jobIDs , list ) else [jobIDs ]
1007
986
1008
987
failedTablesList = []
1009
- for table in [
1010
- "InputData" ,
1011
- "JobParameters" ,
1012
- "AtticJobParameters" ,
1013
- "HeartBeatLoggingInfo" ,
1014
- "OptimizerParameters" ,
1015
- "JobCommands" ,
1016
- "Jobs" ,
1017
- "JobJDLs" ,
1018
- ]:
1019
- cmd = f"DELETE FROM { table } WHERE JobID in ({ ',' .join (str (j ) for j in jobIDList )} )"
1020
- result = self ._update (cmd )
1021
- if not result ["OK" ]:
1022
- failedTablesList .append (table )
1023
988
1024
- if failedTablesList :
1025
- return S_ERROR (f"Errors while job removal (tables { ',' .join (failedTablesList )} )" )
989
+ sqlCmd = "CREATE TEMPORARY TABLE to_delete_Jobs (JobID INT(11) UNSIGNED NOT NULL, PRIMARY KEY (JobID)) ENGINE=MEMORY;"
990
+ returnValueOrRaise (self ._update (sqlCmd ))
991
+ try :
992
+ sqlCmd = "INSERT INTO to_delete_Jobs (JobID) VALUES ( %s )"
993
+ returnValueOrRaise (self ._updatemany (sqlCmd , [(j ,) for j in jobIDList ]))
994
+
995
+ for table in [
996
+ "InputData" ,
997
+ "JobParameters" ,
998
+ "AtticJobParameters" ,
999
+ "HeartBeatLoggingInfo" ,
1000
+ "OptimizerParameters" ,
1001
+ "JobCommands" ,
1002
+ "Jobs" ,
1003
+ "JobJDLs" ,
1004
+ ]:
1005
+ sqlCmd = f"DELETE m from `{ table } ` m JOIN to_delete_Jobs t USING (JobID)"
1006
+ if not self ._update (sqlCmd )["OK" ]:
1007
+ failedTablesList .append (table )
1008
+ finally :
1009
+ sqlCmd = "DROP TEMPORARY TABLE to_delete_Jobs"
1010
+ returnValueOrRaise (self ._update (sqlCmd ))
1026
1011
1027
- return S_OK ()
1012
+ if failedTablesList :
1013
+ raise SErrorException (f"Errors while job removal (tables { ',' .join (failedTablesList )} )" )
1028
1014
1029
1015
#############################################################################
1030
1016
def rescheduleJob (self , jobID ):
0 commit comments