Create watchdog.py
Browse files- watchdog.py +27 -0
watchdog.py
ADDED
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import subprocess
|
2 |
+
import time
|
3 |
+
|
4 |
+
RESTART_DELAY = 5 # seconds
|
5 |
+
|
6 |
+
# Define the commands for both entrypoints
|
7 |
+
commands = [
|
8 |
+
["python", "-m", "DragMusic"],
|
9 |
+
["python", "server.py"]
|
10 |
+
]
|
11 |
+
|
12 |
+
processes = [None, None]
|
13 |
+
|
14 |
+
while True:
|
15 |
+
# Start any process that isn't running
|
16 |
+
for i, cmd in enumerate(commands):
|
17 |
+
if processes[i] is None or processes[i].poll() is not None:
|
18 |
+
print(f"[Watchdog] Starting: {' '.join(cmd)}")
|
19 |
+
processes[i] = subprocess.Popen(cmd)
|
20 |
+
time.sleep(1) # Stagger startups slightly
|
21 |
+
# Check every second if any process has exited
|
22 |
+
time.sleep(1)
|
23 |
+
for i, proc in enumerate(processes):
|
24 |
+
if proc.poll() is not None:
|
25 |
+
print(f"[Watchdog] Process {' '.join(commands[i])} exited. Restarting in {RESTART_DELAY} seconds...")
|
26 |
+
time.sleep(RESTART_DELAY)
|
27 |
+
processes[i] = None
|