File size: 5,261 Bytes
343eed9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import os
import signal
import subprocess
import sys

def kill_engine_processes():
    print("SEARCHING Searching for DarkMedia Engine processes...")
    try:
        # Get all python processes with "content creator" or "Engine" in command line
        if sys.platform == "win32":
            import ctypes
            # Filter for python.exe processes that match our project
            # Fast scan using wmic (Avoids CIM/PowerShell hangs)
            cmd = 'wmic process where "name=\'python.exe\'" get ProcessId,CommandLine'
            try:
                output = subprocess.check_output(cmd, shell=True, timeout=10).decode('utf-8', errors='ignore')
            except subprocess.TimeoutExpired:
                print("⚠️ [TIMEOUT] WMIC scan took too long. Force killing all python.exe...")
                os.system('taskkill /F /IM python.exe /T')
                return
            
            lines = [line.strip() for line in output.split('\n') if line.strip() and "CommandLine" not in line]
            
            pids = []
            my_pid = os.getpid()
            parent_pid = os.getppid()
            
            for line in lines:
                lower_line = line.lower()
                # Skip 00_master_pipeline to avoid self-killing
                if "00_master_pipeline.py" in lower_line or "dashboard_server.py" in lower_line:
                    continue
                
                # Match our project directory or engine keywords
                if "content creator" in lower_line or "engine" in lower_line or "pipeline" in lower_line:
                    # Extract the PID (usually at the end of the line in wmic output)
                    parts = line.rsplit(None, 1)
                    if len(parts) > 1 and parts[-1].isdigit():
                        pid = int(parts[-1])
                        if pid != my_pid and pid != parent_pid:
                            pids.append(pid)
                    else:
                        print(f"ℹ️ Skipping protected PID {pid} (Self/Parent)")
            
            if not pids:
                print("CHECK No orphaned Engine processes found.")
                return

            print(f"WARNING Found {len(pids)} processes to terminate: {pids}")
            for pid in pids:
                try:
                    os.kill(pid, signal.SIGTERM)
                    print("X Terminated PID", pid)
                except Exception as e:
                    print(f"   X Failed to kill {pid}: {e}")
                    # Force kill if SIGTERM fails
                    try:
                        os.kill(pid, signal.SIGKILL)
                        print(f"   FIRE Force killed PID {pid}")
                    except: pass
        else:
            # Linux process cleanup
            print("   🔍 Scan des processus orphelins (Linux)...")
            try:
                # On cherche les processus python qui tournent dans le dossier du projet
                # ou qui contiennent des mots clés spécifiques
                cmd = "ps -ef | grep python | grep -E 'engine|pipeline|backend' | grep -v grep"
                output = subprocess.check_output(cmd, shell=True).decode('utf-8', errors='ignore')
                lines = output.split('\n')
                
                my_pid = os.getpid()
                parent_pid = os.getppid()
                
                pids_to_kill = []
                for line in lines:
                    if not line.strip(): continue
                    parts = line.split()
                    if len(parts) < 2: continue
                    
                    pid = int(parts[1])
                    cmdline = " ".join(parts[7:])
                    
                    # Ne pas se tuer soi-même ni le parent (master_pipeline)
                    if pid == my_pid or pid == parent_pid:
                        continue
                        
                    # Protection spécifique pour le serveur dashboard et le master pipeline
                    if "00_master_pipeline.py" in cmdline or "main.py" in cmdline:
                        continue
                        
                    pids_to_kill.append(pid)
                
                # On cherche aussi Blender
                try:
                    blender_pids = subprocess.check_output(["pgrep", "blender"]).decode().split()
                    for bpid in blender_pids:
                        pids_to_kill.append(int(bpid))
                except: pass

                if pids_to_kill:
                    print(f"   ⚠️ Nettoyage de {len(pids_to_kill)} processus : {pids_to_kill}")
                    for pid in pids_to_kill:
                        try:
                            os.kill(pid, signal.SIGKILL)
                            print(f"      ✅ tué PID {pid}")
                        except: pass
                else:
                    print("   ✅ Aucun processus orphelin détecté.")
                    
            except Exception as e:
                print(f"   ℹ️ Rien à nettoyer ou erreur légère: {e}")
            
    except Exception as e:
        print(f"❌ Error during cleanup: {e}")

if __name__ == "__main__":
    kill_engine_processes()
    print("\n✨ Cleanup finished.")