fix: hold batch admission through startup recovery

This commit is contained in:
阿白叔 committed 2026-10-23 16:00:18 +08:00
1 parent 1baf908ba4
commit ba5b13667b
3 files changed
+54 -14

No files matched your search

@@ -3,7 +3,7 @@
from __future__ import annotations
from collections.abc import Callable
from contextlib import contextmanager
from contextlib import contextmanager, ExitStack
from datetime import datetime
import os
from pathlib import Path
@@ -117,18 +117,21 @@ class TaskCenterService:
self._path_opener = path_opener
self._clock = clock
self._supervised = supervised
if runner is None and recover_on_startup:
from .batch_supervisor import assert_supervisor_idle, pending_supervisors
if pending_supervisors(logging_context.root):
recover_on_startup = False
try:
assert_supervisor_idle(logging_context.root)
except TaskCenterError as error:
if error.code != 'supervisor_busy':
raise
recover_on_startup = False
self._runner = runner or TrialRunner(store, logging_context, probe_source_resolver=probe_source_resolver,
**({'recover_on_startup': False} if not recover_on_startup else {}))
with ExitStack() as startup:
if runner is None and recover_on_startup:
from .batch_supervisor import mutation_lease, pending_supervisors
try:
startup.enter_context(mutation_lease(logging_context.root))
except TaskCenterError as error:
if error.code != 'supervisor_busy':
raise
recover_on_startup = False
if recover_on_startup and pending_supervisors(logging_context.root):
recover_on_startup = False
# Keep admission for the whole recovery, not just an idle probe:
# another window must not start a batch while startup rewrites state.
self._runner = runner or TrialRunner(store, logging_context, probe_source_resolver=probe_source_resolver,
**({'recover_on_startup': False} if not recover_on_startup else {}))
self._quick_run_preparer = quick_run_preparer or QuickRunPreparer(logging_context.root)
self._batch_feedback = None
self._project_facades: dict[str, TaskCenterService] = {}
@@ -60,7 +60,12 @@ def _lock_file(root: Path):
def assert_task_queue_idle(root: Path) -> None:
"""Reject external mutations while a queue owns CAD; its runner may call services."""
from .batch_supervisor import assert_supervisor_idle
assert_supervisor_idle(root)
try:
assert_supervisor_idle(root)
except TaskCenterError as error:
if error.code != 'supervisor_busy':
raise
raise TaskCenterError('quick_run_busy', '另一批操作正在执行,请先安全暂停。') from error
if _QUEUE_OWNER == get_ident():
return
if _QUEUE_LOCK.locked():
+32
View File
@@ -90,6 +90,38 @@ def test_cancelled_request_does_not_launch_supervisor(tmp_path):
assert not (tmp_path/'batch-resources').exists()
def test_startup_recovery_holds_admission_until_runner_initialized(tmp_path, monkeypatch):
from dx_productivity.trial_task_center import service as module
from dx_productivity.trial_task_center.batch_supervisor import assert_supervisor_idle
from dx_productivity.trial_task_center.errors import TaskCenterError
from dx_productivity.trial_task_center.logging_setup import configure_logging
from dx_productivity.trial_task_center.store import TaskStore
checked = []
def runner(*args, **kwargs):
with pytest.raises(TaskCenterError, match='supervisor_busy'):
assert_supervisor_idle(tmp_path)
checked.append(True)
return object()
monkeypatch.setattr(module, 'TrialRunner', runner)
store = TaskStore(tmp_path/'task-center.db')
try:
module.TaskCenterService(store, configure_logging(tmp_path))
assert checked == [True]
assert_supervisor_idle(tmp_path)
finally:
store.close()
def test_crashed_legacy_task_protects_actual_root_without_replay(tmp_path):
from dx_productivity.trial_task_center.batch_supervisor import _mark_interrupted
journal = {'version': 1, 'batch_id': 'a'*32, 'attempt_id': 'b'*32,
'operation': 'execute_task_queue', 'task_ids': ['legacy'], 'state': 'running',
'uncertainty_roots': {'legacy': str(tmp_path)}}
_mark_interrupted(tmp_path, journal, 'missing final result')
assert (tmp_path/'quick-run/uncertain.flag').exists()
assert not (tmp_path/'tasks/legacy/quick-run/uncertain.flag').exists()
def test_parent_job_cleanup_failure_preserves_result_and_can_retry_resources_only(tmp_path, monkeypatch):
from dx_productivity.trial_task_center.batch_supervisor import run_supervised
from dx_productivity.trial_task_center.isolated_desktop import _Win32Api