Exemplo n.º 1
0
def load_app_and_run_server() -> None:
    """Parse arguments, read configuration, and start the server."""
    sys.path.append(os.getcwd())

    shutdown_event = register_signal_handlers()

    args = parse_args(sys.argv[1:])
    with args.config_file:
        config = read_config(args.config_file, args.server_name, args.app_name)
    assert config.server

    configure_logging(config, args.debug)

    app = make_app(config.app)
    listener = make_listener(args.bind)
    server = make_server(config.server, listener, app)

    if einhorn.is_worker():
        einhorn.ack_startup()

    if args.reload:
        reloader.start_reload_watcher(extra_files=[args.config_file.name])

    # clean up leftovers from initialization before we get into requests
    gc.collect()

    logger.info("Listening on %s, PID:%s", listener.getsockname(), os.getpid())
    server.start()
    try:
        shutdown_event.wait()
        logger.info("Finally stopping server, PID:%s", os.getpid())
    finally:
        server.stop()
Exemplo n.º 2
0
def load_app_and_run_server() -> None:
    """Parse arguments, read configuration, and start the server."""
    sys.path.append(os.getcwd())

    shutdown_event = register_signal_handlers()

    args = parse_args(sys.argv[1:])
    with args.config_file:
        config = read_config(args.config_file, args.server_name, args.app_name)
    assert config.server

    configure_logging(config, args.debug)

    app = make_app(config.app)
    listener = make_listener(args.bind)
    server = make_server(config.server, listener, app)

    cfg = parse_config(config.server, {"drain_time": OptionalConfig(Timespan)})

    if einhorn.is_worker():
        einhorn.ack_startup()

    try:
        # pylint: disable=cyclic-import
        from baseplate.server.prometheus import start_prometheus_exporter
    except ImportError:
        logger.debug("Prometheus exporter not available. pip install prometheus-client to enable.")
    else:
        start_prometheus_exporter()

    if args.reload:
        reloader.start_reload_watcher(extra_files=[args.config_file.name])

    # clean up leftovers from initialization before we get into requests
    gc.collect()

    logger.info("Listening on %s", listener.getsockname())
    server.start()
    try:
        shutdown_event.wait()

        SERVER_STATE.state = ServerLifecycle.SHUTTING_DOWN

        if cfg.drain_time:
            logger.debug("Draining inbound requests...")
            time.sleep(cfg.drain_time.total_seconds())
    finally:
        logger.debug("Gracefully shutting down...")
        server.stop()
        logger.info("Exiting")
Exemplo n.º 3
0
def load_app_and_run_server():
    """Parse arguments, read configuration, and start the server."""
    register_signal_handlers()

    args = parse_args(sys.argv[1:])
    config = read_config(args.config_file, args.server_name, args.app_name)
    configure_logging(config, args.debug)

    app = make_app(config.app)
    listener = make_listener(args.bind)
    server = make_server(config.server, listener, app)

    if einhorn.is_worker():
        einhorn.ack_startup()

    if args.reload:
        reloader.start_reload_watcher(extra_files=[args.config_file.name])

    logger.info("Listening on %s", listener.getsockname())

    server.serve_forever()