fix: finish current management item before stopping its batch

This commit is contained in:
阿白叔 committed 2026-11-17 04:53:30 +08:00
1 parent fd50805380
commit e0bb2438e8
5 files changed
+178 -7

No files matched your search

+4 -2
View File
@@ -465,7 +465,8 @@ def run_management(request, *, adapter=None, verifier=None, idle_check=None, pro
def run_management_batch(requests, *, stop=None, progress=None, item_executor=None, context=None, **kwargs):
from threading import Event
from .write_admission import WriteAdmission
from .operation_queue import QueueCancelled, TaskHandoff, request_library_keys, remember_result
from .operation_queue import QueueCancelled, TaskHandoff, request_library_keys, remember_result, current_context
context = context or current_context()
if context is not None:
stop = stop if stop is not None else context.stop
progress = context.guard_callback(progress) if progress else None
@@ -477,7 +478,8 @@ def run_management_batch(requests, *, stop=None, progress=None, item_executor=No
cancelled = False
for request in requests:
keys = request_library_keys(request, context)
stopped = cancelled or stop.is_set() or context is not None and context.stop.is_set()
stopped = (cancelled or stop.is_set() or context is not None
and (context.stop.is_set() or context.stop_after_current.is_set()))
if stopped or keys.intersection(retained_libraries):
result = ManagementResult(request.operation, request.library, request.name, request.partition,
request.source, request.auto_verify, 'complete', 'SKIPPED', True,
+5 -2
View File
@@ -237,6 +237,8 @@ class BatchTaskContext:
stop: _StopSignal = field(default_factory=_StopSignal, repr=False, compare=False)
_closed: Event = field(default_factory=Event, repr=False, compare=False)
native_policy: str = field(default='legacy', kw_only=True)
# Only queue/batch boundaries consume this; the current item retains hard-stop semantics.
stop_after_current: _StopSignal = field(default_factory=_StopSignal, repr=False, compare=False, kw_only=True)
def __post_init__(self):
if self.native_policy not in ('legacy', 'restricted', 'external'):
@@ -402,7 +404,7 @@ class NativeQueue:
available = ready() # Call only this ticket's predicate, outside the queue lock.
with self.condition:
if (self.closed or stop is not None and stop.is_set()
or context is not None and context.stop.is_set()):
or context is not None and (context.stop.is_set() or context.stop_after_current.is_set())):
raise QueueCancelled('已取消排队,未启动原生操作。')
ticket[1] = bool(available)
first = next((entry for entry in self.pending if entry[1]), None)
@@ -422,7 +424,8 @@ class NativeQueue:
notified, previous_state = True, current_state
except Exception:
pass
if stop is not None and stop.is_set():
if (stop is not None and stop.is_set()
or context is not None and context.stop_after_current.is_set()):
raise QueueCancelled('已取消排队,未启动原生操作。')
return context.run(action, stop=stop, timeout_seconds=timeout_seconds) if context else action()
finally:
@@ -100,7 +100,7 @@ class ManagementDialog(QDialog):
self.execute_button = QPushButton('预览并确认执行…')
self.execute_button.clicked.connect(self._execute)
self.stop_button = QPushButton('当前项结束后停止'); self.stop_button.setEnabled(False)
self.stop_button.clicked.connect(lambda:self.stop.set())
self.stop_button.clicked.connect(lambda:self.context.stop_after_current.set())
self.close_button = QPushButton('关闭'); self.close_button.clicked.connect(self.reject)
layout = QVBoxLayout(self)
layout.addWidget(QLabel(f'目标库:{self.template.library}'))
@@ -305,6 +305,6 @@ class ManagementDialog(QDialog):
def closeEvent(self, event):
if self.busy:
self.stop.set(); event.ignore()
self.context.stop_after_current.set(); event.ignore()
else:
super().closeEvent(event)