-
Notifications
You must be signed in to change notification settings - Fork 271
Expand file tree
/
Copy pathmanage.py
More file actions
45 lines (41 loc) · 1.99 KB
/
Copy pathmanage.py
File metadata and controls
45 lines (41 loc) · 1.99 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
#!/usr/bin/env python
import os
import signal
import sys
# OpenBench.apps registers an atexit shutdown() that gracefully stops the PGN
# watcher thread -- joining it so any in-flight write is allowed to finish.
# atexit only runs on a clean interpreter exit, not on a bare SIGTERM, so we
# install a SIGTERM handler that turns the kill signal into a clean sys.exit()
# and thereby triggers that atexit shutdown().
#
# signal.signal() may only be called from the main thread, and ready() (where
# the watcher is spawned) is not guaranteed to run there. So the handler is
# installed at the two process entry points instead, each of which IS the main
# thread of its own process:
#
# manage.py -> the entry point for `runserver` (here)
# OpenSite/wsgi.py -> the module gunicorn imports per worker (the gunicorn case)
#
# Whichever entry point starts the process installs the handler; the other
# no-ops for that run (under runserver, wsgi.py is imported off the main thread
# and skips itself). Keep this block and the handler in sync across both files.
#
# Ignore any further SIGTERMs so the atexit shutdown runs to completion. A
# `pkill -TERM gunicorn` hits each worker directly AND the arbiter, which
# then broadcasts a second SIGTERM to every worker; without this the second
# signal would re-enter here and abort the shutdown partway through.
def graceful_exit_on_sigterm(*args):
signal.signal(signal.SIGTERM, signal.SIG_IGN)
sys.exit(0)
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "OpenSite.settings")
signal.signal(signal.SIGTERM, graceful_exit_on_sigterm)
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)