Ejemplo n.º 1
0
def run_server(args=None, debug=False):
    if args is None:
        args = parse_args()

    if "ERT_STORAGE_TOKEN" in os.environ:
        authtoken = os.environ["ERT_STORAGE_TOKEN"]
    else:
        authtoken = generate_authtoken()
        os.environ["ERT_STORAGE_TOKEN"] = authtoken

    lockfile = Path.cwd() / "storage_server.json"
    if lockfile.exists():
        sys.exit("'storage_server.json' already exists")

    config_args = {}
    if args.debug or debug:
        config_args.update(reload=True,
                           reload_dirs=[os.path.dirname(ert_shared_path)])
        os.environ["ERT_STORAGE_DEBUG"] = "1"

    _, _, sock = port_handler.find_available_port(custom_host=args.host)

    connection_info = _create_connection_info(sock, authtoken)

    # Appropriated from uvicorn.main:run
    os.environ["ERT_STORAGE_NO_TOKEN"] = "1"
    if args.enable_new_storage:
        args.database_url = "sqlite:///ert.db"
    if args.database_url:
        os.environ["ERT_STORAGE_DATABASE_URL"] = args.database_url
        config = uvicorn.Config("ert_storage.app:app", **config_args)
    else:
        # Dark Storage imports from ERT Storage, which connects to the database
        # at startup. We set the database URL to an SQLite in-memory database so
        # that the import succeeds.
        os.environ["ERT_STORAGE_DATABASE_URL"] = "sqlite://"
        os.environ["ERT_STORAGE_RES_CONFIG"] = args.config or find_ert_config()
        config = uvicorn.Config("ert_shared.dark_storage.app:app",
                                **config_args)
    server = Server(config, json.dumps(connection_info), lockfile)

    print("Storage server is ready to accept requests. Listening on:")
    for url in connection_info["urls"]:
        print(f"  {url}")
        print(f"\nOpenAPI Docs: {url}/docs", file=sys.stderr)

    if args.debug or debug:
        print("\tRunning in NON-SECURE debug mode.\n")
        os.environ["ERT_STORAGE_NO_TOKEN"] = "1"
    else:
        print("\tUsername: __token__")
        print(f"\tPassword: {connection_info['authtoken']}\n")

    if config.should_reload:
        supervisor = ChangeReload(config, target=server.run, sockets=[sock])
        supervisor.run()
    else:
        server.run(sockets=[sock])
Ejemplo n.º 2
0
def main(
    bento_identifier: str = "",
    bind: str = "",
    working_dir: t.Optional[str] = None,
    reload: bool = False,
    reload_delay: t.Optional[float] = None,
    backlog: int = 2048,
):
    import uvicorn  # type: ignore

    from ...configuration import get_debug_mode

    ServiceContext.component_name_var.set("dev_api_server")

    parsed = urlparse(bind)

    if parsed.scheme == "fd":
        fd = int(parsed.netloc)
        sock = socket.socket(fileno=fd)
        log_level = "debug" if get_debug_mode() else "info"
        svc = load(bento_identifier,
                   working_dir=working_dir,
                   change_global_cwd=True)
        uvicorn_options = {
            "log_level": log_level,
            "backlog": backlog,
            "reload": reload,
            "reload_delay": reload_delay,
            "log_config": LOGGING_CONFIG,
            "workers": 1,
        }

        if reload:
            # When reload=True, the app parameter in uvicorn.run(app) must be the import str
            asgi_app_import_str = f"{svc._import_str}.asgi_app"  # type: ignore[reportPrivateUsage]
            # TODO: use svc.build_args.include/exclude as default files to watch
            # TODO: watch changes in model store when "latest" model tag is used
            config = uvicorn.Config(asgi_app_import_str, **uvicorn_options)
            server = uvicorn.Server(config)

            from uvicorn.supervisors import ChangeReload  # type: ignore

            ChangeReload(config, target=server.run, sockets=[sock]).run()
        else:
            config = uvicorn.Config(svc.asgi_app, **uvicorn_options)
            uvicorn.Server(config).run(sockets=[sock])
    else:
        raise ValueError(f"Unsupported bind scheme: {bind}")
    def start_listen(self,
                     host,
                     port,
                     ssl_keyfile=None,
                     ssl_certfile=None,
                     ssl_keyfile_password=None):
        if _BACKEND != BACKEND_TYPES.FAST_API and (ssl_keyfile
                                                   or ssl_certfile):
            raise NotImplementedError(
                'Not supported Https for flask. You should install fastapi and uvicorn.'
            )

        if _BACKEND == BACKEND_TYPES.FLASK_WITH_WAITRESS:
            serve(RequestLogger(self.app),
                  _quiet=True,
                  listen="{host}:{port}".format(host=host, port=port))
        elif _BACKEND == BACKEND_TYPES.PURE_FLASK:
            self.app.run(host=host, port=port)
        elif _BACKEND == BACKEND_TYPES.FAST_API:
            config = uvicorn.Config(self.app,
                                    host=host,
                                    port=port,
                                    ssl_keyfile=ssl_keyfile,
                                    ssl_certfile=ssl_certfile,
                                    ssl_keyfile_password=ssl_keyfile_password,
                                    log_config=None)
            config.load()
            if config.is_ssl:
                config.ssl.options |= (
                    ssl.OP_NO_SSLv2 | ssl.OP_NO_SSLv3 | ssl.OP_NO_TLSv1
                    | ssl.OP_NO_TLSv1_1
                )  # RFC 7540 Section 9.2: MUST be TLS >=1.2
                config.ssl.set_ciphers('DHE+AESGCM:ECDHE+AESGCM')
            server = uvicorn.Server(config)
            server.run()
