Makes job working directory creation idempotent

This commit is contained in:
davelopez
2026-07-29 18:51:14 +02:00
parent ddf1768878
commit 45d7e4bf07
2 changed files with 9 additions and 14 deletions
+3 -10
View File
@@ -393,20 +393,13 @@ class JobWorkingDirectory:
def create(self) -> str:
"""Create the working directory and return its path.
Raises ``FileExistsError`` if the per-job directory already exists
(job id collision or leftover from a crashed run).
Idempotent: if the per-job directory already exists (e.g. wrapper
reconstruction for a resubmitted job), it is returned without error.
"""
if custom_path := self._custom_path:
validate_working_directory_path(custom_path)
path = self._per_job_path(custom_path)
try:
os.makedirs(path, exist_ok=False)
except FileExistsError as exc:
raise FileExistsError(
f"Job working directory already exists for job {self._job.id} "
f"at {path!r}; this indicates a job id collision or a leftover "
f"directory from a previous run."
) from exc
os.makedirs(path, exist_ok=True)
return path
self._object_store.create(
self._job,
@@ -68,15 +68,17 @@ class TestJobWorkingDirectoryCustomPath:
# The base must exist too (as parent of the per-job dir).
assert os.path.isdir(base)
def test_create_raises_on_preexisting_per_job_dir(self, tmp_path, object_store):
def test_create_is_idempotent_on_preexisting_per_job_dir(self, tmp_path, object_store):
"""create() returns the same path without error if the directory already exists."""
base = str(tmp_path / "jobs")
job = _make_job(100)
job.working_directory = base
jwd = JobWorkingDirectory(job, object_store)
jwd.create()
with pytest.raises(FileExistsError, match="already exists for job 100"):
jwd.create()
path1 = jwd.create()
path2 = jwd.create()
assert path1 == path2
assert os.path.isdir(path1)
def test_exists_reflects_per_job_dir(self, tmp_path, object_store):
base = str(tmp_path / "jobs")