Skip to content

Commit db93b46

Browse files
committed
fix: make TimeLeft backward compatible
1 parent 281473a commit db93b46

File tree

3 files changed

+59
-4
lines changed

3 files changed

+59
-4
lines changed

src/DIRAC/Resources/Computing/BatchSystems/TimeLeft/MJFResourceUsage.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ def __init__(self, jobID, parameters):
1919
"""Standard constructor"""
2020
super().__init__("MJF", jobID, parameters)
2121

22-
self.queue = os.environ.get("QUEUE")
23-
2422
self.log.verbose(f"jobID={self.jobID}, queue={self.queue}")
2523
self.startTime = time.time()
2624

src/DIRAC/Resources/Computing/BatchSystems/TimeLeft/TimeLeft.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
With this information the utility can calculate in normalized units the
1010
CPU time remaining for a given slot.
1111
"""
12+
import os
1213
import shlex
1314

1415
import DIRAC
@@ -136,6 +137,47 @@ def _getBatchSystemPlugin(self):
136137
jobID = batchSystemInfo.get("JobID")
137138
parameters = batchSystemInfo.get("Parameters")
138139

140+
###########################################################################################
141+
# TODO: remove the following block in v9.0
142+
# This is a temporary fix for the case where the batch system is not set in the configuration
143+
if not type:
144+
self.log.warn(
145+
"Batch system info not set in local configuration: trying to guess from environment variables."
146+
)
147+
self.log.warn("Consider updating your pilot version before switching to v9.0.")
148+
batchSystemInfo = {
149+
"LSF": {
150+
"JobID": "LSB_JOBID",
151+
"Parameters": {
152+
"BinaryPath": "LSB_BINDIR",
153+
"Host": "LSB_HOSTS",
154+
"InfoPath": "LSB_ENVDIR",
155+
"Queue": "LSB_QUEUE",
156+
},
157+
},
158+
"PBS": {"JobID": "PBS_JOBID", "Parameters": {"BinaryPath": "PBS_O_PATH", "Queue": "PBS_O_QUEUE"}},
159+
"SGE": {"JobID": "SGE_TASK_ID", "Parameters": {"BinaryPath": "SGE_BINARY_PATH", "Queue": "QUEUE"}},
160+
"SLURM": {"JobID": "SLURM_JOB_ID", "Parameters": {}},
161+
"HTCondor": {"JobID": "HTCONDOR_JOBID", "Parameters": {"InfoPath": "_CONDOR_JOB_AD"}},
162+
}
163+
type = None
164+
for batchSystem, attributes in batchSystemInfo.items():
165+
if attributes["JobID"] in os.environ:
166+
type = batchSystem
167+
jobID = os.environ[attributes["JobID"]]
168+
parameters = {}
169+
for parameterName, parameterVariable in attributes["Parameters"].items():
170+
parameters[parameterName] = os.environ.get(parameterVariable)
171+
break
172+
173+
if not type and "MACHINEFEATURES" in os.environ and "JOBFEATURES" in os.environ:
174+
# Only use MJF if legacy batch system information not available for now
175+
type = "MJF"
176+
jobID = os.environ.get("JOB_ID")
177+
parameters = {"Queue": os.environ.get("QUEUE")}
178+
179+
###########################################################################################
180+
139181
if not type or type == "Unknown":
140182
self.log.warn(f"Batch system type for site {DIRAC.siteName()} is not currently supported")
141183
return S_ERROR(DErrno.ERESUNK, "Current batch system is not supported")

src/DIRAC/Resources/Computing/BatchSystems/TimeLeft/test/Test_TimeLeft.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
""" Test TimeLeft utility
22
"""
3-
import pytest
4-
53
from DIRAC import S_OK
64
from DIRAC.Resources.Computing.BatchSystems.TimeLeft.TimeLeft import TimeLeft
75

@@ -27,6 +25,23 @@ def test_batchSystemNotDefined(mocker):
2725
assert "Current batch system is not supported" in res["Message"]
2826

2927

28+
def test_batchSystemNotDefinedInConfigButInEnvironmentVariables(mocker, monkeypatch):
29+
"""Test batch system not defined but present in environment variables (should fail from v9.0)"""
30+
mocker.patch(
31+
"DIRAC.Resources.Computing.BatchSystems.TimeLeft.HTCondorResourceUsage.runCommand",
32+
return_value=S_OK("9000 800"),
33+
)
34+
mocker.patch("DIRAC.gConfig.getSections", return_value={})
35+
monkeypatch.setenv("HTCONDOR_JOBID", "12345.0")
36+
monkeypatch.setenv("_CONDOR_JOB_AD", "/path/to/config")
37+
38+
tl = TimeLeft()
39+
tl.cpuPower = 10
40+
res = tl.getTimeLeft()
41+
assert res["OK"]
42+
assert res["Value"] == 82000
43+
44+
3045
def test_getScaledCPU(mocker):
3146
"""Test getScaledCPU()"""
3247
mocker.patch(

0 commit comments

Comments
 (0)