修复:独立回收中断备份并限制 AI 日志增长
This commit is contained in:
@@ -4,6 +4,7 @@ from datetime import datetime, timezone
|
||||
from functools import wraps
|
||||
import hashlib
|
||||
import json
|
||||
import math
|
||||
import os
|
||||
from pathlib import Path
|
||||
import re
|
||||
@@ -166,16 +167,9 @@ def _rotate(root, selected):
|
||||
for folder in base.iterdir():
|
||||
if folder == selected or folder.is_symlink() or not folder.is_dir() or not ID.fullmatch(folder.name):
|
||||
continue
|
||||
# A restore-verified older folder is safe to retire under the shared lock.
|
||||
# Incomplete candidates are kept for 24h and never treated as good backups.
|
||||
# 旧有效备份仅在新有效备份发布后退役;未完成候选使用独立的所有权检查。
|
||||
older = verified(folder, checksums=False)
|
||||
try:
|
||||
owner = json.loads((folder / '.owner.json').read_text())
|
||||
owned = owner.get('kind') == 'quality-backup' and owner.get('root') == str(root) and owner.get('id') == folder.name
|
||||
except (OSError, ValueError):
|
||||
owned = False
|
||||
stale = owned and time.time() - folder.stat().st_mtime > 86400
|
||||
if older is not None or stale:
|
||||
if older is not None:
|
||||
contents = list(folder.rglob('*'))
|
||||
if any(p.is_symlink() for p in contents):
|
||||
continue
|
||||
@@ -204,6 +198,7 @@ def _rotate(root, selected):
|
||||
retire_folder(root,legacy,'legacy')
|
||||
legacy_info['retired']=True
|
||||
atomic(registration,legacy_info)
|
||||
clean_incomplete(root)
|
||||
clean_retired(root)
|
||||
return pointer, removed
|
||||
|
||||
@@ -282,6 +277,75 @@ def backup_ids(root):
|
||||
return {p.name for p in base.iterdir()} if base.exists() else set()
|
||||
|
||||
|
||||
def register_candidate(folder, root):
|
||||
import psutil
|
||||
folder = Path(folder); info = folder.stat()
|
||||
atomic(folder / '.owner.json', {'kind': 'quality-backup', 'formatVersion': 2,
|
||||
'root': str(Path(root).resolve()), 'id': folder.name, 'createdAt': time.time(),
|
||||
'ownerPid': os.getpid(), 'ownerCreated': psutil.Process().create_time(),
|
||||
'directoryIdentity': [info.st_dev, info.st_ino], 'children': [], 'childPending': False})
|
||||
|
||||
|
||||
def candidate_child(folder, pid=None, pending=False):
|
||||
import psutil
|
||||
path = Path(folder) / '.owner.json'; value = json.loads(path.read_text())
|
||||
if value['ownerPid'] != os.getpid(): raise ValueError('Candidate owner changed')
|
||||
value['childPending'] = pending
|
||||
if pid is not None:
|
||||
value['children'].append({'pid': pid, 'created': psutil.Process(pid).create_time()})
|
||||
atomic(path, value)
|
||||
|
||||
|
||||
def clean_incomplete(root, now=None):
|
||||
"""独立回收超过24小时的已登记、已退出候选;不依赖新备份或业务库可用。"""
|
||||
import psutil
|
||||
root = Path(root).resolve(); now = time.time() if now is None else now
|
||||
result = {'incompleteBackupsRemoved': 0, 'incompleteBackupBytesRemoved': 0, 'incompleteBackupsDeferred': 0}
|
||||
base = root / 'output/backups'
|
||||
if any(p.is_symlink() for p in (root / 'output', base)): raise ValueError('Unsafe backup root')
|
||||
def exited(item, pid_key, created_key):
|
||||
if (not isinstance(item.get(pid_key), int) or item[pid_key] <= 1 or
|
||||
not isinstance(item.get(created_key), (float, int)) or
|
||||
not math.isfinite(item[created_key]) or item[created_key] <= 0): return False
|
||||
try:
|
||||
process = psutil.Process(item[pid_key])
|
||||
return (abs(process.create_time() - item[created_key]) >= .02 or
|
||||
not process.is_running() or process.status() == psutil.STATUS_ZOMBIE)
|
||||
except psutil.NoSuchProcess: return True
|
||||
except psutil.Error: return False
|
||||
with lock(root, timeout=0):
|
||||
pointer = root / '.runtime/retention/latest.json'
|
||||
try:
|
||||
if pointer.is_symlink(): return result
|
||||
protected = json.loads(pointer.read_text())['id'] if pointer.exists() else None
|
||||
except (OSError, ValueError, KeyError, TypeError): return result
|
||||
for folder in base.iterdir() if base.exists() else []:
|
||||
if folder.name == protected or folder.is_symlink() or not folder.is_dir() or not ID.fullmatch(folder.name): continue
|
||||
owner = folder / '.owner.json'
|
||||
if owner.is_symlink(): continue
|
||||
try:
|
||||
value = json.loads(owner.read_text()); info = folder.stat()
|
||||
if not isinstance(value, dict): continue
|
||||
if (value.get('kind') != 'quality-backup' or value.get('root') != str(root) or
|
||||
value.get('id') != folder.name or not isinstance(value.get('createdAt'), (int, float)) or
|
||||
not math.isfinite(value['createdAt']) or now - max(value['createdAt'], info.st_mtime) < 86400): continue
|
||||
if verified(folder, checksums=False) is not None: continue
|
||||
if (value.get('formatVersion') != 2 or value.get('directoryIdentity') != [info.st_dev, info.st_ino] or
|
||||
value.get('childPending') is not False or not isinstance(value.get('children'), list) or
|
||||
not exited(value, 'ownerPid', 'ownerCreated') or
|
||||
not all(isinstance(p, dict) and exited(p, 'pid', 'created') for p in value['children'])):
|
||||
result['incompleteBackupsDeferred'] += 1; continue
|
||||
contents = list(folder.rglob('*'))
|
||||
if any(p.is_symlink() for p in contents): continue
|
||||
size = sum(p.stat().st_size for p in contents if p.is_file())
|
||||
if folder.stat().st_ino != info.st_ino or json.loads(owner.read_text()) != value: continue
|
||||
retire_folder(root, folder, 'backup')
|
||||
result['incompleteBackupsRemoved'] += 1; result['incompleteBackupBytesRemoved'] += size
|
||||
except (OSError, ValueError, TypeError): continue
|
||||
clean_retired(root)
|
||||
return result
|
||||
|
||||
|
||||
def discard_failed_new(root, before):
|
||||
"""Delete only this call's registered, incomplete candidate after it has stopped."""
|
||||
root=Path(root).resolve();base=root/'output/backups'
|
||||
|
||||
Reference in New Issue
Block a user