From 5c6f4a7098b4742ed847f7a5ebfb327090a7e0d0 Mon Sep 17 00:00:00 2001 From: Jan Krueger Date: Tue, 13 Aug 2024 14:37:03 +0200 Subject: [PATCH 1/2] fix #14 and #15 --- openapi_server/controllers/__init__.py | 20 +++++++++++--------- requirements.txt | 2 +- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/openapi_server/controllers/__init__.py b/openapi_server/controllers/__init__.py index 2f0e8c6..49c407d 100644 --- a/openapi_server/controllers/__init__.py +++ b/openapi_server/controllers/__init__.py @@ -35,19 +35,21 @@ def enable_memcache(enabled=True): MEMCACHE = enabled -def configure_memcache(enabled=True, host="127.0.0.1:11211", expire=300): +def configure_memcache(enabled=True, host="127.0.0.1:11211", expire=300, prefix=""): """ Configure memcache. :param enabled: Enabled memcached support, defaults to True :param host: memcached host as string ":", defaults to "127.0.0.1:11211" :param expire : time in seconds after cache expires, default to 300 + :param prefix : optional memcached key prefix, default to "" :return: None """ - global MEMCACHE, MEMCACHEDHOST, MEMCACHEDCLIENT, MEMCACHEEXPIREAFTER # pylint: disable=W0603 + global MEMCACHE, MEMCACHEDHOST, MEMCACHEDCLIENT, MEMCACHEEXPIREAFTER, MEMCACHEDPREFIX # pylint: disable=W0603 MEMCACHE = enabled MEMCACHEDHOST = host MEMCACHEEXPIREAFTER = expire MEMCACHEDCLIENT = MemCachedClient(MEMCACHEDHOST, serde=SerDe()) + MEMCACHEDPREFIX = prefix def get_flavors(): @@ -63,16 +65,16 @@ def get_flavors(): timestamp = datetime.now() if MEMCACHE: # check if memcached contains a list of flavors - flavors = MEMCACHEDCLIENT.get('FlavorGPU') - if MEMCACHEDCLIENT.get('FlavorGPU.timestamp'): - timestamp = datetime.strptime(MEMCACHEDCLIENT.get('FlavorGPU.timestamp'), '%Y-%m-%d %H:%M:%S') + flavors = MEMCACHEDCLIENT.get(MEMCACHEDPREFIX+'FlavorGPU') + if MEMCACHEDCLIENT.get(MEMCACHEDPREFIX+'FlavorGPU.timestamp'): + timestamp = datetime.strptime(MEMCACHEDCLIENT.get(MEMCACHEDPREFIX+'FlavorGPU.timestamp'), '%Y-%m-%d %H:%M:%S') if not flavors: flavors = __get_flavors_as_json_() if MEMCACHE: # update memcached - MEMCACHEDCLIENT.set("FlavorGPU", flavors, MEMCACHEEXPIREAFTER) - MEMCACHEDCLIENT.set("FlavorGPU.timestamp", timestamp.strftime('%Y-%m-%d %H:%M:%S'), MEMCACHEEXPIREAFTER) + MEMCACHEDCLIENT.set(MEMCACHEDPREFIX+"FlavorGPU", flavors, MEMCACHEEXPIREAFTER) + MEMCACHEDCLIENT.set(MEMCACHEDPREFIX+"FlavorGPU.timestamp", timestamp.strftime('%Y-%m-%d %H:%M:%S'), MEMCACHEEXPIREAFTER) return {"flavors": flavors, "timestamp": timestamp.strftime('%Y-%m-%d %H:%M:%S')} @@ -97,5 +99,5 @@ def update_cache(): Updates the cached data stored in the configured MemCached service. :return: None """ - MEMCACHEDCLIENT.set("FlavorGPU", __get_flavors_as_json_(), MEMCACHEEXPIREAFTER) - MEMCACHEDCLIENT.set("FlavorGPU.timestamp", datetime.now().strftime('%Y-%m-%d %H:%M:%S'), MEMCACHEEXPIREAFTER) + MEMCACHEDCLIENT.set(MEMCACHEDPREFIX+"FlavorGPU", __get_flavors_as_json_(), MEMCACHEEXPIREAFTER) + MEMCACHEDCLIENT.set(MEMCACHEDPREFIX+"FlavorGPU.timestamp", datetime.now().strftime('%Y-%m-%d %H:%M:%S'), MEMCACHEEXPIREAFTER) diff --git a/requirements.txt b/requirements.txt index b45e284..f3794bf 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,4 @@ -connexion[swagger-ui] >= 2.6.0; python_version>="3.6" +connexion[swagger-ui] == 2.14.2 swagger-ui-bundle >= 0.0.2 python_dateutil >= 2.6.0 setuptools >= 21.0.0 From cb78902ec9b2bf3641facac54b8233e5b737aae3 Mon Sep 17 00:00:00 2001 From: Jan Krueger Date: Tue, 13 Aug 2024 15:36:06 +0200 Subject: [PATCH 2/2] adjust cli arguments --- OpenStackGPUServer.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/OpenStackGPUServer.py b/OpenStackGPUServer.py index 64de2f8..b6c02f7 100644 --- a/OpenStackGPUServer.py +++ b/OpenStackGPUServer.py @@ -63,8 +63,12 @@ def update_cache(): 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="Update cache only, implies option --memcached and forces updating the cache.", + help="Bind address for REST API server.", type=str, default="127.0.0.1:8080") PARSER.add_argument("--workers", @@ -80,6 +84,7 @@ def update_cache(): # validate arguments MEMCACHEDHOST = ARGS.memcachedHost.split(":") + MEMCACHEDPREFIX= ARGS.memcachedPrefix BIND = ARGS.bind.split(":") WORKERS = ARGS.workers TIMEOUT = ARGS.timeout @@ -87,7 +92,8 @@ def update_cache(): # configure memcached openapi_server.controllers.configure_memcache(enabled=True, host=MEMCACHEDHOST, - expire=300) + expire=TIMEOUT, + prefix=MEMCACHEDPREFIX) # run gunicorn in a separate child process if os.fork() == 0: