fix(logger): add Windows platform support for file locking

This commit is contained in:
cui0711
2026-01-30 16:27:49 +08:00
parent 5463d3bb89
commit 788b248dbc

View File

@@ -7,10 +7,19 @@ Appends task completion results to results.json in real-time.
import json import json
import os import os
import time import time
import fcntl import platform
from pathlib import Path from pathlib import Path
from typing import Dict, Any, Optional from typing import Dict, Any, Optional
# Import fcntl only on Unix-like systems (Linux, macOS)
# On Windows, we'll use msvcrt for file locking
if platform.system() != "Windows":
import fcntl
HAS_FCNTL = True
else:
import msvcrt
HAS_FCNTL = False
def extract_domain_from_path(result_path: str) -> str: def extract_domain_from_path(result_path: str) -> str:
""" """
@@ -66,8 +75,12 @@ def append_task_result(
# Thread-safe JSON append with file locking # Thread-safe JSON append with file locking
try: try:
with open(results_file, 'a+') as f: with open(results_file, 'a+') as f:
# Lock the file for exclusive access # Lock the file for exclusive access (platform-specific)
fcntl.flock(f.fileno(), fcntl.LOCK_EX) if HAS_FCNTL:
fcntl.flock(f.fileno(), fcntl.LOCK_EX)
else:
# Windows file locking using msvcrt
msvcrt.locking(f.fileno(), msvcrt.LK_LOCK, 1)
try: try:
# Move to beginning to read existing content # Move to beginning to read existing content
@@ -95,8 +108,12 @@ def append_task_result(
f.write('\n') # Add newline for readability f.write('\n') # Add newline for readability
finally: finally:
# Always unlock the file # Always unlock the file (platform-specific)
fcntl.flock(f.fileno(), fcntl.LOCK_UN) if HAS_FCNTL:
fcntl.flock(f.fileno(), fcntl.LOCK_UN)
else:
# Windows unlock using msvcrt
msvcrt.locking(f.fileno(), msvcrt.LK_UNLCK, 1)
print(f"📝 Logged result: {domain}/{task_id} -> {result_entry['status']} (score: {score})") print(f"📝 Logged result: {domain}/{task_id} -> {result_entry['status']} (score: {score})")