Ejemplo n.º 4
0
def test_non_default_uvloop_config_raises_warning(app):
    app.config.USE_UVLOOP = True

    class CustomServer(uvicorn.Server):
        def install_signal_handlers(self):
            pass

    config = uvicorn.Config(app=app, loop="asyncio", limit_max_requests=0)
    server = CustomServer(config=config)

    with pytest.warns(UserWarning) as records:
        server.run()

    all_tasks = asyncio.all_tasks(asyncio.get_event_loop())
    for task in all_tasks:
        task.cancel()

    msg = ""
    for record in records:
        _msg = str(record.message)
        if _msg.startswith("You have set the USE_UVLOOP configuration"):
            msg = _msg
            break

    assert msg == (
        "You have set the USE_UVLOOP configuration option, but Sanic "
        "cannot control the event loop when running in ASGI mode."
        "This option will be ignored.")
Ejemplo n.º 5
0
def trigger_api() -> None:
    """Initiates the fast API in a dedicated process using uvicorn server.

    See Also:
        - Checks if the port is being used. If so, makes a ``GET`` request to the endpoint.
        - Attempts to kill the process listening to the port, if the endpoint doesn't respond.
    """
    url = f'http://{env.offline_host}:{env.offline_port}'

    if is_port_in_use(port=env.offline_port):
        logger.info(f'{env.offline_port} is currently in use.')

        try:
            res = requests.get(url=url, timeout=1)
            if res.ok:
                logger.info(f'{url} is accessible.')
                return
            raise requests.exceptions.ConnectionError
        except (requests.exceptions.ConnectionError, requests.exceptions.Timeout):
            logger.error('Unable to connect to existing uvicorn server.')

        if not kill_port_pid(port=env.offline_port):  # This might terminate Jarvis
            logger.critical('Failed to kill existing PID. Attempting to re-create session.')

    argument_dict = {
        "app": "api.fast:app",
        "host": env.offline_host,
        "port": env.offline_port,
        "reload": True
    }
    if not env.macos:
        del argument_dict['reload']

    config = uvicorn.Config(**argument_dict)
    APIServer(config=config).run_in_parallel()
Ejemplo n.º 6
0
def example_server() -> Iterator[Server]:
    from example.server import app

    config = uvicorn.Config(app=app, loop="asyncio")
    server = Server(config)
    with server.serve_in_thread():
        yield server
