-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathOpenStackGPUServer.py
More file actions
109 lines (94 loc) · 3.8 KB
/
OpenStackGPUServer.py
File metadata and controls
109 lines (94 loc) · 3.8 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
""" Commandline Server """ # pylint: disable=invalid-name,abstract-method
import argparse
import datetime
import os
import time
import logging
import gunicorn.app.base
import openapi_server
import openapi_server.controllers
# create simple Logger to updateCache messages to console
LOG = logging.getLogger('updateCache')
HANDLER = logging.StreamHandler()
HANDLER.setLevel(logging.INFO)
formatter = logging.Formatter("[%(asctime)s] [%(process)d] [%(levelname)s] %(message)s", datefmt='%Y-%d-%m %I:%M:%S %z')
HANDLER.setFormatter(formatter)
LOG.addHandler(HANDLER)
LOG.setLevel(logging.DEBUG)
class StandaloneApplication(gunicorn.app.base.BaseApplication):
"""
Customized unicorn application to run gunicorn inside a Python
application.
see https://docs.gunicorn.org/en/stable/custom.html
"""
def __init__(self, app, options=None):
self.options = options or {}
self.application = app
super().__init__()
def load_config(self):
""" overwrite gunicorn.app.base.BaseApplication.load_config() """
config = {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):
""" overwrite gunicorn.app.base.BaseApplication.load() """
return self.application
def update_cache():
""" Helper function. Updates cached data continuously. """
LOG.info("Start updating cached data continuously.")
while True:
start = datetime.datetime.now()
openapi_server.controllers.update_cache()
stop = datetime.datetime.now()
runningtime = (stop-start).seconds
LOG.info("Updating cached data took %d seconds.", runningtime)
sleepingtime = max(0, int(openapi_server.controllers.MEMCACHEEXPIREAFTER/2)-runningtime)
LOG.info("Sleeping for %d seconds.", sleepingtime)
time.sleep(sleepingtime)
if __name__ == '__main__':
PARSER = argparse.ArgumentParser("Run a OpenstackGPUServer.")
PARSER.add_argument("--memcachedHost",
help="Combination of host:port where a memcached server listened.",
type=str,
default="127.0.0.1:11211")
PARSER.add_argument("--memcachedPrefix",
help="Prefix for memcached keys",
type=str,
default='')
PARSER.add_argument("--bind",
help="Bind address for REST API server.",
type=str,
default="127.0.0.1:8080")
PARSER.add_argument("--workers",
help="Number of available Workers ",
type=int,
default=4)
PARSER.add_argument("--timeout",
help="Seconds until gunicorn worker timeouts.",
type=int,
default=300)
ARGS = PARSER.parse_args()
# validate arguments
MEMCACHEDHOST = ARGS.memcachedHost.split(":")
MEMCACHEDPREFIX= ARGS.memcachedPrefix
BIND = ARGS.bind.split(":")
WORKERS = ARGS.workers
TIMEOUT = ARGS.timeout
# configure memcached
openapi_server.controllers.configure_memcache(enabled=True,
host=MEMCACHEDHOST,
expire=TIMEOUT,
prefix=MEMCACHEDPREFIX)
# run gunicorn in a separate child process
if os.fork() == 0:
# child
OPTIONS = {
'bind': f"{BIND[0]}:{BIND[1]}",
'workers': WORKERS,
'timeout' : TIMEOUT
}
StandaloneApplication(openapi_server.app(), OPTIONS).run()
else:
# parent
update_cache()