Skip to content

Commit

Permalink
Clean up runserver handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Yolken committed Dec 1, 2016
1 parent 4eaeac4 commit b192578
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 58 deletions.
53 changes: 37 additions & 16 deletions superset/bin/superset
Expand Up @@ -34,31 +34,52 @@ class GunicornSupersetApplication(gunicorn.app.base.BaseApplication):
return app


def run_gunicorn():
def run_server():
parser = argparse.ArgumentParser(description='Run gunicorn for superset')
subparsers = parser.add_subparsers()
gunicorn_parser = subparsers.add_parser('runserver_gunicorn')

gunicorn_parser.add_argument('-a', '--address', type=str, default='127.0.0.1')
gunicorn_parser.add_argument('-p', '--port', type=int, default=8088)
gunicorn_parser.add_argument('-w', '--workers', type=int, default=4)
gunicorn_parser.add_argument('-t', '--timeout', type=int, default=30)
gunicorn_parser = subparsers.add_parser('runserver')

gunicorn_parser.add_argument(
'-d', '--debug', action='store_true',
help='Start the web server in debug mode')
gunicorn_parser.add_argument(
'-a', '--address', type=str, default='127.0.0.1',
help='Specify the address to which to bind the web server')
gunicorn_parser.add_argument(
'-p', '--port', type=int, default=8088,
help='Specify the port on which to run the web server')
gunicorn_parser.add_argument(
'-w', '--workers', type=int, default=4,
help='Number of gunicorn web server workers to fire up')
gunicorn_parser.add_argument(
'-t', '--timeout', type=int, default=30,
help='Specify the timeout (seconds) for the gunicorn web server')

args = parser.parse_args()

gunicorn_app_obj = GunicornSupersetApplication(
args.address, args.port, args.workers, args.timeout)
gunicorn_app_obj.run()
if args.debug:
from superset import app

app.run(
host='0.0.0.0',
port=int(args.port),
threaded=True,
debug=True)
else:
gunicorn_app_obj = GunicornSupersetApplication(
args.address, args.port, args.workers, args.timeout)
gunicorn_app_obj.run()


if __name__ == "__main__":
if len(sys.argv) > 1 and sys.argv[1] == 'runserver_gunicorn':
# In the gunicorn case, don't go through manager so that superset
# import is deferred until GunicornSupersetApplication.load is called;
# this allows for the app to be cleanly forked without running the
# gunicorn command-line.
run_gunicorn()
if len(sys.argv) > 1 and sys.argv[1] == 'runserver':
# In the runserver case, don't go through the manager so that superset
# import is deferred until the app is loaded; this allows for the app to be run via pex
# and cleanly forked in the gunicorn case.
#
# TODO: Refactor cli so that gunicorn can be started without first importing superset;
# this will allow us to move the runserver logic back into cli module.
run_server()
else:
from superset.cli import manager
manager.run()
42 changes: 0 additions & 42 deletions superset/cli.py
Expand Up @@ -21,48 +21,6 @@
manager.add_command('db', MigrateCommand)


@manager.option(
'-d', '--debug', action='store_true',
help="Start the web server in debug mode")
@manager.option(
'-a', '--address', default=config.get("SUPERSET_WEBSERVER_ADDRESS"),
help="Specify the address to which to bind the web server")
@manager.option(
'-p', '--port', default=config.get("SUPERSET_WEBSERVER_PORT"),
help="Specify the port on which to run the web server")
@manager.option(
'-w', '--workers', default=config.get("SUPERSET_WORKERS", 2),
help="Number of gunicorn web server workers to fire up")
@manager.option(
'-t', '--timeout', default=config.get("SUPERSET_WEBSERVER_TIMEOUT"),
help="Specify the timeout (seconds) for the gunicorn web server")
def runserver(debug, address, port, timeout, workers):
"""Starts a Superset web server"""
debug = debug or config.get("DEBUG")
if debug:
app.run(
host='0.0.0.0',
port=int(port),
threaded=True,
debug=True)
else:
print(
"DEPRECATED: Please run gunicorn via the "
"'runserver_gunicorn' command")

# TODO(byolken): Remove this and associated manager options.
cmd = (
"gunicorn "
"-w {workers} "
"--timeout {timeout} "
"-b {address}:{port} "
"--limit-request-line 0 "
"--limit-request-field_size 0 "
"superset:app").format(**locals())
print("Starting server with command: " + cmd)
Popen(cmd, shell=True).wait()


@manager.command
def init():
"""Inits the Superset application"""
Expand Down

0 comments on commit b192578

Please sign in to comment.