Ejemplo n.º 7
0
def setup_app(context: Context, *args, **kwargs):
    """
    Setup the application for tests.

    Steps:
        - Bootstrap the uvicorn server with the FastAPI app running.
        - Add the 'url_root' to context.
        - yield
        - Finish the server
    """
    import uvicorn

    from drivr import app

    from ..modules.server_app import Server

    app_port = context.config.userdata.get("app_port")
    app_host = context.config.userdata.get("app_host")

    config = uvicorn.Config(
        app,
        host=app_host,
        port=int(app_port),
        log_level=context.config.userdata.get("uvicorn_debug"),
        loop="asyncio",
    )
    context.url_root = f"http://{app_host}:{app_port}"

    server = Server(config=config)
    with server.run_in_thread():
        yield
Ejemplo n.º 8
0
 def __init__(self, app: FastAPI, host: str, port: int, workers=1):
     """
     Create a Uvicorn test server for use as a pytest fixture
     """
     self._startup_done = asyncio.Event()
     super().__init__(
         config=uvicorn.Config(app, host=host, port=port, workers=1))
Ejemplo n.º 9
0
def app_run():
    config = uvicorn.Config(app, host=online_inference_app_params.host, port=online_inference_app_params.port)
    server = Server(config=config)

    time.sleep(APP_START_DELAY)
    with server.run_in_thread():
        time.sleep(APP_LIFE_DURATION)
Ejemplo n.º 10
0
    def __init__(self, app, **kwargs):
        self.event = threading.Event()
        self.config = uvicorn.Config(app, **kwargs)
        self.server = uvicorn.Server(config=self.config)
        self.config.load()

        super().__init__(coroutine=self.run(), name="Webserver thread")
Ejemplo n.º 11
0
def example_server() -> Iterator[Server]:
    sys.path.append("example")
    from server import app

    config = uvicorn.Config(app=app, loop="asyncio")
    server = Server(config=config)
    yield from serve_in_thread(server)
Ejemplo n.º 12
0
    async def run(self):
        sock = socket.socket()
        if SOCKET_REUSE_PORT_ENABLED:
            set_socket_reuse_port(sock)
        try:
            sock.bind((self.host, self.port))
        except OSError:
            # The OS failed to bind a socket to the given host and port.
            raise ValueError(
                f"""Failed to bind Ray Serve HTTP proxy to '{self.host}:{self.port}'.
Please make sure your http-host and http-port are specified correctly.""")

        # Note(simon): we have to use lower level uvicorn Config and Server
        # class because we want to run the server as a coroutine. The only
        # alternative is to call uvicorn.run which is blocking.
        config = uvicorn.Config(
            self.wrapped_app,
            host=self.host,
            port=self.port,
            root_path=self.root_path,
            lifespan="off",
            access_log=False,
        )
        server = uvicorn.Server(config=config)
        # TODO(edoakes): we need to override install_signal_handlers here
        # because the existing implementation fails if it isn't running in
        # the main thread and uvicorn doesn't expose a way to configure it.
        server.install_signal_handlers = lambda: None

        self.setup_complete.set()
        await server.serve(sockets=[sock])
Ejemplo n.º 13
0
    async def run(self):
        sock = socket.socket()
        # These two socket options will allow multiple process to bind the the
        # same port. Kernel will evenly load balance among the port listeners.
        # Note: this will only work on Linux.
        sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        if hasattr(socket, "SO_REUSEPORT"):
            sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)

        try:
            sock.bind((self.host, self.port))
        except OSError:
            # The OS failed to bind a socket to the given host and port.
            raise ValueError(
                f"""Failed to bind Ray Serve HTTP proxy to '{self.host}:{self.port}'.
Please make sure your http-host and http-port are specified correctly.""")

        # Note(simon): we have to use lower level uvicorn Config and Server
        # class because we want to run the server as a coroutine. The only
        # alternative is to call uvicorn.run which is blocking.
        config = uvicorn.Config(self.wrapped_app,
                                host=self.host,
                                port=self.port,
                                lifespan="off",
                                access_log=False)
        server = uvicorn.Server(config=config)
        # TODO(edoakes): we need to override install_signal_handlers here
        # because the existing implementation fails if it isn't running in
        # the main thread and uvicorn doesn't expose a way to configure it.
        server.install_signal_handlers = lambda: None

        self.setup_complete.set()
        await server.serve(sockets=[sock])
