fix: 手动运行启动后立即显示运行中状态
- Runner.start 在返回前先拿锁并创建运行记录,后台线程只负责执行 - 修复刚点运行时状态接口短暂显示空闲、紧接着的查询拿到上一条记录的问题
This commit is contained in:
+21
-7
@@ -74,16 +74,33 @@ class Runner:
|
||||
return text
|
||||
|
||||
def start(self, trigger: str, dry_run: bool = False) -> bool:
|
||||
if self.busy():
|
||||
run_id = self._begin(trigger, dry_run)
|
||||
if run_id is None:
|
||||
return False
|
||||
threading.Thread(target=self.run, args=(trigger, dry_run), name=f"etl-{trigger}", daemon=True).start()
|
||||
threading.Thread(target=self._finish, args=(run_id, dry_run), name=f"etl-{trigger}", daemon=True).start()
|
||||
return True
|
||||
|
||||
def run(self, trigger: str, dry_run: bool = False) -> int | None:
|
||||
run_id = self._begin(trigger, dry_run)
|
||||
if run_id is None:
|
||||
return None
|
||||
self._finish(run_id, dry_run)
|
||||
return run_id
|
||||
|
||||
def _begin(self, trigger: str, dry_run: bool) -> int | None:
|
||||
if not self._lock.acquire(blocking=False):
|
||||
return None
|
||||
try:
|
||||
return self._execute(trigger, dry_run)
|
||||
run_id = self.runs.create(trigger, dry_run)
|
||||
except BaseException:
|
||||
self._lock.release()
|
||||
raise
|
||||
self._current = {"id": run_id, "trigger": trigger, "dry_run": dry_run, "started_at": now_text()}
|
||||
return run_id
|
||||
|
||||
def _finish(self, run_id: int, dry_run: bool) -> None:
|
||||
try:
|
||||
self._execute(run_id, dry_run)
|
||||
finally:
|
||||
self._process = None
|
||||
self._current = None
|
||||
@@ -98,10 +115,8 @@ class Runner:
|
||||
self._terminate(process)
|
||||
return True
|
||||
|
||||
def _execute(self, trigger: str, dry_run: bool) -> int:
|
||||
def _execute(self, run_id: int, dry_run: bool) -> None:
|
||||
settings = self.settings_store.get()
|
||||
run_id = self.runs.create(trigger, dry_run)
|
||||
self._current = {"id": run_id, "trigger": trigger, "dry_run": dry_run, "started_at": now_text()}
|
||||
self.log_dir.mkdir(parents=True, exist_ok=True)
|
||||
self.output_dir.mkdir(parents=True, exist_ok=True)
|
||||
command = [
|
||||
@@ -145,7 +160,6 @@ class Runner:
|
||||
exit_code=exit_code,
|
||||
)
|
||||
self._prune(settings.keep_outputs, settings.keep_runs)
|
||||
return run_id
|
||||
|
||||
def _classify(
|
||||
self,
|
||||
|
||||
@@ -199,6 +199,8 @@ class RunnerTest(unittest.TestCase):
|
||||
def test_cancel_stops_worker_and_busy_runner_rejects_second_run(self) -> None:
|
||||
self.mode_file.write_text("sleep", encoding="utf-8")
|
||||
self.assertTrue(self.runner.start("manual"))
|
||||
self.assertEqual(self.runner.current["trigger"], "manual")
|
||||
self.assertFalse(self.runner.start("manual"))
|
||||
deadline = time.monotonic() + 10
|
||||
while self.runner._process is None and time.monotonic() < deadline:
|
||||
time.sleep(0.05)
|
||||
|
||||
Reference in New Issue
Block a user