fix: scope catalog availability and report status progress
This commit is contained in:
1 parent
31a93cd313
commit
4fb978ee21
5 files changed
+465
-44
No files matched your search
@@ -5,6 +5,7 @@ import ntpath
|
||||
import time
|
||||
from collections import Counter
|
||||
from dataclasses import replace
|
||||
from datetime import datetime
|
||||
from types import SimpleNamespace
|
||||
import json
|
||||
|
||||
@@ -76,6 +77,7 @@ STATUS_LABELS = {
|
||||
'MALFORMED': '状态桥协议错误',
|
||||
'CRASHED': '状态桥崩溃',
|
||||
}
|
||||
CATALOG_MANAGER_WAIT_SECONDS = 120
|
||||
VERIFY_RELEASE_WAIT_SECONDS = 120
|
||||
|
||||
def application_icon_path():
|
||||
@@ -118,13 +120,16 @@ class MainWindow(QWidget):
|
||||
self.native_backup_timer.timeout.connect(self._poll_native_backup)
|
||||
self._injected_reader = reader is not None
|
||||
self.reader = reader or read_library_fast
|
||||
self.catalog_reader = catalog_reader or (None if reader is not None else getattr(self.management_adapter,'catalog',None))
|
||||
self.catalog_reader = catalog_reader or (None if reader is not None else getattr(
|
||||
self.management_adapter, 'catalog_registrations', getattr(self.management_adapter, 'catalog', None)))
|
||||
self._view_revision = 0
|
||||
self._jobs = {}
|
||||
self._dialogs = []
|
||||
self._current_job = None
|
||||
self._stale_table = False
|
||||
self._table_library_path = None
|
||||
self._catalog_snapshot_time = ''
|
||||
self._catalog_retry = False
|
||||
self._read_counts = (None, None, None)
|
||||
self._diagnostic_evidence = {}
|
||||
self.pool = QThreadPool(self); self.pool.setMaxThreadCount(8)
|
||||
@@ -218,6 +223,9 @@ class MainWindow(QWidget):
|
||||
self.status_filter.currentIndexChanged.connect(self._apply_filters)
|
||||
self.timeout_spin = QSpinBox(); self.timeout_spin.setRange(30, 1800); self.timeout_spin.setValue(DEFAULT_TIMEOUT_SECONDS); self.timeout_spin.setSuffix(' 秒 / 无进展')
|
||||
self.load_button = QPushButton('读取 CBB'); self.load_button.clicked.connect(self.load_library)
|
||||
self.catalog_status_button = QPushButton('刷新已登记项状态')
|
||||
self.catalog_status_button.clicked.connect(self._refresh_registered_catalog)
|
||||
self.catalog_status_button.hide()
|
||||
self.browse_button = QPushButton('选择'); self.browse_button.clicked.connect(self.choose_library)
|
||||
self.selected_batch_button = QPushButton('批量校验已选'); self.selected_batch_button.clicked.connect(self.verify_selected)
|
||||
self.batch_button = QPushButton('校验可见非 READY'); self.batch_button.clicked.connect(self.verify_all)
|
||||
@@ -256,7 +264,7 @@ class MainWindow(QWidget):
|
||||
self.output.setMaximumHeight(180)
|
||||
layout = QVBoxLayout(self)
|
||||
row = QHBoxLayout(); row.addWidget(QLabel('中心库')); row.addWidget(self.lmc_edit)
|
||||
row.addWidget(self.browse_button); row.addWidget(self.load_button)
|
||||
row.addWidget(self.browse_button); row.addWidget(self.load_button); row.addWidget(self.catalog_status_button)
|
||||
layout.addLayout(row)
|
||||
self.library_notice = QLabel()
|
||||
self.library_notice.setWordWrap(True)
|
||||
@@ -363,6 +371,7 @@ class MainWindow(QWidget):
|
||||
self._reimport_offers = ()
|
||||
self._diagnostic_evidence.clear()
|
||||
self._resume_batch = None
|
||||
self._catalog_retry = False
|
||||
self._stale_table = bool(self._records_by_name)
|
||||
self._release_library_lease()
|
||||
self.loaded_path = None
|
||||
@@ -548,7 +557,17 @@ class MainWindow(QWidget):
|
||||
current_library = _path_key(self.mentor_lmc_path or self.lmc_edit.text()) if self.lmc_edit.text().strip() else None
|
||||
notice = ('上次操作结果待复查,执行前将重新检查占用。'
|
||||
if self._management_library_unsafe(current_library) else '')
|
||||
notice = '当前显示上次读取结果(已失效)· 请重新读取中心库。' if self._stale_table else notice
|
||||
if self._stale_table:
|
||||
if self._catalog_snapshot_time:
|
||||
when = self._catalog_snapshot_time
|
||||
notice = f'显示上次完整清单({when},待刷新)· {self._table_library_path}'
|
||||
else:
|
||||
notice = '当前显示上次读取结果(已失效)· 请重新读取中心库。'
|
||||
self.catalog_status_button.setVisible(
|
||||
self._catalog_retry and not self._closing and self._table_library_path == self.lmc_edit.text()
|
||||
and self._read_counts[1] is not None and self.mentor_lmc_path is not None)
|
||||
self.catalog_status_button.setEnabled(not self.busy)
|
||||
self.load_button.setText('重试读取' if self._catalog_retry else '读取 CBB')
|
||||
self.library_notice.setText(notice)
|
||||
self.library_notice.setVisible(bool(notice))
|
||||
for widget in (self.lmc_edit, self.browse_button, self.load_button,
|
||||
@@ -1272,11 +1291,14 @@ class MainWindow(QWidget):
|
||||
|
||||
def load_library(self):
|
||||
if self._closing or not self.lmc_edit.text(): return
|
||||
if (self._current_job is not None and self._current_job.kind == 'read'
|
||||
and self._current_job.active and self._current_job.revision == self._view_revision): return
|
||||
if any(job.kind in ('read', 'catalog') and job.active
|
||||
and not job.context.stop.is_set() and job.revision == self._view_revision
|
||||
for job in self._jobs.values()):
|
||||
return
|
||||
self._cancel_catalog_read()
|
||||
self._view_revision += 1
|
||||
self._diagnostic_evidence.clear()
|
||||
self._catalog_retry = False
|
||||
self._stale_table = bool(self._records_by_name)
|
||||
job = self._new_job('read', library=self.lmc_edit.text())
|
||||
job.lease = None
|
||||
@@ -1293,7 +1315,7 @@ class MainWindow(QWidget):
|
||||
self._release_library_lease()
|
||||
self.loaded_path = None
|
||||
path = self.lmc_edit.text()
|
||||
preserve = self._table_library_path == path and self._read_counts[1] is not None
|
||||
preserve = self._read_counts[1] is not None
|
||||
self.status_label.setText('正在读取中心库…')
|
||||
def read():
|
||||
import pythoncom
|
||||
@@ -1323,8 +1345,9 @@ class MainWindow(QWidget):
|
||||
len(result.records), int((time.monotonic() - started) * 1000),
|
||||
dict(statuses), unknown[:20])
|
||||
skip_reasons = {}
|
||||
if not result.error:
|
||||
if not result.error and (self.catalog_reader is None or result.catalog_verified):
|
||||
for record in result.records:
|
||||
job.context.check_active()
|
||||
reason = preflight_skip_reason(mentor_path, record.name)
|
||||
if reason:
|
||||
skip_reasons[record.name] = reason
|
||||
@@ -1352,12 +1375,14 @@ class MainWindow(QWidget):
|
||||
self._publish_check_statuses(mentor_path,
|
||||
{record.name: record.status for record in result.records
|
||||
if record.catalog_source != 'directory_only'}, lease)
|
||||
preview = self.catalog_reader is not None and not result.catalog_verified
|
||||
self._stale_table = False
|
||||
self._table_library_path = path
|
||||
self._catalog_snapshot_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S') if result.catalog_verified else ''
|
||||
self._read_counts = (result.directory_count, result.native_count,
|
||||
result.missing_project_count if result.missing_project_count is not None else len(skip_reasons))
|
||||
self._maintenance_recovery_view = recovery_view
|
||||
self.loaded_path = path
|
||||
self.loaded_path = path if not preview or self._injected_reader else None
|
||||
try:
|
||||
if self._settings_enabled:
|
||||
self.data_root.mkdir(parents=True, exist_ok=True)
|
||||
@@ -1426,7 +1451,7 @@ class MainWindow(QWidget):
|
||||
self.status_label.setText('协调阻止恢复视图:仅按目录显示工程,状态未确认(UNKNOWN),不代表损坏。'
|
||||
'仅可明确执行 ICDB 维护或释放检查,其他写入与原生状态读取已禁用。')
|
||||
self._apply_filters()
|
||||
if self.catalog_reader is not None and not result.catalog_verified:
|
||||
if preview:
|
||||
self._start_catalog(job, result, lease, mentor_path, loaded)
|
||||
self._start(read, loaded, queued=False, job=job, metadata_only=True,
|
||||
stale=lambda payload: self._close_path_lease(payload[2]))
|
||||
@@ -1462,18 +1487,29 @@ class MainWindow(QWidget):
|
||||
directories = {record.name: record for record in initial.records}
|
||||
names = tuple(sorted(native.keys() | directories.keys()))
|
||||
context.check_active()
|
||||
phase('catalog_status', f'正在核对 {len(native)} 项原生状态…')
|
||||
phase('catalog_status', f'已核对 0/{len(native)} 项原生状态…')
|
||||
if checkpoint is None:
|
||||
statuses = self.status_refresher(tuple(native), Path(library))
|
||||
else:
|
||||
statuses = self.status_refresher(tuple(native), Path(library), checkpoint)
|
||||
if native and (not native.keys() <= statuses.keys()
|
||||
or any(statuses[name] == 'UNAVAILABLE' for name in native)
|
||||
or not any(statuses[name] in {'READY', 'PENDING'} for name in native)):
|
||||
from cbb_refresh_verify.operation_queue import note_metadata_progress
|
||||
last_notice = 0.0
|
||||
|
||||
def report_progress(done, total):
|
||||
nonlocal last_notice
|
||||
note_metadata_progress()
|
||||
now = time.monotonic()
|
||||
if done == total or now - last_notice >= .2:
|
||||
last_notice = now
|
||||
phase('catalog_status', f'已核对 {done}/{total} 项原生状态…')
|
||||
|
||||
statuses = self.status_refresher(tuple(native), Path(library), checkpoint, progress=report_progress)
|
||||
if any(statuses.get(name) not in {'READY', 'PENDING'} for name in native):
|
||||
raise ValueError('原生状态读取未确认,请重新读取中心库。')
|
||||
context.check_active()
|
||||
phase('catalog_reconcile', '正在合并目录和状态…')
|
||||
records, reasons = [], {}
|
||||
for name in names:
|
||||
context.check_active()
|
||||
source = ('both' if name in native and name in directories else
|
||||
'native_only' if name in native else 'directory_only')
|
||||
reason = ('CATALOG_NOT_REGISTERED: 仅发现目录,原生目录未登记。' if name not in native else
|
||||
@@ -1502,13 +1538,19 @@ class MainWindow(QWidget):
|
||||
except NativeOperationError as error:
|
||||
if not error.safe_to_continue or not str(error).startswith('LIBRARY_MANAGER_BUSY:'):
|
||||
raise
|
||||
phase('catalog_waiting', 'Library Manager 正在使用,退出后将自动重试读取。')
|
||||
deadline = time.monotonic() + 30
|
||||
phase('catalog_waiting', f'{error} 等待释放后自动重试读取。')
|
||||
deadline = time.monotonic() + CATALOG_MANAGER_WAIT_SECONDS
|
||||
blockers = set(error.blocking_manager_pids)
|
||||
while time.monotonic() < deadline:
|
||||
context.check_active()
|
||||
owners = query_owners(('LibraryManager.exe',), timeout_seconds=2)
|
||||
try:
|
||||
owners = query_owners(('LibraryManager.exe',),
|
||||
timeout_seconds=min(2, max(.1, deadline - time.monotonic())))
|
||||
except (OSError, RuntimeError):
|
||||
context.stop.wait(1)
|
||||
continue
|
||||
context.check_active()
|
||||
if not owners:
|
||||
if not owners or blockers and blockers.isdisjoint(owner['pid'] for owner in owners):
|
||||
return attempt(root/'retry')
|
||||
context.stop.wait(1)
|
||||
context.check_active()
|
||||
@@ -1529,6 +1571,8 @@ class MainWindow(QWidget):
|
||||
cancelled = context.stop.is_set() or isinstance(error, QueueCancelled)
|
||||
detail = '已取消读取中心库。' if cancelled else f'原生目录未确认,请重新读取:{error}'
|
||||
phase('catalog_cancelled' if cancelled else 'catalog_failed', detail)
|
||||
self._catalog_retry = True
|
||||
self._stale_table = bool(self._records_by_name) and bool(self._catalog_snapshot_time)
|
||||
self.status_label.setText(detail)
|
||||
|
||||
def completed(payload):
|
||||
@@ -1550,6 +1594,28 @@ class MainWindow(QWidget):
|
||||
worker.signals.finished.connect(lambda:(self._lifecycle_workers.discard(worker),self._finish_job(job)))
|
||||
self.pool.start(worker)
|
||||
|
||||
def _refresh_registered_catalog(self):
|
||||
if (self.busy or not self._catalog_retry or self.mentor_lmc_path is None
|
||||
or self._table_library_path != self.lmc_edit.text() or self._read_counts[1] is None):
|
||||
return
|
||||
library, lease = self.mentor_lmc_path, self.library_lease
|
||||
names = tuple(name for name, record in self._records_by_name.items()
|
||||
if record.catalog_source in {'both', 'native_only'})
|
||||
if not names:
|
||||
self.status_label.setText('上次清单中没有已登记项。')
|
||||
return
|
||||
job = self._new_job('status', library=library, targets=names, lease=lease)
|
||||
self.status_label.setText('正在刷新上次登记项的原生状态…')
|
||||
|
||||
def refreshed(statuses):
|
||||
if any(statuses.get(name) not in {'READY', 'PENDING'} for name in names):
|
||||
self.status_label.setText('原生状态未确认,保留上次读取结果。')
|
||||
return
|
||||
self._apply_refreshed_records((names, statuses, None, ''))
|
||||
self.status_label.setText(f'已刷新 {len(names)} 项原生状态,目录仍待重新读取。')
|
||||
|
||||
self._start(lambda: self.status_refresher(names, Path(library)), refreshed, job=job)
|
||||
|
||||
def _execution_status_reader(self, operation_id, lease):
|
||||
"""Recheck registration once per submission and status at each admission."""
|
||||
catalog_reader, status_reader = self.catalog_reader, self.status_refresher
|
||||
@@ -2000,7 +2066,7 @@ class MainWindow(QWidget):
|
||||
from shiboken6 import isValid
|
||||
if not isValid(self) or not isValid(self.cbb_table):
|
||||
return
|
||||
if event.get('stage') in {'catalog_start', 'catalog_status', 'catalog_waiting'}:
|
||||
if event.get('stage') in {'catalog_start', 'catalog_status', 'catalog_waiting', 'catalog_reconcile'}:
|
||||
catalog_job = self._jobs.get(event.get('catalog_id'))
|
||||
if catalog_job is None or catalog_job.context.stop.is_set():
|
||||
return
|
||||
|
||||
Reference in new issue
Block a user