Ejemplo n.º 14
0
    async def run(self):
        sock = socket.socket()
        # These two socket options will allow multiple process to bind the the
        # same port. Kernel will evenly load balance among the port listeners.
        # Note: this will only work on Linux.
        sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        if hasattr(socket, "SO_REUSEPORT"):
            sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
        sock.bind((self.host, self.port))

        # Note(simon): we have to use lower level uvicorn Config and Server
        # class because we want to run the server as a coroutine. The only
        # alternative is to call uvicorn.run which is blocking.
        config = uvicorn.Config(
            self.wrapped_app,
            host=self.host,
            port=self.port,
            lifespan="off",
            access_log=False)
        server = uvicorn.Server(config=config)
        # TODO(edoakes): we need to override install_signal_handlers here
        # because the existing implementation fails if it isn't running in
        # the main thread and uvicorn doesn't expose a way to configure it.
        server.install_signal_handlers = lambda: None
        await server.serve(sockets=[sock])
Ejemplo n.º 15
0
def main():
    # add better logging
    logger.remove()
    logger.add(sys.stdout, backtrace=True, diagnose=True, enqueue=True)
    # disable info logging from APScheduler
    logging.getLogger("apscheduler").setLevel(logging.WARNING)

    # configuration
    c = init_settings(default_config)
    logger.debug(f"using following configuration:\n{c}")

    # configure uvicorn
    config = uvicorn.Config(app, host="0.0.0.0", port=8000)
    server = uvicorn.Server(config)

    # event loop
    loop = asyncio.get_event_loop()

    # init scheduler
    initial_scheduled_tasks = c["initial_scheduled_tasks"]
    scheduler = MyScheduler(initial_scheduled_tasks)
    logger.debug(f"scheduler started {scheduler}")

    # init hass-instance
    global hass
    hass = HassInstance(
        c["hass_url"],
        c["hass_api_key"],
        scheduler=scheduler,
        update_freq=c["hass_update_frequency_seconds"],
        states=states,
    )

    # mqtt timer object
    mqtt_timer_counters = {}

    # init mqtt
    mqtt_events = {"on_message": on_hass_mqtt_message}
    mqtt = MyMQTT(
        c["mqtt_broker"],
        auth=(c["mqtt_user"], c["mqtt_password"]),
        event_functions=mqtt_events,
        hass_ref=hass,
        app_config=c,
        mqtt_timer_counters=mqtt_timer_counters,
    )

    # start job processing mqtt timers every second
    scheduler.add_task(
        process_mqtt_timers,
        "interval",
        [mqtt_timer_counters, mqtt.client],
        seconds=1,
        id="process_mqtt_timers",
    )

    # start event-loop
    loop.run_until_complete(server.serve())
    logger.info("stopping hass_assister")
Ejemplo n.º 16
0
def uds_server():
    uds = "https_test_server.sock"
    config = uvicorn.Config(app=app, lifespan="off", uds=uds, loop="asyncio")
    server = TestServer(config=config)
    try:
        yield from serve_in_thread(server)
    finally:
        os.remove(uds)
Ejemplo n.º 17
0
def serve(ctx):
    server = uvicorn.Server(
        uvicorn.Config(
            app="main:app",
            uds="/run/nginx/uvicorn.sock",
            forwarded_allow_ips="*",
        ), )
    server.run()
Ejemplo n.º 18
0
 def serve_uvicorn(self, host, port, **kwargs):
     util.ensure_pip("uvicorn")
     import uvicorn
     self.server_version = uvicorn.__version__
     reload = kwargs.get("reload", False)
     app_asgi = build_asgi_i(self)
     config = uvicorn.Config(app_asgi, host=host, port=port, reload=reload)
     self._server = uvicorn.Server(config=config)
     self._server.run()
Ejemplo n.º 19
0
def uds_server() -> typing.Iterator[UvicornServer]:
    uds = "test_server.sock"
    config = uvicorn.Config(app=app, lifespan="off", loop="asyncio", uds=uds)
    server = UvicornServer(config=config)
    try:
        with server.serve_in_thread():
            yield server
    finally:
        os.remove(uds)
