Work on fixing bugs and reformat
This commit is contained in:
@@ -24,12 +24,13 @@ management command), you can "touch" any file under the watched path:
|
||||
The watcher ignores `__pycache__`, `.pyc` files and `.git` paths.
|
||||
"""
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
from watchdog.observers import Observer
|
||||
|
||||
from watchdog.events import FileSystemEventHandler
|
||||
from watchdog.observers import Observer
|
||||
|
||||
|
||||
class ChangeHandler(FileSystemEventHandler):
|
||||
@@ -48,7 +49,7 @@ class ChangeHandler(FileSystemEventHandler):
|
||||
|
||||
def _check_and_restart(self, path):
|
||||
# Ignore pycache and compiled files
|
||||
if '__pycache__' in path or '.pyc' in path or '.git' in path:
|
||||
if "__pycache__" in path or ".pyc" in path or ".git" in path:
|
||||
return
|
||||
|
||||
now = time.time()
|
||||
@@ -62,13 +63,16 @@ class ChangeHandler(FileSystemEventHandler):
|
||||
|
||||
def _restart_ur(self):
|
||||
# Determine target container from environment (default `ur_gia`)
|
||||
target = os.environ.get('TARGET_CONTAINER', 'ur_gia')
|
||||
target = os.environ.get("TARGET_CONTAINER", "ur_gia")
|
||||
print(f'[{time.strftime("%H:%M:%S")}] Restarting {target}...', flush=True)
|
||||
# Try podman first (preferred in this setup), then docker
|
||||
cmd = f"podman restart {target} 2>/dev/null || docker restart {target} 2>/dev/null"
|
||||
result = subprocess.run(cmd, shell=True, capture_output=True)
|
||||
if result.returncode == 0:
|
||||
print(f'[{time.strftime("%H:%M:%S")}] {target} restarted successfully', flush=True)
|
||||
print(
|
||||
f'[{time.strftime("%H:%M:%S")}] {target} restarted successfully',
|
||||
flush=True,
|
||||
)
|
||||
else:
|
||||
print(f'[{time.strftime("%H:%M:%S")}] {target} restart failed', flush=True)
|
||||
time.sleep(1)
|
||||
@@ -80,17 +84,20 @@ def main():
|
||||
|
||||
# Allow overriding watched paths via environment variable `WATCH_PATHS`.
|
||||
# Default is `/code/core,/code/app` but you can set e.g. `WATCH_PATHS=/code/core`
|
||||
watch_paths_env = os.environ.get('WATCH_PATHS', '/code/core,/code/app')
|
||||
watch_paths = [p.strip() for p in watch_paths_env.split(',') if p.strip()]
|
||||
watch_paths_env = os.environ.get("WATCH_PATHS", "/code/core,/code/app")
|
||||
watch_paths = [p.strip() for p in watch_paths_env.split(",") if p.strip()]
|
||||
for path in watch_paths:
|
||||
if os.path.exists(path):
|
||||
observer.schedule(handler, path, recursive=True)
|
||||
print(f'Watching: {path}', flush=True)
|
||||
print(f"Watching: {path}", flush=True)
|
||||
else:
|
||||
print(f'Not found (will not watch): {path}', flush=True)
|
||||
print(f"Not found (will not watch): {path}", flush=True)
|
||||
|
||||
observer.start()
|
||||
print(f'[{time.strftime("%H:%M:%S")}] File watcher started. Monitoring for changes...', flush=True)
|
||||
print(
|
||||
f'[{time.strftime("%H:%M:%S")}] File watcher started. Monitoring for changes...',
|
||||
flush=True,
|
||||
)
|
||||
|
||||
try:
|
||||
while True:
|
||||
@@ -100,5 +107,5 @@ def main():
|
||||
observer.join()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
@@ -15,9 +15,9 @@ Touching a file under the watched path will trigger a restart of the target
|
||||
container; e.g. `touch /code/core/__restart__` will cause the watcher to act.
|
||||
"""
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
import subprocess
|
||||
|
||||
|
||||
def get_mtime(path):
|
||||
@@ -25,9 +25,9 @@ def get_mtime(path):
|
||||
max_mtime = 0
|
||||
for root, dirs, files in os.walk(path):
|
||||
# Skip pycache and hidden dirs
|
||||
dirs[:] = [d for d in dirs if not d.startswith('.') and d != '__pycache__']
|
||||
dirs[:] = [d for d in dirs if not d.startswith(".") and d != "__pycache__"]
|
||||
for file in files:
|
||||
if file.endswith(('.pyc', '.pyo')):
|
||||
if file.endswith((".pyc", ".pyo")):
|
||||
continue
|
||||
try:
|
||||
mtime = os.path.getmtime(os.path.join(root, file))
|
||||
@@ -39,9 +39,9 @@ def get_mtime(path):
|
||||
|
||||
def restart_ur():
|
||||
"""Restart target container (defaults to `ur_gia`)."""
|
||||
target = os.environ.get('TARGET_CONTAINER', 'ur_gia')
|
||||
target = os.environ.get("TARGET_CONTAINER", "ur_gia")
|
||||
print(f'[{time.strftime("%H:%M:%S")}] Restarting {target}...', flush=True)
|
||||
cmd = f'podman restart {target} 2>/dev/null || docker restart {target} 2>/dev/null'
|
||||
cmd = f"podman restart {target} 2>/dev/null || docker restart {target} 2>/dev/null"
|
||||
result = subprocess.run(cmd, shell=True, capture_output=True)
|
||||
if result.returncode == 0:
|
||||
print(f'[{time.strftime("%H:%M:%S")}] {target} restarted', flush=True)
|
||||
@@ -53,16 +53,16 @@ def main():
|
||||
# In the container the repository is mounted at /code
|
||||
# Allow overriding watched paths via environment variable `WATCH_PATHS`.
|
||||
# Default is `/code/core,/code/app`.
|
||||
paths_env = os.environ.get('WATCH_PATHS', '/code/core,/code/app')
|
||||
paths = [p.strip() for p in paths_env.split(',') if p.strip()]
|
||||
paths_env = os.environ.get("WATCH_PATHS", "/code/core,/code/app")
|
||||
paths = [p.strip() for p in paths_env.split(",") if p.strip()]
|
||||
last_mtimes = {}
|
||||
|
||||
for path in paths:
|
||||
if os.path.exists(path):
|
||||
print(f'Watching: {path}', flush=True)
|
||||
print(f"Watching: {path}", flush=True)
|
||||
last_mtimes[path] = get_mtime(path)
|
||||
else:
|
||||
print(f'Not found: {path}', flush=True)
|
||||
print(f"Not found: {path}", flush=True)
|
||||
|
||||
print(f'[{time.strftime("%H:%M:%S")}] Watcher started', flush=True)
|
||||
restart_debounce = 0
|
||||
@@ -77,15 +77,17 @@ def main():
|
||||
continue
|
||||
current_mtime = get_mtime(path)
|
||||
if current_mtime > last_mtimes.get(path, 0):
|
||||
print(f'[{time.strftime("%H:%M:%S")}] Changes in {path}', flush=True)
|
||||
print(
|
||||
f'[{time.strftime("%H:%M:%S")}] Changes in {path}', flush=True
|
||||
)
|
||||
last_mtimes[path] = current_mtime
|
||||
if restart_debounce <= 0:
|
||||
restart_ur()
|
||||
restart_debounce = 5 # Don't restart more than every 5s
|
||||
except KeyboardInterrupt:
|
||||
print('Watcher stopped', flush=True)
|
||||
print("Watcher stopped", flush=True)
|
||||
sys.exit(0)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user