-
Notifications
You must be signed in to change notification settings - Fork 82
/
run_gunicorn.py
executable file
·50 lines (43 loc) · 1.24 KB
/
run_gunicorn.py
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
#!/usr/bin/env python
from contextlib import contextmanager
from os import getenv
from os import putenv
from subprocess import run
from tempfile import TemporaryDirectory
PROMETHEUS_ENV_VAR = "prometheus_multiproc_dir"
LISTEN_PORT = getenv("LISTEN_PORT", 8080)
@contextmanager
def prometheus_temp_dir():
"""
Creates a new temporary folder and sets it to the Prometheus' environment variable.
"""
with TemporaryDirectory() as temp_dir:
orig = getenv(PROMETHEUS_ENV_VAR)
putenv(PROMETHEUS_ENV_VAR, temp_dir)
putenv("BYPASS_RBAC", "true")
putenv("BYPASS_TENANT_TRANSLATION", "true")
try:
yield
finally:
putenv(PROMETHEUS_ENV_VAR, orig)
def run_server():
"""
Runs the Gunicorn server in a new subprocess. This way it gets the new environment
variables.
"""
bind = f"0.0.0.0:{LISTEN_PORT}"
run(
(
"gunicorn",
"--log-level=debug",
f"--bind={bind}",
"--log-file=-",
"--access-logfile=-",
"--reload",
"--worker-class=uvicorn.workers.UvicornWorker",
"run:app",
)
)
if __name__ == "__main__":
with prometheus_temp_dir():
run_server()