-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfileWatcher.py
54 lines (45 loc) · 1.66 KB
/
fileWatcher.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
42
43
44
45
46
47
48
49
50
51
52
53
54
import subprocess
import sys
import time
from watchdog.events import FileSystemEventHandler
from watchdog.observers import Observer
class ChangeHandler(FileSystemEventHandler):
def __init__(self):
self.last_restart = time.time()
def on_any_event(self, event):
if time.time() - self.last_restart < 5: # 10 seconds cooldown
return
if event.is_directory:
return
if event.event_type == "modified" and (
event.src_path.endswith(".py") or event.src_path.endswith(".html")
):
print("Python file change detected, restarting uWSGI...")
subprocess.run(["sudo", "systemctl", "reload", "uwsgi-NatureTech.service"])
self.last_restart = time.time()
# check if the changed file is in the static_workfile folder
if event.event_type == "modified" and event.src_path.startswith(
"/home/butros/NatureTech/static_workfile"
):
print("Static file change detected, restarting nginx...")
subprocess.run(
[
"/home/butros/NatureTech_env/bin/python",
"/home/butros/NatureTech/manage.py",
"collectstatic",
"--noinput",
]
)
self.last_restart = time.time()
if __name__ == "__main__":
path = sys.argv[1] if len(sys.argv) > 1 else "."
event_handler = ChangeHandler()
observer = Observer()
observer.schedule(event_handler, path, recursive=True)
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()