-
Notifications
You must be signed in to change notification settings - Fork 91
Only use flask for debug #1607
base: master
Are you sure you want to change the base?
Only use flask for debug #1607
Changes from all commits
268379e
860f8a1
562252b
ba6a8bb
70f4ceb
55e4b52
52ebcb4
9164eef
2a31f78
6badbb5
6f59917
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,13 +6,32 @@ | |
from __future__ import unicode_literals | ||
|
||
import requests | ||
import multiprocessing | ||
import gunicorn.app.base | ||
|
||
import ga4gh.server.cli as cli | ||
import ga4gh.server.frontend as frontend | ||
|
||
import ga4gh.common.cli as common_cli | ||
|
||
|
||
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 self.options.iteritems() | ||
if key in self.cfg.settings and value is not None]) | ||
for key, value in config.iteritems(): | ||
self.cfg.set(key.lower(), value) | ||
|
||
def load(self): | ||
return self.application | ||
|
||
|
||
def addServerOptions(parser): | ||
parser.add_argument( | ||
"--port", "-P", default=8000, type=int, | ||
|
@@ -28,20 +47,42 @@ def addServerOptions(parser): | |
help="The configuration file to use") | ||
parser.add_argument( | ||
"--tls", "-t", action="store_true", default=False, | ||
help="Start in TLS (https) mode.") | ||
help="Start in TLS (https) mode (for Flask debug)") | ||
parser.add_argument( | ||
"--dont-use-reloader", default=False, action="store_true", | ||
help="Don't use the flask reloader") | ||
help="Don't use the Flask reloader (for Flask debug)") | ||
parser.add_argument( | ||
"--debug", "-d", action='store_true', default=False, | ||
help="Runs the server using the gunicorn WSGI server.") | ||
cli.addVersionArgument(parser) | ||
cli.addDisableUrllibWarningsArgument(parser) | ||
|
||
|
||
def runGunicornServer(parsedArgs): | ||
options = { | ||
'bind': '%s:%s' % (parsedArgs.host, parsedArgs.port), | ||
'workers': number_of_workers(), | ||
'accesslog': '-', # Puts the access log on stdout | ||
'errorlog': '-' # Puts the error log on stdout | ||
} | ||
app = StandaloneApplication(frontend.app, options) | ||
app.run() | ||
return app | ||
|
||
|
||
def getServerParser(): | ||
""" | ||
Used by sphinx.argparse. | ||
""" | ||
parser = common_cli.createArgumentParser("GA4GH reference server") | ||
addServerOptions(parser) | ||
return parser | ||
|
||
|
||
def number_of_workers(): | ||
return (multiprocessing.cpu_count() * 2) + 1 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should probably have a comment here justifying this formula There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lifted from here http://docs.gunicorn.org/en/latest/custom.html?highlight=multiprocessing I'll add a link |
||
|
||
|
||
def server_main(args=None): | ||
parser = getServerParser() | ||
parsedArgs = parser.parse_args(args) | ||
|
@@ -52,7 +93,10 @@ def server_main(args=None): | |
sslContext = None | ||
if parsedArgs.tls or ("OIDC_PROVIDER" in frontend.app.config): | ||
sslContext = "adhoc" | ||
frontend.app.run( | ||
host=parsedArgs.host, port=parsedArgs.port, | ||
use_reloader=not parsedArgs.dont_use_reloader, | ||
ssl_context=sslContext) | ||
if parsedArgs.debug: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't get why this is called |
||
frontend.app.run(host=parsedArgs.host, | ||
port=parsedArgs.port, | ||
use_reloader=not parsedArgs.dont_use_reloader, | ||
ssl_context=sslContext) | ||
else: | ||
runGunicornServer(parsedArgs) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -194,6 +194,7 @@ def getCmdLine(self): | |
python server_dev.py | ||
--dont-use-reloader | ||
--disable-urllib-warnings | ||
-d | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. best to use the non-shorthand version of the flag here, for clarity in reading this file |
||
--host 0.0.0.0 | ||
--config TestConfig | ||
--config-file {} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
""" | ||
Tests related to the server start script | ||
""" | ||
from __future__ import division | ||
from __future__ import print_function | ||
from __future__ import unicode_literals | ||
|
||
import unittest | ||
|
||
import ga4gh.server.cli.server as server | ||
import ga4gh.server.frontend as frontend | ||
|
||
|
||
class TestExceptionHandler(unittest.TestCase): | ||
""" | ||
Test that the server script functions behave in expected ways. | ||
""" | ||
def testGetServerParser(self): | ||
self.assertIsNotNone(server.getServerParser(), | ||
"The server parser should be returned so " | ||
"that we can create docs for sphinx") | ||
|
||
def test_number_of_workers(self): | ||
self.assertTrue(type(server.number_of_workers()) == int, | ||
"The number of workers function should return an " | ||
"integer.") | ||
|
||
def testStandaloneApplicationInstance(self): | ||
app = server.StandaloneApplication(frontend.app) | ||
self.assertIsNotNone(app.run, "Ensures the class instantiates from " | ||
"our WSGI app properly. The run " | ||
"function will spawn many processes.") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the docs say the
-g
flag is to run gunicornThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, I started by making running under gunicorn a flag, but I believe the default functionality when running
ga4gh_server
should be to run in a multiworker environment. The-d
will run using the Flask debug server (werkzeug).