Skip to content

Transforms complex documents like PDFs and Office docs into LLM-ready markdown/JSON for your Agentic workflows.

Restart managed parse server on tier change

jinzhenjfa42a0c0e410
diff --git a/mineru/doclib/app.py b/mineru/doclib/app.py
index a8082f9e..f0758b58 100644
--- a/mineru/doclib/app.py
+++ b/mineru/doclib/app.py
@@ -132,6 +132,7 @@ def create_app(cfg: Config | None = None) -> FastAPI:
health.managed_url = managed_url
logging.info("Managed parse-server started (PID %d, tier=%s)", proc.pid, managed_tier)
health.managed_proc = proc
+ health.running_managed_tier = managed_tier
health.local_starting = True
health.local_started_at = asyncio.get_event_loop().time()
except Exception as exc:
diff --git a/mineru/doclib/background/parse_server_health.py b/mineru/doclib/background/parse_server_health.py
index 87f603d8..e0921b24 100644
--- a/mineru/doclib/background/parse_server_health.py
+++ b/mineru/doclib/background/parse_server_health.py
@@ -37,6 +37,7 @@ class ParseServerHealth:
self_hosted_url: str | None = None
managed_url: str = DEFAULT_MANAGED_URL
managed_tier: Tier | None = None
+ running_managed_tier: Tier | None = None
local_last_probe_at: int | None = None
local_last_success_at: int | None = None
local_last_failure_at: int | None = None
@@ -234,7 +235,8 @@ class ParseServerHealthCheck:
mode = (await self.config_svc.get("parse_server.local.mode")) or "disabled"
health.local_mode = mode
managed_tier = (await self.config_svc.get("parse_server.local.managed_tier")) or "standard"
- health.managed_tier = cast(Tier, managed_tier)
+ desired_managed_tier = cast(Tier, managed_tier)
+ health.managed_tier = desired_managed_tier
self_hosted_url = await self.config_svc.get("parse_server.local.self_hosted_url")
health.self_hosted_url = self_hosted_url if self_hosted_url else None
@@ -274,31 +276,63 @@ class ParseServerHealthCheck:
continue # still loading models, don't restart
logger.warning("Managed parse-server is unhealthy, attempting restart")
await self._try_restart_managed(health)
+ elif health.local_mode == "managed" and health.local_healthy:
+ await self._try_restart_managed_for_tier_change(health, desired_managed_tier)
await asyncio.sleep(self.interval_sec)
- async def _try_restart_managed(self, health: ParseServerHealth) -> None:
- if health.restart_count >= MAX_RESTART_ATTEMPTS:
+ async def _try_restart_managed_for_tier_change(
+ self, health: ParseServerHealth, desired_managed_tier: Tier
+ ) -> bool:
+ running_managed_tier = health.running_managed_tier
+ if running_managed_tier is None or running_managed_tier == desired_managed_tier:
+ return False
+
+ logger.info(
+ "Managed parse-server tier changed from %s to %s, restarting",
+ running_managed_tier,
+ desired_managed_tier,
+ )
+ await self._try_restart_managed(
+ health,
+ reason="tier-change",
+ marker=f"tier change {running_managed_tier}->{desired_managed_tier}",
+ count_restart=False,
+ )
+ return True
+
+ async def _try_restart_managed(
+ self,
+ health: ParseServerHealth,
+ *,
+ reason: str = "restart",
+ marker: str | None = None,
+ count_restart: bool = True,
+ ) -> None:
+ if count_restart and health.restart_count >= MAX_RESTART_ATTEMPTS:
logger.error(
"Managed parse-server failed %d restarts, disabling", MAX_RESTART_ATTEMPTS
)
health.local_mode = "disabled"
return
- health.restart_count += 1
+ if count_restart:
+ health.restart_count += 1
managed_tier = (await self.config_svc.get("parse_server.local.managed_tier")) or "standard"
- stop_managed_parse_server(health.managed_proc, timeout_sec=self.stop_timeout_sec, reason="restart")
+ stop_managed_parse_server(health.managed_proc, timeout_sec=self.stop_timeout_sec, reason=reason)
health.managed_proc = None
+ health.running_managed_tier = None
try:
proc, managed_url = start_managed_parse_server(
tier=cast(Tier, managed_tier),
managed_cfg=self.managed_parse_server,
log_cfg=self.log_cfg,
- marker=f"restart attempt {health.restart_count}",
+ marker=marker or f"restart attempt {health.restart_count}",
)
health.managed_url = managed_url
logger.info("Managed parse-server restarted (PID %d, tier=%s)", proc.pid, managed_tier)
health.managed_proc = proc
+ health.running_managed_tier = cast(Tier, managed_tier)
health.local_starting = True
health.local_started_at = asyncio.get_event_loop().time()
except Exception as exc:
diff --git a/tests/unittest/test_doclib_cache_semantics.py b/tests/unittest/test_doclib_cache_semantics.py
index 9ec8eed5..88b952c1 100644
--- a/tests/unittest/test_doclib_cache_semantics.py
+++ b/tests/unittest/test_doclib_cache_semantics.py
@@ -600,6 +600,97 @@ def test_managed_parse_server_restart_stops_recorded_proc_before_start(monkeypat
assert health.managed_proc.pid == 67890
+def test_managed_parse_server_tier_change_detection_triggers_restart(monkeypatch: pytest.MonkeyPatch) -> None:
+ calls: list[tuple[str, str, bool]] = []
+
+ class _ConfigSvc:
+ async def get(self, key: str) -> str:
+ raise AssertionError(f"unexpected config read: {key}")
+
+ checker = ParseServerHealthCheck(
+ _ConfigSvc(),
+ interval_sec=1,
+ probe_timeout_sec=2,
+ startup_grace_sec=3,
+ stop_timeout_sec=4,
+ managed_parse_server=ManagedParseServerConfig(host="127.0.0.2", port=16580, port_probe_count=3),
+ )
+
+ async def _restart(
+ health_arg: ParseServerHealth,
+ *,
+ reason: str,
+ marker: str | None,
+ count_restart: bool,
+ ) -> None:
+ assert health_arg is health
+ calls.append((reason, marker or "", count_restart))
+
+ monkeypatch.setattr(checker, "_try_restart_managed", _restart)
+ health = ParseServerHealth(running_managed_tier="standard")
+
+ restarted = asyncio.run(checker._try_restart_managed_for_tier_change(health, "pro"))
+ unchanged = asyncio.run(checker._try_restart_managed_for_tier_change(health, "standard"))
+
+ assert restarted is True
+ assert unchanged is False
+ assert calls == [("tier-change", "tier change standard->pro", False)]
+
+
+def test_managed_parse_server_tier_change_restart_uses_desired_tier(monkeypatch: pytest.MonkeyPatch) -> None:
+ events: list[str] = []
+ old_proc = object()
+
+ class _ConfigSvc:
+ async def get(self, key: str) -> str:
+ assert key == "parse_server.local.managed_tier"
+ return "pro"
+
+ class _Proc:
+ pid = 24680
+
+ def _stop(proc: object, *, timeout_sec: int, reason: str) -> None:
+ assert proc is old_proc
+ assert timeout_sec == 4
+ assert reason == "tier-change"
+ events.append("stop")
+
+ def _start(*, tier: Tier, managed_cfg: ManagedParseServerConfig, log_cfg: LogConfig | None, marker: str) -> tuple[_Proc, str]:
+ assert tier == "pro"
+ assert managed_cfg.host == "127.0.0.2"
+ assert log_cfg is None
+ assert marker == "tier change standard->pro"
+ events.append("start")
+ return _Proc(), "http://127.0.0.2:16582"
+
+ monkeypatch.setattr("mineru.doclib.background.parse_server_health.stop_managed_parse_server", _stop)
+ monkeypatch.setattr("mineru.doclib.background.parse_server_health.start_managed_parse_server", _start)
+
+ checker = ParseServerHealthCheck(
+ _ConfigSvc(),
+ interval_sec=1,
+ probe_timeout_sec=2,
+ startup_grace_sec=3,
+ stop_timeout_sec=4,
+ managed_parse_server=ManagedParseServerConfig(host="127.0.0.2", port=16580, port_probe_count=3),
+ )
+ health = ParseServerHealth(managed_proc=old_proc, running_managed_tier="standard", restart_count=2)
+
+ asyncio.run(
+ checker._try_restart_managed(
+ health,
+ reason="tier-change",
+ marker="tier change standard->pro",
+ count_restart=False,
+ )
+ )
+
+ assert events == ["stop", "start"]
+ assert health.managed_proc.pid == 24680
+ assert health.running_managed_tier == "pro"
+ assert health.restart_count == 2
+
+
def test_get_file_stat_returns_typed_file_stat(tmp_path: Path) -> None:
source = tmp_path / "note.txt"
source.write_text("content", encoding="utf-8")