@@ -48,28 +48,41 @@ echo $$ > "$PID_FILE"
4848rm -f " $LOCK_FILE "
4949
5050echo " 🔄 Watch-backend started with PID $$ "
51- echo " 🔄 Watching for changes in apps/api/src/*.py"
51+ echo " 🔄 Watching for changes in apps/api/src/**/* .py (recursive) "
5252echo " Press Ctrl+C to stop"
5353
54- # Utilise fswatch sur macOS pour détecter les changements
55- if command -v fswatch & > /dev/null; then
56- echo " ✅ Using fswatch for file monitoring"
57- fswatch -o apps/api/src/* .py | while read num ; do
54+ # Debounce: évite les redémarrages multiples pour des changements rapides
55+ DEBOUNCE_SECONDS=2
56+ LAST_RESTART=0
57+
58+ restart_backend () {
59+ CURRENT_TIME=$( date +%s)
60+ if [ $(( CURRENT_TIME - LAST_RESTART)) -ge $DEBOUNCE_SECONDS ]; then
5861 echo " [$( date ' +%Y-%m-%d %H:%M:%S' ) ] 📝 Change detected, restarting backend..."
5962 docker compose restart backend
6063 echo " [$( date ' +%Y-%m-%d %H:%M:%S' ) ] ✅ Backend restarted"
64+ LAST_RESTART=$CURRENT_TIME
65+ else
66+ echo " [$( date ' +%Y-%m-%d %H:%M:%S' ) ] ⏳ Debouncing, skipping restart"
67+ fi
68+ }
69+
70+ # Utilise fswatch sur macOS pour détecter les changements (récursif)
71+ if command -v fswatch & > /dev/null; then
72+ echo " ✅ Using fswatch for file monitoring (recursive)"
73+ # -r: récursif, -e: exclure, --include: inclure seulement .py
74+ fswatch -r -o --include ' \.py$' --exclude ' .*' apps/api/src | while read num ; do
75+ restart_backend
6176 done
6277else
6378 echo " ⚠️ fswatch not found, using polling mode (less efficient)"
6479 echo " 💡 Install fswatch with: brew install fswatch"
65- # Alternative: utilise find avec polling
80+ # Alternative: utilise find avec polling (récursif)
6681 while true ; do
67- CURRENT_HASH=$( find apps/api/src -name " *.py" -type f -exec md5 {} \; | md5)
82+ CURRENT_HASH=$( find apps/api/src -name " *.py" -type f -exec md5 {} \; 2> /dev/null | md5)
6883 if [ " $LAST_HASH " != " $CURRENT_HASH " ]; then
69- if [ ! -z " $LAST_HASH " ]; then
70- echo " [$( date ' +%Y-%m-%d %H:%M:%S' ) ] 📝 Change detected, restarting backend..."
71- docker compose restart backend
72- echo " [$( date ' +%Y-%m-%d %H:%M:%S' ) ] ✅ Backend restarted"
84+ if [ -n " $LAST_HASH " ]; then
85+ restart_backend
7386 fi
7487 LAST_HASH=$CURRENT_HASH
7588 fi
0 commit comments