-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathsite_runner.py
More file actions
55 lines (44 loc) · 1.67 KB
/
Copy pathsite_runner.py
File metadata and controls
55 lines (44 loc) · 1.67 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
"""Supervisor wrapper for one Flask site.
Why this exists: Werkzeug's threaded serve_forever() doesn't honor SIGTERM
(installs / shadows handlers, blocks in C-level select). We can't kill Flask
cleanly via SIGTERM. So we run Flask as a child subprocess of this thin
supervisor, and the supervisor itself is the PID that control_server tracks.
SIGTERM to the supervisor → supervisor SIGKILLs the child → exits.
The supervisor's main loop is os.waitpid() which IS interruptible by signals.
Usage: python3 /opt/site_runner.py <site_name> <port>
"""
import os
import signal
import subprocess
import sys
def main():
site = sys.argv[1]
port = int(sys.argv[2])
# Make this supervisor the session leader so kill_site can SIGKILL the
# whole process group (supervisor + Flask child) via os.killpg(pid).
# subprocess.Popen(..., start_new_session=True) already does this when
# control_server spawns us, but websyn_start.sh's `exec ... &` doesn't,
# so we self-reparent here. OSError is fine — already a leader.
try:
os.setsid()
except OSError:
pass
child = subprocess.Popen(
['python3', '-c',
f"from app import app; "
f"app.run(host='0.0.0.0', port={port}, "
f"debug=False, use_reloader=False, threaded=True)"],
cwd=f'/opt/WebSyn/{site}',
)
def shutdown(signum, frame):
try:
child.kill() # SIGKILL — Flask won't shut down gracefully anyway
except ProcessLookupError:
pass
os._exit(0)
signal.signal(signal.SIGTERM, shutdown)
signal.signal(signal.SIGINT, shutdown)
rc = child.wait()
sys.exit(rc)
if __name__ == '__main__':
main()