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 os
import time
import fcntl
import platform
from pathlib import Path
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:
"""
@@ -66,8 +75,12 @@ def append_task_result(
# Thread-safe JSON append with file locking
try:
with open(results_file, 'a+') as f:
# Lock the file for exclusive access
fcntl.flock(f.fileno(), fcntl.LOCK_EX)
# Lock the file for exclusive access (platform-specific)
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:
# Move to beginning to read existing content
@@ -95,8 +108,12 @@ def append_task_result(
f.write('\n') # Add newline for readability
finally:
# Always unlock the file
fcntl.flock(f.fileno(), fcntl.LOCK_UN)
# Always unlock the file (platform-specific)
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})")