diff --git a/app/nullius/execution.py b/app/nullius/execution.py index fcd04e579..a074fe6af 100644 --- a/app/nullius/execution.py +++ b/app/nullius/execution.py @@ -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, @@ -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: @@ -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]: diff --git a/app/ui_tests/test_run_routes.py b/app/ui_tests/test_run_routes.py index d0e4283c9..1fe145bf3 100644 --- a/app/ui_tests/test_run_routes.py +++ b/app/ui_tests/test_run_routes.py @@ -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) diff --git a/app/web/execute.py b/app/web/execute.py index 9447d2c1d..05ee28fe8 100644 --- a/app/web/execute.py +++ b/app/web/execute.py @@ -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