Watch django server with gunicorn and daphne
pushing to github
2 files changed, +31 -13
+6-4settings.py
| @@ -1,10 +1,12 @@ | ||
| 1 | 1 | from django.conf import settings |
| 2 | 2 | |
| 3 | 3 | |
| 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) | |
| 8 | 10 | |
| 9 | 11 | SMARTWATCH_COLLECT_STATIC = getattr(settings, 'SMARTWATCH_COLLECT_STATIC', True) |
| 10 | 12 | SMARTWATCH_MIGRATE = getattr(settings, 'SMARTWATCH_MIGRATE', True) |
+25-9watch.py
| @@ -1,4 +1,5 @@ | ||
| 1 | 1 | import sys |
| 2 | +import time | |
| 2 | 3 | import psutil |
| 3 | 4 | import logging |
| 4 | 5 | from watchdog.observers import Observer |
| @@ -22,9 +23,9 @@ def kill_gunicorn(): | ||
| 22 | 23 | if 'gunicorn' in process.name(): |
| 23 | 24 | process.kill() |
| 24 | 25 | |
| 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 | |
| 28 | 29 | kill_gunicorn() |
| 29 | 30 | os.system(f'gunicorn -b {hostname}:{port} {get_project_name()}.wsgi:application') |
| 30 | 31 | |
| @@ -35,26 +36,41 @@ def kill_daphne(): | ||
| 35 | 36 | process.kill() |
| 36 | 37 | |
| 37 | 38 | |
| 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 | |
| 41 | 42 | kill_daphne() |
| 42 | 43 | os.system(f'daphne -b {hostname} -p {port} {get_project_name()}.asgi:application') |
| 43 | 44 | |
| 44 | 45 | |
| 45 | 46 | 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() | |
| 48 | 51 | print('started the servers') |
| 49 | 52 | |
| 50 | 53 | |
| 51 | 54 | 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 | + | |
| 52 | 61 | 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 | + | |
| 53 | 67 | should_restart = event.src_path.endswith('.py') |
| 54 | - if event.src_path == 'requirements.txt': | |
| 68 | + if 'requirements.txt' in event.src_path: | |
| 55 | 69 | logging.info(f'{event.src_path} has been modified. Installing requirements...') |
| 56 | 70 | should_restart = True |
| 57 | 71 | os.system('pip install -r requirements.txt') |
| 72 | + if 'templates' in event.src_path: | |
| 73 | + should_restart = True | |
| 58 | 74 | if settings.SMARTWATCH_MIGRATE and 'migrations' in event.src_path: |
| 59 | 75 | should_restart = False |
| 60 | 76 | logging.info(f'{event.src_path} has been modified. Running migrations...') |