From 48f4c30b2140305f3bc3e4d372879f95ac74c4f2 Mon Sep 17 00:00:00 2001 From: Lin Yang Date: Fri, 11 Nov 2016 17:55:18 -0800 Subject: [PATCH] Use gunicorn instead of werkzeug Flask use werkzeug as default WSGI container, but it cannot support production requirement and leads to crash in some cases. So use gunicorn instead of it. Change-Id: I2e542ab79c755c2407ae695a6de29c063233269b Closes-Bug: #1641242 --- requirements.txt | 2 +- setup.cfg | 2 +- valence/cmd/__init__.py | 0 valence/{run.py => cmd/api.py} | 28 +++++++++++++++++++++++++++- 4 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 valence/cmd/__init__.py rename valence/{run.py => cmd/api.py} (53%) diff --git a/requirements.txt b/requirements.txt index 09afc05..e85b923 100644 --- a/requirements.txt +++ b/requirements.txt @@ -11,4 +11,4 @@ python-dateutil==2.5.3 pytz==2016.7 requests==2.11.1 six==1.10.0 -Werkzeug==0.11.11 +gunicorn==19.6.0 diff --git a/setup.cfg b/setup.cfg index d824881..7828023 100644 --- a/setup.cfg +++ b/setup.cfg @@ -52,4 +52,4 @@ source-dir = releasenotes/source [entry_points] console_scripts = - valence = valence.run:main + valence = valence.cmd.api:main diff --git a/valence/cmd/__init__.py b/valence/cmd/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/valence/run.py b/valence/cmd/api.py similarity index 53% rename from valence/run.py rename to valence/cmd/api.py index 89ed654..ac295d0 100755 --- a/valence/run.py +++ b/valence/cmd/api.py @@ -14,16 +14,42 @@ # under the License. import logging + +import gunicorn.app.base +from gunicorn.six import iteritems + from valence.api.route import app as application from valence import config as cfg + LOG = logging.getLogger(__name__) +class StandaloneApplication(gunicorn.app.base.BaseApplication): + + def __init__(self, app, options=None): + self.options = options or {} + self.application = app + super(StandaloneApplication, self).__init__() + + def load_config(self): + config = dict([(key, value) for key, value in iteritems(self.options) + if key in self.cfg.settings and value is not None]) + for key, value in iteritems(config): + self.cfg.set(key.lower(), value) + + def load(self): + return self.application + + def main(): - application.run(host=cfg.bind_host, port=cfg.bind_port, debug=cfg.debug) + options = { + 'bind': '%s:%s' % (cfg.bind_host, cfg.bind_port) + } + StandaloneApplication(application, options).run() LOG.info(("Valence Server on http://%(host)s:%(port)s"), {'host': cfg.bind_host, 'port': cfg.bind_port}) + if __name__ == '__main__': main()