fix: preserve native reads and advancing metadata operations
This commit is contained in:
1 parent
5b7e7bea2f
commit
07206ce0d1
5 files changed
+128
-8
No files matched your search
@@ -244,13 +244,17 @@ class NativeManagementAdapter:
|
||||
worker.bind_cancellation(job['cancel_signal'])
|
||||
if hasattr(worker, 'bind_session'):
|
||||
worker.bind_session(job)
|
||||
from .operation_queue import commit_worker_start
|
||||
commit_worker_start(worker, job)
|
||||
while worker.poll() is None and time.monotonic() < deadline:
|
||||
if self.stop is not None and self.stop.is_set():
|
||||
Path(job['cancel_signal']).touch()
|
||||
break
|
||||
time.sleep(.1)
|
||||
from contextlib import nullcontext
|
||||
from .operation_queue import commit_worker_start, native_channel
|
||||
# Only admitted native waiting outlives the UI's metadata deadline.
|
||||
channel = native_channel(read_only=True) if job.get('worker_role') == 'catalog_readonly' else nullcontext()
|
||||
with channel:
|
||||
commit_worker_start(worker, job)
|
||||
while worker.poll() is None and time.monotonic() < deadline:
|
||||
if self.stop is not None and self.stop.is_set():
|
||||
Path(job['cancel_signal']).touch()
|
||||
break
|
||||
time.sleep(.1)
|
||||
if worker.poll() is None:
|
||||
# DesktopWorker.close() requests a kill without a cancellation file.
|
||||
# Keep this exact worker alive until its native call returns normally.
|
||||
|
||||
@@ -99,6 +99,16 @@ def check_active():
|
||||
raise QueueCancelled('任务已取消,未提交新的原生操作。')
|
||||
|
||||
|
||||
def note_metadata_progress():
|
||||
"""Renew inactivity only after completed I/O; never revive an abandoned call."""
|
||||
call = getattr(_local, 'call', None)
|
||||
if call is not None:
|
||||
with _tasks_guard:
|
||||
if not call.native_depth:
|
||||
check_active()
|
||||
call.phase_started = time.monotonic()
|
||||
|
||||
|
||||
def remember_result(result):
|
||||
call = getattr(_local, 'call', None)
|
||||
if call is not None:
|
||||
|
||||
@@ -12,6 +12,7 @@ import os
|
||||
from pathlib import Path
|
||||
import shutil
|
||||
import stat
|
||||
from .operation_queue import note_metadata_progress
|
||||
|
||||
|
||||
MANIFEST_VERSION = 1
|
||||
@@ -207,6 +208,7 @@ def scan_tree(root, *, scope=None, exclude=None, check=None):
|
||||
if not (stat.S_ISDIR(metadata.st_mode) or stat.S_ISREG(metadata.st_mode)):
|
||||
raise ValueError(f'不支持的特殊文件:{entry.path}')
|
||||
entries[path] = metadata
|
||||
note_metadata_progress()
|
||||
if stat.S_ISDIR(metadata.st_mode):
|
||||
folders.append(path)
|
||||
return dict(sorted(entries.items()))
|
||||
@@ -252,6 +254,7 @@ def build_manifest(root, *, scope=None, exclude=None, progress=None, hash_functi
|
||||
_emit(progress, 'hash', 0, 0, total_files=len(selected))
|
||||
with ThreadPoolExecutor(max_workers=8) as pool:
|
||||
for count, (relative, item) in enumerate(pool.map(hashed, selected), 1):
|
||||
note_metadata_progress()
|
||||
files[relative] = item
|
||||
hashed_bytes += item['size']
|
||||
if count % 256 == 0 or count == len(selected):
|
||||
@@ -285,6 +288,7 @@ def build_exclusive_library_manifest(root, *, scope, progress=None):
|
||||
raise OSError(f'LIBRARY_DATABASE_IN_USE: 无法独占读取 {path}:{error}') from error
|
||||
handles[path] = handle
|
||||
stack.callback(handle.Close)
|
||||
note_metadata_progress()
|
||||
|
||||
def digest(path):
|
||||
if path not in handles:
|
||||
@@ -388,6 +392,7 @@ def copy_verified(source, destination, *, scope=None, progress=None, hash_functi
|
||||
|
||||
with ThreadPoolExecutor(max_workers=8) as pool:
|
||||
for count, (relative, digest, label) in enumerate(pool.map(copy_file, files), 1):
|
||||
note_metadata_progress()
|
||||
if label is not None:
|
||||
size = entries[source/relative].st_size
|
||||
record[relative] = {'size': size, 'sha256': digest, 'scope': label}
|
||||
@@ -405,7 +410,8 @@ def copy_verified(source, destination, *, scope=None, progress=None, hash_functi
|
||||
if hash_function(destination/relative) != item['sha256']:
|
||||
raise OSError(f'复制内容校验失败:{path}')
|
||||
with ThreadPoolExecutor(max_workers=8) as pool:
|
||||
list(pool.map(validate_bytes, record))
|
||||
for _ in pool.map(validate_bytes, record):
|
||||
note_metadata_progress()
|
||||
if _metadata_record(source, scan_tree(source)) != expected_metadata:
|
||||
raise OSError(f'复制期间目录内容变化:{source}')
|
||||
if _metadata_record(destination, scan_tree(destination)) != expected_metadata:
|
||||
|
||||
Reference in new issue
Block a user