Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 31 additions & 5 deletions app/nullius/execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,9 @@ def events(self, value: List[dict]) -> None:
self._unread, self._events = False, value


def _save(self) -> None:
def _save(self, strict: bool = False) -> None:
"""``run.json``, the run as it is now. A write that fails is let go, the
last record saved standing; ``strict`` (the first, ``start_all``): OSError."""
with self._turn:
data = {"id": self.id, "plan": self.plan.to_dict(), "state": self.state, "step": self.step, "part": self.part, "pid": self.pid,
"log_offset": self.log_offset, "exit_code": self.exit_code, "error": self.error, "question": self.question, "ends": self.ends,
Expand All @@ -496,7 +498,8 @@ def _save(self) -> None:
os.makedirs(self.dir, exist_ok=True)
ondisk.write_whole(os.path.join(self.dir, RECORD), json.dumps(data, indent=1))
except OSError:
pass
if strict:
raise


def log(self, line: str) -> None:
Expand Down Expand Up @@ -1727,9 +1730,32 @@ def _probe(run: Callable, argv: List[str], timeout: float) -> tuple:


def start(plan: Plan, **kw) -> Execution:
ex = Execution(plan, **kw)
EXECUTIONS[ex.id] = ex
return ex.start()
return start_all([Execution(plan, **kw)])[0]


def start_all(runs: List[Execution]) -> List[Execution]:
"""Start *runs*, those of one launch, once each has its first record: a
run without one could not be picked up after a restart (``restore``).
OSError when one cannot be saved: none is registered or started, and the
records already saved are removed, since a restart would start them."""
saved = []
try:
for ex in runs:
ex._save(strict=True)
saved.append(ex)
except OSError:
for ex in runs:
try:
if ex in saved:
os.remove(os.path.join(ex.dir, RECORD))
os.rmdir(ex.dir) # only the folder the save made, empty
except OSError:
pass
raise
for ex in runs:
EXECUTIONS[ex.id] = ex
ex.start()
return runs


def active() -> List[Execution]:
Expand Down
19 changes: 19 additions & 0 deletions app/ui_tests/test_run_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,25 @@ def test_workspace_failure_starts_no_worker_and_writes_no_run_record(self):
self.assertEqual(execution.EXECUTIONS, {})
self.assertEqual(list((self.tmp / "runs").glob("*/run.json")), [])

def test_a_run_record_failure_starts_no_worker_and_leaves_no_run_record(self):
started, write = [], execution.ondisk.write_whole

def fail_second(path, data, mode=None): # the first run's record saved, the second's refused
if Path(path).parent.name == "second":
raise OSError(28, "No space left on device")
return write(path, data, mode)

with patch.dict(execution.EXECUTIONS, clear=True), \
patch.object(execution.Plan, "check", return_value=""), \
patch.object(execution.Execution, "_main", lambda ex: started.append(ex.plan.name)), \
patch.object(execution.ondisk, "write_whole", fail_second):
runs, problem, status = web.execute.begin([
execution.Plan(name="first", script="true"), execution.Plan(name="second", script="true")], "trials")
self.assertEqual((runs, status), ([], 503), problem)
self.assertIn(str(self.tmp / "runs"), problem)
self.assertEqual((started, execution.EXECUTIONS), ([], {}))
self.assertEqual(list((self.tmp / "runs").glob("*/run.json")), [])

def test_a_failed_workspace_save_does_not_add_roots_in_memory(self):
ws = workspace.create(self.home, [str(self.root)], "trials")
web.shared.open_workspace(ws)
Expand Down
8 changes: 5 additions & 3 deletions app/web/execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,11 @@ def begin(plans: list, into: str = "") -> tuple:
return [], str(error), 400
except OSError:
return [], "The launch workspace could not be saved. Check its permissions and free space, then retry.", 503
for ex in prepared:
execution.EXECUTIONS[ex.id] = ex
ex.start()
try:
execution.start_all(prepared)
except OSError as error:
return [], (f"The run's record could not be saved ({error.strerror or error}). "
f"Check the permissions and free space of {execution.runs_root()}, then retry."), 503
return prepared, "", 200


Expand Down