better testcase managment, script fix

This commit is contained in:
Dobromir Popov
2025-06-26 17:51:48 +03:00
parent 1f47576723
commit 99386dbc50
3 changed files with 296 additions and 81 deletions

View File

@ -11,9 +11,27 @@ import sys
import time
import signal
from pathlib import Path
import threading
# Global timeout flag
timeout_reached = False
def timeout_handler():
"""Handler for overall script timeout"""
global timeout_reached
timeout_reached = True
print("\n⚠️ WARNING: Script timeout reached (10s) - forcing exit")
os._exit(0) # Force exit
def kill_stale_processes():
"""Kill stale trading dashboard processes safely"""
global timeout_reached
# Set up overall timeout (10 seconds)
timer = threading.Timer(10.0, timeout_handler)
timer.daemon = True
timer.start()
try:
import psutil
except ImportError:
@ -33,9 +51,15 @@ def kill_stale_processes():
try:
print("Scanning for stale processes...")
# Get all Python processes
# Get all Python processes with timeout
python_processes = []
scan_start = time.time()
for proc in psutil.process_iter(['pid', 'name', 'cmdline']):
if timeout_reached or (time.time() - scan_start) > 3.0: # 3s max for scanning
print("Process scanning timeout - proceeding with found processes")
break
try:
if proc.info['pid'] == current_pid:
continue
@ -58,15 +82,24 @@ def kill_stale_processes():
if not python_processes:
print("No stale processes found")
timer.cancel() # Cancel the timeout
return True
print(f"Found {len(python_processes)} target processes to terminate:")
for p in python_processes:
for p in python_processes[:5]: # Show max 5 to save time
print(f" - PID {p['pid']}: {p['name']} - {p['cmdline'][:80]}...")
if len(python_processes) > 5:
print(f" ... and {len(python_processes) - 5} more")
# Graceful termination first
# Graceful termination first (with reduced wait time)
print("\nAttempting graceful termination...")
termination_start = time.time()
for p in python_processes:
if timeout_reached or (time.time() - termination_start) > 2.0:
print("Termination timeout - moving to force kill")
break
try:
proc = p['proc']
if proc.is_running():
@ -75,12 +108,18 @@ def kill_stale_processes():
except Exception as e:
failed_processes.append(f"Failed to terminate PID {p['pid']}: {e}")
# Wait for graceful shutdown
time.sleep(2.0)
# Wait for graceful shutdown (reduced from 2.0 to 1.0)
time.sleep(1.0)
# Force kill remaining processes
print("\nChecking for remaining processes...")
kill_start = time.time()
for p in python_processes:
if timeout_reached or (time.time() - kill_start) > 2.0:
print("Force kill timeout - exiting")
break
try:
proc = p['proc']
if proc.is_running():
@ -94,23 +133,18 @@ def kill_stale_processes():
except Exception as e:
failed_processes.append(f"Failed to kill PID {p['pid']}: {e}")
# Results
print(f"\n=== Process Cleanup Results ===")
if killed_processes:
print(f"Successfully cleaned up {len(killed_processes)} processes:")
for msg in killed_processes:
print(f"{msg}")
# Results (quick summary)
print(f"\n=== Quick Results ===")
print(f"✓ Cleaned up {len(killed_processes)} processes")
if failed_processes:
print(f"\nFailed to clean up {len(failed_processes)} processes:")
for msg in failed_processes:
print(f"{msg}")
print(f"Failed: {len(failed_processes)} processes")
print(f"\nCleanup completed. {len(killed_processes)} processes terminated.")
timer.cancel() # Cancel the timeout if we finished early
return len(failed_processes) == 0
except Exception as e:
print(f"Error during process cleanup: {e}")
timer.cancel()
return False
def kill_stale_fallback():
@ -120,10 +154,10 @@ def kill_stale_fallback():
try:
if os.name == 'nt': # Windows
import subprocess
# Kill Python processes with dashboard keywords
# Kill Python processes with dashboard keywords (with timeout)
result = subprocess.run([
'taskkill', '/f', '/im', 'python.exe'
], capture_output=True, text=True)
], capture_output=True, text=True, timeout=5.0)
if result.returncode == 0:
print("Windows: Killed all Python processes")
@ -132,25 +166,32 @@ def kill_stale_fallback():
else: # Unix/Linux
import subprocess
# More targeted approach for Unix
subprocess.run(['pkill', '-f', 'dashboard'], capture_output=True)
subprocess.run(['pkill', '-f', 'scalping'], capture_output=True)
subprocess.run(['pkill', '-f', 'tensorboard'], capture_output=True)
# More targeted approach for Unix (with timeouts)
subprocess.run(['pkill', '-f', 'dashboard'], capture_output=True, timeout=2.0)
subprocess.run(['pkill', '-f', 'scalping'], capture_output=True, timeout=2.0)
subprocess.run(['pkill', '-f', 'tensorboard'], capture_output=True, timeout=2.0)
print("Unix: Killed dashboard-related processes")
return True
except subprocess.TimeoutExpired:
print("Fallback method timed out")
return False
except Exception as e:
print(f"Fallback method failed: {e}")
return False
if __name__ == "__main__":
print("=" * 50)
print("STALE PROCESS CLEANUP")
print("STALE PROCESS CLEANUP (10s timeout)")
print("=" * 50)
start_time = time.time()
success = kill_stale_processes()
elapsed = time.time() - start_time
exit_code = 0 if success else 1
print(f"Completed in {elapsed:.1f}s")
print("=" * 50)
sys.exit(exit_code)
sys.exit(exit_code)