Skip to content

Commit

Permalink
Merge pull request #16 from deNBI/Bugfix_and_Enhancement
Browse files Browse the repository at this point in the history
fix #14 and #15
  • Loading branch information
awalende authored Aug 13, 2024
2 parents 536596d + cb78902 commit 8ec0e0e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
10 changes: 8 additions & 2 deletions OpenStackGPUServer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -80,14 +84,16 @@ def update_cache():

# 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=300)
expire=TIMEOUT,
prefix=MEMCACHEDPREFIX)

# run gunicorn in a separate child process
if os.fork() == 0:
Expand Down
20 changes: 11 additions & 9 deletions openapi_server/controllers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 "<ip>:<port>", 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():
Expand All @@ -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')}

Expand All @@ -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)
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 8ec0e0e

Please sign in to comment.