irongit

Watch django server with gunicorn and daphne

smartwatch/watch.py
96 lines3.1 KBPython
1import sys
2import time
3import psutil
4import logging
5from watchdog.observers import Observer
6from watchdog.events import FileSystemEventHandler
7from django.core.management import call_command
8from django.contrib.staticfiles import finders
9import os
10from multiprocessing import Process
11from . import settings
12
13def get_project_name():
14 settings_module = os.getenv('DJANGO_SETTINGS_MODULE')
15 if settings_module is not None:
16 project_name = settings_module.split('.')[0]
17 return project_name
18 else:
19 return None
20
21def kill_gunicorn():
22 for process in psutil.process_iter():
23 if 'gunicorn' in process.name():
24 process.kill()
25
26def start_gunicorn(hostname=None, port=None):
27 hostname = hostname or settings.SMARTWATCH_GUNICORN_HOST
28 port = port or settings.SMARTWATCH_GUNICORN_PORT
29 kill_gunicorn()
30 os.system(f'gunicorn -b {hostname}:{port} {get_project_name()}.wsgi:application')
31
32
33def kill_daphne():
34 for process in psutil.process_iter():
35 if 'daphne' in process.name():
36 process.kill()
37
38
39def start_daphne(hostname=None, port=None):
40 hostname = hostname or settings.SMARTWATCH_DAPHNE_HOST
41 port = port or settings.SMARTWATCH_DAPHNE_PORT
42 kill_daphne()
43 os.system(f'daphne -b {hostname} -p {port} {get_project_name()}.asgi:application')
44
45
46def start_servers():
47 if settings.SMARTWATCH_USE_GUNICORN:
48 Process(target=start_gunicorn).start()
49 if settings.SMARTWATCH_USE_DAPHNE:
50 Process(target=start_daphne).start()
51 print('started the servers')
52
53
54class 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
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
67 should_restart = event.src_path.endswith('.py')
68 if 'requirements.txt' in event.src_path:
69 logging.info(f'{event.src_path} has been modified. Installing requirements...')
70 should_restart = True
71 os.system('pip install -r requirements.txt')
72 if 'templates' in event.src_path:
73 should_restart = True
74 if settings.SMARTWATCH_MIGRATE and 'migrations' in event.src_path:
75 should_restart = False
76 logging.info(f'{event.src_path} has been modified. Running migrations...')
77 call_command('migrate')
78 if settings.SMARTWATCH_COLLECT_STATIC and 'static' in event.src_path:
79 logging.info(f'{event.src_path} has been modified. Collecting static files...')
80 call_command('collectstatic', '--noinput')
81 if should_restart:
82 logging.info(f'{event.src_path} has been modified. Restarting server...')
83 start_servers()
84
85
86def watch_files():
87 server_handler = ServerHandler()
88 observer = Observer()
89 observer.schedule(server_handler, '.', recursive=True)
90 observer.start()
91 try:
92 while observer.is_alive():
93 observer.join(1)
94 finally:
95 observer.stop()
96 observer.join()