-
Notifications
You must be signed in to change notification settings - Fork 8
/
run.py
71 lines (54 loc) · 1.88 KB
/
run.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from gevent import monkey
monkey.patch_all()
import logging
import multiprocessing
import falcon
import gunicorn.app.base
from app import configure, create_app, start
from app.config import settings
class Application(gunicorn.app.base.BaseApplication):
def __init__(self, app, options=None):
self.options = options or {}
self.application = app
super(Application, self).__init__()
def load_config(self):
config = dict(
[
(key, value)
for key, value in self.options.items()
if key in self.cfg.settings and value is not None
]
)
for key, value in config.items():
self.cfg.set(key.lower(), value)
def load(self):
return self.application
def init_app() -> falcon.App:
configure()
return create_app()
def _post_fork(server=None, w=None):
_config_logging()
start()
def _config_logging():
for logger in "gunicorn.access", "gunicorn.error":
logging.getLogger(logger).propagate = True
logging.getLogger(logger).handlers = []
if __name__ == "__main__":
app = init_app()
env_name = settings.get("ENV_NAME")
default_workers = (multiprocessing.cpu_count() * 2) + 1
opts = {
"accesslog": settings.get("ACCESS_LOG"),
"access_log_format": settings.get("ACCESS_LOG_FORMAT"),
"bind": settings.get("BIND"),
"errorlog": settings.get("ERROR_LOG"),
"keepalive": settings.get("KEEP_ALIVE"),
"post_fork": _post_fork,
"proc_name": settings.get("APP_NAME"),
"max_requests": settings.get("MAX_REQUESTS"),
"max_requests_jitter": settings.get("MAX_REQUESTS_JITTER"),
"worker_class": settings.get("WORKER_CLASS"),
"workers": settings.get("WORKERS")
or (1 if env_name == "LOCAL" else default_workers),
}
Application(app, opts).run()