-
Notifications
You must be signed in to change notification settings - Fork 1
/
watchdog_script.py
41 lines (34 loc) · 1.13 KB
/
watchdog_script.py
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
import subprocess
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
import time
import os
class ChangeHandler(FileSystemEventHandler):
def __init__(self, command):
self.command = command
self.process = None
self.restart_process()
def restart_process(self):
if self.process:
self.process.terminate()
print("\nRestarting server...")
self.process = subprocess.Popen(self.command, shell=True)
def on_modified(self, event):
if event.src_path.endswith('.py'):
print(f"Detected change in {event.src_path}")
self.restart_process()
if __name__ == "__main__":
path = os.path.dirname(os.path.abspath(__file__))
command = "python src/main.py"
# Optional: Add --init flag on first run
# command = "python src/main.py --init"
event_handler = ChangeHandler(command)
observer = Observer()
observer.schedule(event_handler, path, recursive=True)
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()