-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdaphne_reloader.py
More file actions
executable file
·176 lines (149 loc) · 5.66 KB
/
Copy pathdaphne_reloader.py
File metadata and controls
executable file
·176 lines (149 loc) · 5.66 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/usr/bin/env python
"""
Daphne Auto-Reloader for macOS
Watches for file changes and automatically restarts Daphne server
"""
import os
import sys
import time
import signal
import subprocess
from pathlib import Path
import threading
try:
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
except ImportError:
print("Error: watchdog is not installed.")
print("Please install it with: pip install watchdog")
sys.exit(1)
class DaphneReloader(FileSystemEventHandler):
def __init__(self, command, watch_dirs, ignore_patterns=None):
self.command = command
self.watch_dirs = watch_dirs
self.ignore_patterns = ignore_patterns or [
'*.pyc', '__pycache__', '.git', '.idea', '*.log',
'*.sqlite3', 'staticfiles', 'media', 'node_modules',
'.DS_Store', '*.swp', '*.swo', '*~'
]
self.process = None
self.restart_lock = threading.RLock() # Use RLock for reentrant locking
self.last_restart = 0
self.restart_delay = 1 # Minimum seconds between restarts
# Start the initial process
self.start_process()
def should_ignore(self, path):
"""Check if the file/directory should be ignored"""
path_str = str(path)
for pattern in self.ignore_patterns:
if pattern in path_str:
return True
if pattern.startswith('*') and path_str.endswith(pattern[1:]):
return True
return False
def on_any_event(self, event):
"""Handle file system events"""
# Ignore directory events and certain file patterns
if event.is_directory:
return
if self.should_ignore(event.src_path):
return
# Only react to Python files and templates
valid_extensions = ('.py', '.html', '.css', '.js', '.json')
if not event.src_path.endswith(valid_extensions):
return
# Debounce rapid changes
current_time = time.time()
if current_time - self.last_restart < self.restart_delay:
return
print(f"\n🔄 Detected change in: {event.src_path}")
self.restart_process()
def start_process(self):
"""Start the Daphne process"""
with self.restart_lock:
try:
print(f"🚀 Starting Daphne: {' '.join(self.command)}")
self.process = subprocess.Popen(
self.command,
stdout=sys.stdout,
stderr=sys.stderr,
preexec_fn=os.setsid # Create new process group for proper cleanup
)
self.last_restart = time.time()
print(f"✅ Daphne started with PID: {self.process.pid}")
except Exception as e:
print(f"❌ Failed to start Daphne: {e}")
sys.exit(1)
def stop_process(self):
"""Stop the current Daphne process"""
if self.process:
try:
# Send SIGTERM to the process group
os.killpg(os.getpgid(self.process.pid), signal.SIGTERM)
# Wait for graceful shutdown (max 5 seconds)
try:
self.process.wait(timeout=5)
print("✅ Daphne stopped gracefully")
except subprocess.TimeoutExpired:
# Force kill if it doesn't stop gracefully
os.killpg(os.getpgid(self.process.pid), signal.SIGKILL)
self.process.wait()
print("⚠️ Daphne force stopped")
except ProcessLookupError:
# Process already terminated
pass
except Exception as e:
print(f"⚠️ Error stopping Daphne: {e}")
self.process = None
def restart_process(self):
"""Restart the Daphne process"""
with self.restart_lock:
print("🔄 Restarting Daphne...")
self.stop_process()
time.sleep(0.5) # Brief pause before restart
self.start_process()
def main():
# Parse command line arguments
if len(sys.argv) < 2:
print("Usage: python daphne_reloader.py [daphne command and args]")
print("Example: python daphne_reloader.py daphne dkp.dkp.asgi:application -p 8001")
sys.exit(1)
# Get the Daphne command from arguments
daphne_command = sys.argv[1:]
# Determine directories to watch
current_dir = Path.cwd()
watch_dirs = []
# Add the dkp directory and its subdirectories
dkp_dir = current_dir / 'dkp'
if dkp_dir.exists():
watch_dirs.append(str(dkp_dir))
else:
# Fallback to current directory
watch_dirs.append(str(current_dir))
# Create the reloader
reloader = DaphneReloader(daphne_command, watch_dirs)
# Set up the file watcher
observer = Observer()
for watch_dir in watch_dirs:
observer.schedule(reloader, watch_dir, recursive=True)
print(f"👀 Watching directory: {watch_dir}")
# Start watching
observer.start()
print("\n✨ Daphne auto-reloader is running")
print("Press Ctrl+C to stop\n")
try:
while True:
time.sleep(1)
# Check if process is still running
if reloader.process and reloader.process.poll() is not None:
print("\n⚠️ Daphne process died unexpectedly, restarting...")
reloader.restart_process()
except KeyboardInterrupt:
print("\n\n🛑 Shutting down...")
observer.stop()
reloader.stop_process()
observer.join()
print("👋 Goodbye!")
sys.exit(0)
if __name__ == "__main__":
main()