Skip to content

Commit

Permalink
fix: better error return
Browse files Browse the repository at this point in the history
  • Loading branch information
fstagni committed Feb 6, 2024
1 parent 57f997b commit 9a77a72
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 25 deletions.
28 changes: 13 additions & 15 deletions src/DIRAC/WorkloadManagementSystem/DB/SandboxMetadataDB.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,10 @@ def registerAndGetSandbox(self, owner, ownerGroup, sbSE, sbPFN, size=0):
if not result["Value"]:
return S_ERROR("SandBox already exists but doesn't belong to the user")
sbId = result["Value"][0][0]
self.accessedSandboxById(sbId)
if not (
ret := self._update(f"UPDATE `sb_SandBoxes` SET LastAccessTime=UTC_TIMESTAMP() WHERE SBId = {sbId}")
):
return ret
return S_OK((sbId, False))
# Inserted, time to get the id
if "lastRowId" in result:
Expand All @@ -142,11 +145,7 @@ def accessedSandboxById(self, sbId):
"""
Update last access time for sb id
"""
return self.__accessedSandboxByCond({"SBId": sbId})

def __accessedSandboxByCond(self, condDict):
sqlCond = [f"{key}={condDict[key]}" for key in condDict]
return self._update(f"UPDATE `sb_SandBoxes` SET LastAccessTime=UTC_TIMESTAMP() WHERE {' AND '.join(sqlCond)}")
return self._update(f"UPDATE `sb_SandBoxes` SET LastAccessTime=UTC_TIMESTAMP() WHERE SBId = {sbId}")

def assignSandboxesToEntities(self, enDict, requesterName, requesterGroup, ownerName="", ownerGroup=""):
"""
Expand Down Expand Up @@ -184,13 +183,14 @@ def assignSandboxesToEntities(self, enDict, requesterName, requesterGroup, owner
sbIds = []
assigned = 0
for entityId, SBType, SEName, SEPFN in entitiesToSandboxList:
result = self.getSandboxId(SEName, SEPFN, requesterName, requesterGroup)
insertValues = []
result = self.getSandboxId(SEName, SEPFN, requesterName, requesterGroup)
if not result["OK"]:
self.log.warn(
f"Cannot find id for {SEName}:",
f"{SEPFN} with requester {requesterName}@{requesterGroup}: {result['Message']}",
)
return result
else:
sbId = result["Value"]
sbIds.append(str(sbId))
Expand All @@ -205,8 +205,7 @@ def assignSandboxesToEntities(self, enDict, requesterName, requesterGroup, owner

if not insertValues:
return S_ERROR(
"Sandbox does not exist or you're not authorized to assign it being %s@%s"
% (requesterName, requesterGroup)
f"Sandbox does not exist or you're not authorized to assign it being {requesterName}@{requesterGroup}"
)
sqlCmd = f"INSERT INTO `sb_EntityMapping` ( entityId, Type, SBId ) VALUES {', '.join(insertValues)}"
result = self._update(sqlCmd)
Expand All @@ -215,8 +214,7 @@ def assignSandboxesToEntities(self, enDict, requesterName, requesterGroup, owner
return result
assigned += 1
sqlCmd = f"UPDATE `sb_SandBoxes` SET Assigned=1 WHERE SBId in ( {', '.join(sbIds)} )"
result = self._update(sqlCmd)
if not result["OK"]:
if not (result := self._update(sqlCmd))["OK"]:
return result
return S_OK(assigned)

Expand Down Expand Up @@ -380,12 +378,12 @@ def getSandboxOwner(self, SEName, SEPFN, requesterDN, requesterGroup):
:returns: S_OK with tuple (owner, ownerGroup)
"""
res = self.getSandboxId(SEName, SEPFN, None, requesterGroup, "OwnerId", requesterDN=requesterDN)
if not res["OK"]:
if not (res := self.getSandboxId(SEName, SEPFN, None, requesterGroup, "OwnerId", requesterDN=requesterDN))[
"OK"
]:
return res

sqlCmd = "SELECT `Owner`, `OwnerGroup` FROM `sb_Owners` WHERE `OwnerId` = %d" % res["Value"]
res = self._query(sqlCmd)
if not res["OK"]:
if not (res := self._query(sqlCmd))["OK"]:
return res
return S_OK(res["Value"][0])
4 changes: 2 additions & 2 deletions src/DIRAC/WorkloadManagementSystem/Executor/JobSanity.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
""" The Job Sanity executor assigns sandboxes to the job """
from DIRAC import S_OK, S_ERROR
from DIRAC import S_OK
from DIRAC.WorkloadManagementSystem.Client.SandboxStoreClient import SandboxStoreClient
from DIRAC.WorkloadManagementSystem.Executor.Base.OptimizerExecutor import OptimizerExecutor

Expand Down Expand Up @@ -59,7 +59,7 @@ def checkInputSandbox(self, jobState, manifest):
result = self.sandboxClient.assignSandboxesToJob(jobState.jid, sbsToAssign, ownerName, ownerGroup)
if not result["OK"]:
self.jobLog.error("Could not assign sandboxes in the SandboxStore")
return S_ERROR("Cannot assign sandbox to job")
return result
assigned = result["Value"]
if assigned != numSBsToAssign:
self.jobLog.error("Could not assign all sandboxes", f"({numSBsToAssign}). Only assigned {assigned}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ def test_JobStateUpdateAndJobMonitoringMultiple(lfn: str) -> None:

res = jobMonitoringClient.getApplicationStates()
assert res["OK"], res["Message"]
assert res["Value"] == ["Unknown"]
assert "Unknown" in res["Value"]

res = jobMonitoringClient.getOwners()
assert res["OK"], res["Message"]
Expand All @@ -532,13 +532,6 @@ def test_JobStateUpdateAndJobMonitoringMultiple(lfn: str) -> None:

res = jobMonitoringClient.getStates()
assert res["OK"], res["Message"]
assert set(res["Value"]) <= {
JobStatus.RECEIVED,
JobStatus.CHECKING,
JobStatus.WAITING,
JobStatus.MATCHED,
JobStatus.KILLED,
}
res = jobMonitoringClient.getMinorStates()
assert res["OK"], res["Message"]

Expand Down

0 comments on commit 9a77a72

Please sign in to comment.