Ejemplo n.º 20
0
def main(
    bento_identifier: str = "",
    runner_name: str = "",
    bind: str = "",
    working_dir: t.Optional[str] = None,
) -> None:
    """
    Start a runner server.

    Args:
        bento_identifier: the Bento identifier
        name: the name of the runner
        bind: the bind address URI. Can be:
            - tcp://host:port
            - unix://path/to/unix.sock
            - file:///path/to/unix.sock
            - fd://12
        working_dir: (Optional) the working directory
    """

    import uvicorn  # type: ignore

    from bentoml._internal.server.runner_app import RunnerAppFactory

    ServiceContext.component_name_var.set(runner_name)

    svc = load(bento_identifier, working_dir=working_dir, change_global_cwd=True)
    runner = svc.runners[runner_name]
    app = t.cast("ASGI3Application", RunnerAppFactory(runner)())

    parsed = urlparse(bind)
    uvicorn_options = {
        "log_level": "info",
        "log_config": LOGGING_CONFIG,
        "workers": 1,
    }
    if parsed.scheme in ("file", "unix"):
        uvicorn.run(
            app,
            uds=uri_to_path(bind),
            **uvicorn_options,
        )
    elif parsed.scheme == "tcp":
        uvicorn.run(
            app,
            host=parsed.hostname,
            port=parsed.port,
            **uvicorn_options,
        )
    elif parsed.scheme == "fd":
        # when fd is provided, we will skip the uvicorn internal supervisor, thus there is only one process
        fd = int(parsed.netloc)
        sock = socket.socket(fileno=fd)
        config = uvicorn.Config(app, **uvicorn_options)
        uvicorn.Server(config).run(sockets=[sock])
    else:
        raise ValueError(f"Unsupported bind scheme: {bind}")
Ejemplo n.º 21
0
Archivo: main.py Proyecto: ManInFez/ert
def run_server(args=None, debug=False):
    if args is None:
        args = parse_args()

    if "ERT_STORAGE_TOKEN" in os.environ:
        authtoken = os.environ["ERT_STORAGE_TOKEN"]
    else:
        authtoken = generate_authtoken()
        os.environ["ERT_STORAGE_TOKEN"] = authtoken

    # Use sqlite in cwd if nothing else is specified
    if "ERT_STORAGE_DATABASE_URL" not in os.environ:
        os.environ["ERT_STORAGE_DATABASE_URL"] = "sqlite:///ert.db"

    lockfile = Path.cwd() / "storage_server.json"
    if lockfile.exists():
        sys.exit("'storage_server.json' already exists")

    config_args = {}
    if args.debug or debug:
        config_args.update(reload=True,
                           reload_dirs=[os.path.dirname(ert_shared_path)])
        os.environ["ERT_STORAGE_DEBUG"] = "1"

    sock = bind_open_socket(args.host)
    connection_info = {
        "urls": [
            f"http://{host}:{sock.getsockname()[1]}" for host in (
                sock.getsockname()[0],
                socket.gethostname(),
                socket.getfqdn(),
            )
        ],
        "authtoken":
        authtoken,
    }

    # Appropriated from uvicorn.main:run
    config = uvicorn.Config("ert_storage.app:app", **config_args)
    server = Server(config, json.dumps(connection_info), lockfile)

    print("Storage server is ready to accept requests. Listening on:")
    for url in connection_info["urls"]:
        print(f"  {url}")

    print(f"\nOpenAPI Docs: {url}/docs", file=sys.stderr)
    if args.debug or debug:
        print("\tRunning in NON-SECURE debug mode.\n")
    else:
        print(f"\tUsername: __token__")
        print(f"\tPassword: {connection_info['authtoken']}\n")

    if config.should_reload:
        supervisor = ChangeReload(config, target=server.run, sockets=[sock])
        supervisor.run()
    else:
        server.run(sockets=[sock])
