Skip to content

Commit b45e28e

Browse files
authored
Merge pull request #5772 from fstagni/cherry-pick-2-734faf191-integration
[sweep:integration] DIRAC APIs check if a JobState transition is allowed
2 parents fc76e1d + cc39792 commit b45e28e

File tree

3 files changed

+47
-3
lines changed

3 files changed

+47
-3
lines changed

src/DIRAC/Interfaces/API/Dirac.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1675,6 +1675,10 @@ def deleteJob(self, jobID):
16751675
return ret
16761676
jobID = ret["Value"]
16771677

1678+
res = JobStatus.checkJobStateTransition(jobID, JobStatus.DELETED)
1679+
if not res["OK"]:
1680+
return res
1681+
16781682
result = WMSClient(useCertificates=self.useCertificates).deleteJob(jobID)
16791683
if result["OK"]:
16801684
if self.jobRepo:
@@ -1705,6 +1709,10 @@ def rescheduleJob(self, jobID):
17051709
return ret
17061710
jobID = ret["Value"]
17071711

1712+
res = JobStatus.checkJobStateTransition(jobID, JobStatus.RESCHEDULED)
1713+
if not res["OK"]:
1714+
return res
1715+
17081716
result = WMSClient(useCertificates=self.useCertificates).rescheduleJob(jobID)
17091717
if result["OK"]:
17101718
if self.jobRepo:
@@ -1721,11 +1729,11 @@ def killJob(self, jobID):
17211729
17221730
Example Usage:
17231731
1724-
>>> print dirac.killJob(12345)
1732+
>>> print(dirac.killJob(12345))
17251733
{'OK': True, 'Value': [12345]}
17261734
17271735
:param jobID: JobID
1728-
:type jobID: int, str or python:list
1736+
:type jobID: int, str
17291737
:returns: S_OK,S_ERROR
17301738
17311739
"""
@@ -1734,6 +1742,10 @@ def killJob(self, jobID):
17341742
return ret
17351743
jobID = ret["Value"]
17361744

1745+
res = JobStatus.checkJobStateTransition(jobID, JobStatus.KILLED)
1746+
if not res["OK"]:
1747+
return res
1748+
17371749
result = WMSClient(useCertificates=self.useCertificates).killJob(jobID)
17381750
if result["OK"]:
17391751
if self.jobRepo:

src/DIRAC/WorkloadManagementSystem/Client/JobStatus.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
"""
22
This module contains constants and lists for the possible job states.
33
"""
4+
5+
from DIRAC import gLogger, S_OK, S_ERROR
46
from DIRAC.Core.Utilities.StateMachine import State, StateMachine
57

68

@@ -89,3 +91,33 @@ def __init__(self, state):
8991
RECEIVED: State(1, [CHECKING, WAITING, FAILED, DELETED], defState=RECEIVED),
9092
SUBMITTING: State(0, [RECEIVED, CHECKING, DELETED], defState=SUBMITTING), # initial state
9193
}
94+
95+
96+
def checkJobStateTransition(jobID, candidateState, currentStatus=None, jobMonitoringClient=None):
97+
"""Utility to check if a job state transition is allowed"""
98+
if not currentStatus:
99+
if not jobMonitoringClient:
100+
from DIRAC.WorkloadManagementSystem.Client.JobMonitoringClient import JobMonitoringClient
101+
102+
jobMonitoringClient = JobMonitoringClient()
103+
104+
res = jobMonitoringClient.getJobsStatus(jobID)
105+
if not res["OK"]:
106+
return res
107+
try:
108+
currentStatus = res["Value"][jobID]["Status"]
109+
except KeyError:
110+
return S_ERROR("Job does not exist")
111+
112+
res = JobsStateMachine(currentStatus).getNextState(candidateState)
113+
if not res["OK"]:
114+
return res
115+
116+
# If the JobsStateMachine does not accept the candidate, return an ERROR
117+
if candidateState != res["Value"]:
118+
gLogger.error(
119+
"Job Status Error",
120+
"%s can't move from %s to %s" % (jobID, currentStatus, candidateState),
121+
)
122+
return S_ERROR("Job state transition not allowed")
123+
return S_OK()

src/DIRAC/WorkloadManagementSystem/DB/JobDB.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,7 @@ def setJobsMajorStatus(self, jIDList, candidateStatus, force=False):
650650
return res
651651
nextState = res["Value"]
652652

653-
# If the JobsStateMachine does not accept the candidate, add it to separate dictionary
653+
# The JobsStateMachine might force a different status
654654
if candidateStatus != nextState:
655655
self.log.error(
656656
"Job Status Error",

0 commit comments

Comments
 (0)