refactor: unify capability shutdown convergence

This commit is contained in:
jxxghp
2026-08-24 13:48:11 +08:00
parent 2aa41ea8fe
commit 009631b8ee
18 changed files with 330 additions and 70 deletions
+40 -13
View File
@@ -1126,7 +1126,8 @@ class CapabilityRuntime:
"""同步撤销并停止能力资源;物化实现保留供后续显式重启。"""
self._stop_sync(capability_id, reason=reason, shutdown=False)
def _stop_sync(self, capability_id: str, *, reason: str, shutdown: bool) -> None:
def _stop_sync(self, capability_id: str, *, reason: str, shutdown: bool) -> bool:
"""停止同步能力并返回资源 owner 是否已经真实收敛。"""
state = self._state(capability_id)
adapter = self._adapter(state, AdapterExecutionMode.SYNC)
while True:
@@ -1146,7 +1147,7 @@ class CapabilityRuntime:
if stop_owner is None and pending is None:
if state.lifecycle is not CapabilityLifecycleState.FAILED:
state.lifecycle = CapabilityLifecycleState.STOPPED
return
return True
state.generation += 1
generation = state.generation
future: Future[Any] = Future()
@@ -1166,8 +1167,10 @@ class CapabilityRuntime:
except BaseException:
if not shutdown:
raise
if waiter_operation == "stop":
return False
if waiter_operation == "stop":
return
return True
continue
break
@@ -1207,6 +1210,7 @@ class CapabilityRuntime:
reason=reason,
started_at=started_at,
)
return True
except BaseException as error:
with state.lock:
state.lifecycle = CapabilityLifecycleState.FAILED
@@ -1229,12 +1233,20 @@ class CapabilityRuntime:
)
if not shutdown:
raise operation_error from error
return False
async def stop_async(self, capability_id: str, *, reason: str) -> None:
"""异步撤销并停止能力资源。"""
await self._stop_async(capability_id, reason=reason, shutdown=False)
async def _stop_async(self, capability_id: str, *, reason: str, shutdown: bool) -> None:
async def _stop_async(
self,
capability_id: str,
*,
reason: str,
shutdown: bool,
) -> bool:
"""停止异步能力并返回资源 owner 是否已经真实收敛。"""
state = self._state(capability_id)
adapter = self._adapter(state, AdapterExecutionMode.ASYNC)
while True:
@@ -1254,7 +1266,7 @@ class CapabilityRuntime:
if stop_owner is None and pending is None:
if state.lifecycle is not CapabilityLifecycleState.FAILED:
state.lifecycle = CapabilityLifecycleState.STOPPED
return
return True
state.generation += 1
generation = state.generation
future: Future[Any] = Future()
@@ -1274,8 +1286,10 @@ class CapabilityRuntime:
except BaseException:
if not shutdown:
raise
if waiter_operation == "stop":
return False
if waiter_operation == "stop":
return
return True
continue
break
@@ -1321,6 +1335,7 @@ class CapabilityRuntime:
reason=reason,
started_at=started_at,
)
return True
except BaseException as error:
with state.lock:
state.lifecycle = CapabilityLifecycleState.FAILED
@@ -1343,9 +1358,10 @@ class CapabilityRuntime:
)
if not shutdown:
raise operation_error from error
return False
def shutdown(self, *, reason: str) -> None:
"""不可逆关闭仅含同步 adapter 的 Runtime,并阻止并发首启重新发布"""
def shutdown(self, *, reason: str) -> bool:
"""不可逆关闭同步 Runtime,并返回全部 owner 是否收敛"""
async_kinds = {
kind
for kind, adapter in self._adapters.items()
@@ -1357,21 +1373,32 @@ class CapabilityRuntime:
)
with self._shutdown_lock:
self._shutdown = True
converged = True
for spec in self._registry.list_specs():
self._stop_sync(spec.id, reason=reason, shutdown=True)
if not self._stop_sync(spec.id, reason=reason, shutdown=True):
converged = False
return converged
async def shutdown_async(self, *, reason: str) -> None:
"""不可逆关闭混合同步/异步 adapter 的 Runtime"""
async def shutdown_async(self, *, reason: str) -> bool:
"""不可逆关闭混合 Runtime,并返回全部 owner 是否收敛"""
with self._shutdown_lock:
self._shutdown = True
converged = True
for spec in self._registry.list_specs():
adapter = self._adapters[spec.kind]
if getattr(adapter, "execution_mode", None) is AdapterExecutionMode.ASYNC:
await self._stop_async(spec.id, reason=reason, shutdown=True)
stopped = await self._stop_async(
spec.id,
reason=reason,
shutdown=True,
)
else:
await asyncio.to_thread(
stopped = await asyncio.to_thread(
self._stop_sync,
spec.id,
reason=reason,
shutdown=True,
)
if not stopped:
converged = False
return converged