Ejemplo n.º 22
0
def main(
    bento_identifier: str = "",
    bind: str = "",
    runner_map: t.Optional[str] = None,
    backlog: int = 2048,
    working_dir: t.Optional[str] = None,
):
    import uvicorn  # type: ignore

    ServiceContext.component_name_var.set("api_server")

    log_level = "info"
    if runner_map is not None:
        from ...configuration.containers import DeploymentContainer

        DeploymentContainer.remote_runner_mapping.set(json.loads(runner_map))
    svc = load(bento_identifier, working_dir=working_dir, change_global_cwd=True)

    parsed = urlparse(bind)
    uvicorn_options: dict[str, Any] = {
        "log_level": log_level,
        "backlog": backlog,
        "log_config": LOGGING_CONFIG,
        "workers": 1,
    }
    app = t.cast("ASGI3Application", svc.asgi_app)
    if parsed.scheme in ("file", "unix"):
        path = uri_to_path(bind)
        uvicorn_options["uds"] = path
        config = uvicorn.Config(app, **uvicorn_options)
        uvicorn.Server(config).run()
    elif parsed.scheme == "tcp":
        uvicorn_options["host"] = parsed.hostname
        uvicorn_options["port"] = parsed.port
        config = uvicorn.Config(app, **uvicorn_options)
        uvicorn.Server(config).run()
    elif parsed.scheme == "fd":
        # when fd is provided, we will skip the uvicorn internal supervisor, thus there is only one process
        fd = int(parsed.netloc)
        sock = socket.socket(fileno=fd)
        config = uvicorn.Config(app, **uvicorn_options)
        uvicorn.Server(config).run(sockets=[sock])
    else:
        raise ValueError(f"Unsupported bind scheme: {bind}")
Ejemplo n.º 23
0
def run():
    loop = asyncio.get_event_loop()

    # あんまりキレイじゃないけれど..process間通信的な物が欲しい

    config = uvicorn.Config(api, port=44444)
    server = uvicorn.Server(config=config)

    tasks = [server.serve(), worker()]
    loop.run_until_complete(asyncio.wait(tasks))
Ejemplo n.º 24
0
def run():
    loop = asyncio.get_event_loop()

    config = uvicorn.Config(api, port=44444)
    server = uvicorn.Server(config=config)

    loop.create_task(server.serve()).add_done_callback(
        lambda *args, **kwargs: worker.stop())
    loop.create_task(worker.start())
    loop.run_forever()
Ejemplo n.º 25
0
def main(log_level="info"):
    server = uvicorn.Server(
        uvicorn.Config(
            app="saatja.app:app",
            host="0.0.0.0",  # nosec 0.0.0.0 is not a mistake
            port=conf.PORT,
            log_level=log_level,
            reload=False,
        ), )
    server.run()
Ejemplo n.º 26
0
    def __init__(self, app, host='127.0.0.1', port=PORT):
        """Create a Uvicorn test server

        Args:
            app (FastAPI, optional): the FastAPI app. Defaults to main.app.
            host (str, optional): the host ip. Defaults to '127.0.0.1'.
            port (int, optional): the port. Defaults to PORT.
        """
        self._startup_done = asyncio.Event()
        super().__init__(config=uvicorn.Config(app, host=host, port=port))
Ejemplo n.º 27
0
    def __init__(self, app: fastapi.FastAPI, host: str = '127.0.0.1', port: int = 8000) -> None:
        """Initialize a FakeAPI instance by mounting a FastAPI app and starting Uvicorn.

        Args:
            app (FastAPI, optional): the FastAPI app.
            host (str, optional): the host ip. Defaults to '127.0.0.1'.
            port (int, optional): the port. Defaults to 8000.
        """
        self._startup_done = asyncio.Event()
        super().__init__(config=uvicorn.Config(app, host=host, port=port))
Ejemplo n.º 28
0
    def __init__(self, cam, port):
        self.cam = cam

        self.app = Starlette()
        self.app.debug = True

        self.app.route(self.IMAGE_URL)(self.image)
        self.app.route("/")(self.index)

        self.config = uvicorn.Config(self.app, host="0.0.0.0", port=port)
        self.server = uvicorn.Server(config=self.config)
Ejemplo n.º 29
0
 async def _run(self):
     api_server = uvicorn.Server(
         config=uvicorn.Config(self.app, port=self.port, host="localhost"))
     task = asyncio.create_task(api_server.serve())
     while True:
         await asyncio.sleep(0.1)
         if self._shutdown.isSet():
             api_server.should_exit = True
             await api_server.shutdown()
             await asyncio.wait([task])
             return
Ejemplo n.º 30
0
    async def run_api(self):
        try:
            self.fastapi.get("/status")(self.status)

            config = uvicorn.Config(self.fastapi,
                                    host="0.0.0.0", port=self.config.port)

            server = uvicorn.Server(config)

            await server.serve()
        except asyncio.CancelledError:
            pass