fix: supervise management calls across cancellation and parent loss
This commit is contained in:
1 parent
8d3eab1164
commit
6278091f21
8 files changed
+297
-24
No files matched your search
@@ -169,7 +169,7 @@ def _may_have_mutated(record):
|
||||
or any(not isinstance(item, dict) or item.get('stage') not in prewrite for item in history))
|
||||
|
||||
|
||||
def run_management(request, *, adapter=None, verifier=None, idle_check=None, progress=None):
|
||||
def run_management(request, *, adapter=None, verifier=None, idle_check=None, progress=None, stop=None):
|
||||
from .verify_runner import VerifyRequest, run_verify
|
||||
from .backup_recovery import _idle_check
|
||||
import win32con, win32event
|
||||
@@ -191,6 +191,8 @@ def run_management(request, *, adapter=None, verifier=None, idle_check=None, pro
|
||||
change_report = None
|
||||
def invoke(callback, *args):
|
||||
nonlocal owner_safe
|
||||
if stop is not None and stop.is_set():
|
||||
raise InterruptedError('MANAGEMENT_CANCELLED: 已停止后续原生操作')
|
||||
owner_safe = False
|
||||
try:
|
||||
result = callback(*args)
|
||||
@@ -244,6 +246,10 @@ def run_management(request, *, adapter=None, verifier=None, idle_check=None, pro
|
||||
from .com_runtime import initialize_com, uninitialize_com
|
||||
initialized = initialize_com()
|
||||
try:
|
||||
if stop is not None and stop.is_set():
|
||||
return finish('CANCELLED', '已取消,未启动原生管理操作')
|
||||
if hasattr(adapter, 'bind_stop'):
|
||||
adapter.bind_stop(stop)
|
||||
if request.operation not in ('add', 'update', 'remove'):
|
||||
raise ValueError('不支持的管理操作。')
|
||||
validate_name(request.name); validate_name(request.partition)
|
||||
@@ -349,6 +355,8 @@ def run_management(request, *, adapter=None, verifier=None, idle_check=None, pro
|
||||
if build_exclusive_library_manifest(library_root, scope=scope, progress=progress) != original:
|
||||
raise OSError('内容基线记录后中心库发生变化,停止写入。')
|
||||
if request.operation in ('remove', 'update'):
|
||||
if stop is not None and stop.is_set():
|
||||
return finish('CANCELLED', '已取消,未开始移除', owner_safe)
|
||||
checkpoint('native_remove')
|
||||
mutated = True; owner_safe = False
|
||||
native = invoke(adapter.remove, request, root/'remove')
|
||||
@@ -365,6 +373,9 @@ def run_management(request, *, adapter=None, verifier=None, idle_check=None, pro
|
||||
checkpoint('removed')
|
||||
if request.operation == 'remove':
|
||||
return finish('REMOVED', '已由原生操作移除,重新读取目录确认不存在;应用未创建备份。')
|
||||
if stop is not None and stop.is_set():
|
||||
return finish('RECOVERY_REQUIRED' if mutated else 'CANCELLED',
|
||||
'已停止后续导入;已发生的原生变更需要核对', owner_safe)
|
||||
checkpoint('native_add')
|
||||
mutated = True; owner_safe = False
|
||||
native = invoke(adapter.add, staged, root/'add')
|
||||
@@ -391,7 +402,7 @@ def run_management(request, *, adapter=None, verifier=None, idle_check=None, pro
|
||||
checkpoint('verify')
|
||||
owner_safe = False
|
||||
verified = verifier(VerifyRequest(library, request.name, root/'verify',
|
||||
mapped_drive=request.mapped_drive, mapped_remote_root=request.mapped_remote_root), progress=progress)
|
||||
mapped_drive=request.mapped_drive, mapped_remote_root=request.mapped_remote_root), progress=progress, stop=stop)
|
||||
owner_safe = verified.safe_to_continue
|
||||
checkpoint('verified', verification=asdict(verified))
|
||||
good = verified.status == 'PASS' and verified.library_flag == 'GREEN' and owner_safe
|
||||
@@ -417,14 +428,24 @@ def run_management(request, *, adapter=None, verifier=None, idle_check=None, pro
|
||||
|
||||
def run_management_batch(requests, *, stop=None, progress=None, **kwargs):
|
||||
results = []
|
||||
retained_libraries = set()
|
||||
for request in requests:
|
||||
if stop is not None and stop.is_set():
|
||||
break
|
||||
result = run_management(request, progress=progress, **kwargs)
|
||||
key = str(request.library).casefold()
|
||||
if key in retained_libraries:
|
||||
result = ManagementResult(request.operation, request.library, request.name, request.partition,
|
||||
request.source, request.auto_verify, 'complete', 'SKIPPED', True,
|
||||
'同库原生会话尚未释放,未启动新的写入')
|
||||
results.append(result)
|
||||
if progress:
|
||||
progress({'stage': result.status, 'block': result.name, 'detail': result.detail})
|
||||
continue
|
||||
result = run_management(request, progress=progress, stop=stop, **kwargs)
|
||||
results.append(result)
|
||||
if progress:
|
||||
progress({'stage': result.status, 'block': result.name, 'detail': result.detail,
|
||||
'evidence_path': str(result.evidence_path or '')})
|
||||
if not result.safe_to_continue:
|
||||
break
|
||||
retained_libraries.add(key)
|
||||
return results
|
||||
Reference in new issue
Block a user