irongit

Watch django server with gunicorn and daphne

pushing to github

huncholanehuncholaneauthored
parent 043f808commit 1e894b5ebbec754645ee28c66083130dc2736f6aBrowse files

2 files changed, +31 -13

+6-4settings.py
@@ -1,10 +1,12 @@
11 from django.conf import settings
22
33
4-SMARTWATCH_HTTP_PORT = getattr(settings, 'SMARTWATCH_HTTP_PORT', 8000)
5-SMARTWATCH_HTTP_HOST = getattr(settings, 'SMARTWATCH_HTTP_HOST', '0.0.0.0')
6-SMARTWATCH_WS_PORT = getattr(settings, 'SMARTWATCH_WS_PORT', 8001)
7-SMARTWATCH_WS_HOST = getattr(settings, 'SMARTWATCH_WS_HOST', '0.0.0.0')
4+SMARTWATCH_GUNICORN_PORT = getattr(settings, 'SMARTWATCH_GUNICORN_PORT', 8000)
5+SMARTWATCH_GUNICORN_HOST = getattr(settings, 'SMARTWATCH_GUNICORN_HOST', '0.0.0.0')
6+SMARTWATCH_USE_GUNICORN = getattr(settings, 'SMARTWATCH_USE_GUNICORN', True)
7+SMARTWATCH_DAPHNE_PORT = getattr(settings, 'SMARTWATCH_DAPHNE_PORT', 8001)
8+SMARTWATCH_DAPHNE_HOST = getattr(settings, 'SMARTWATCH_DAPHNE_HOST', '0.0.0.0')
9+SMARTWATCH_USE_DAPHNE = getattr(settings, 'SMARTWATCH_USE_DAPHNE', True)
810
911 SMARTWATCH_COLLECT_STATIC = getattr(settings, 'SMARTWATCH_COLLECT_STATIC', True)
1012 SMARTWATCH_MIGRATE = getattr(settings, 'SMARTWATCH_MIGRATE', True)
+25-9watch.py
@@ -1,4 +1,5 @@
11 import sys
2+import time
23 import psutil
34 import logging
45 from watchdog.observers import Observer
@@ -22,9 +23,9 @@ def kill_gunicorn():
2223 if 'gunicorn' in process.name():
2324 process.kill()
2425
25-def run_gunicorn_http(hostname=None, port=None):
26- hostname = hostname or settings.SMARTWATCH_HTTP_HOST
27- port = port or settings.SMARTWATCH_HTTP_PORT
26+def start_gunicorn(hostname=None, port=None):
27+ hostname = hostname or settings.SMARTWATCH_GUNICORN_HOST
28+ port = port or settings.SMARTWATCH_GUNICORN_PORT
2829 kill_gunicorn()
2930 os.system(f'gunicorn -b {hostname}:{port} {get_project_name()}.wsgi:application')
3031
@@ -35,26 +36,41 @@ def kill_daphne():
3536 process.kill()
3637
3738
38-def run_daphne_ws(hostname=None, port=None):
39- hostname = hostname or settings.SMARTWATCH_WS_HOST
40- port = port or settings.SMARTWATCH_WS_PORT
39+def start_daphne(hostname=None, port=None):
40+ hostname = hostname or settings.SMARTWATCH_DAPHNE_HOST
41+ port = port or settings.SMARTWATCH_DAPHNE_PORT
4142 kill_daphne()
4243 os.system(f'daphne -b {hostname} -p {port} {get_project_name()}.asgi:application')
4344
4445
4546 def start_servers():
46- Process(target=run_gunicorn_http).start()
47- # Process(target=run_daphne_ws).start()
47+ if settings.SMARTWATCH_USE_GUNICORN:
48+ Process(target=start_gunicorn).start()
49+ if settings.SMARTWATCH_USE_DAPHNE:
50+ Process(target=start_daphne).start()
4851 print('started the servers')
4952
5053
5154 class ServerHandler(FileSystemEventHandler):
55+ DEBOUNCE_SECONDS = 1
56+
57+ def __init__(self, *args, **kwargs):
58+ super().__init__(*args, **kwargs)
59+ self.last_modified = time.time()
60+
5261 def on_modified(self, event):
62+ current_time = time.time()
63+ if current_time - self.last_modified < self.DEBOUNCE_SECONDS:
64+ return
65+ self.last_modified = current_time
66+
5367 should_restart = event.src_path.endswith('.py')
54- if event.src_path == 'requirements.txt':
68+ if 'requirements.txt' in event.src_path:
5569 logging.info(f'{event.src_path} has been modified. Installing requirements...')
5670 should_restart = True
5771 os.system('pip install -r requirements.txt')
72+ if 'templates' in event.src_path:
73+ should_restart = True
5874 if settings.SMARTWATCH_MIGRATE and 'migrations' in event.src_path:
5975 should_restart = False
6076 logging.info(f'{event.src_path} has been modified. Running migrations...')