From 96a752b948be4ec94f5fb6d6ce38d81f05c235a7 Mon Sep 17 00:00:00 2001
From: Ross Brattain <ross.b.brattain@intel.com>
Date: Tue, 24 Jan 2017 13:12:21 -0800
Subject: uwsgi: move init_db to uwsgi entry point

uwsgi will call the 'callable' function which should normally be
Flask.__call__().

But we need to init the db first, so make a wrapper function that inits
the db and then calls app()

Also refactor add_resource into for loop instead of reduce.  reduce is
not really approriate here since we aren't consuming the return value

JIRA: YARDSTICK-543

Change-Id: I692d6d42de09f7d6ecf1a67a22e3019d97a4f3ca
Signed-off-by: Ross Brattain <ross.b.brattain@intel.com>
---
 api/server.py     | 11 ++++++++---
 api/yardstick.ini |  2 +-
 2 files changed, 9 insertions(+), 4 deletions(-)

diff --git a/api/server.py b/api/server.py
index be7963481..1d42feffb 100644
--- a/api/server.py
+++ b/api/server.py
@@ -10,7 +10,6 @@ from __future__ import absolute_import
 
 import inspect
 import logging
-from functools import reduce
 from six.moves import filter
 
 from flasgger import Swagger
@@ -38,6 +37,10 @@ def shutdown_session(exception=None):
     db_session.remove()
 
 
+for u in urlpatterns:
+    api.add_resource(u.resource, u.url, endpoint=u.endpoint)
+
+
 def init_db():
     def func(a):
         try:
@@ -52,8 +55,10 @@ def init_db():
     Base.metadata.create_all(bind=engine)
 
 
-reduce(lambda a, b: a.add_resource(b.resource, b.url,
-                                   endpoint=b.endpoint) or a, urlpatterns, api)
+def app_wrapper(*args, **kwargs):
+    init_db()
+    return app(*args, **kwargs)
+
 
 if __name__ == '__main__':
     _init_logging()
diff --git a/api/yardstick.ini b/api/yardstick.ini
index 2ba881fc1..d2e8956e2 100644
--- a/api/yardstick.ini
+++ b/api/yardstick.ini
@@ -9,7 +9,7 @@ threads = 5
 async = true
 max-requests = 5000
 chmod-socket = 666
-callable = app
+callable = app_wrapper
 enable-threads = true
 close-on-exec = 1
 daemonize= /var/log/yardstick/uwsgi.log
-- 
cgit