Exemple #1
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}")
Exemple #2
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])
Exemple #3
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])
    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")
    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()
Exemple #6
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])
Exemple #7
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")
def serve(ctx):
    server = uvicorn.Server(
        uvicorn.Config(
            app="main:app",
            uds="/run/nginx/uvicorn.sock",
            forwarded_allow_ips="*",
        ), )
    server.run()
Exemple #9
0
 def __init__(self, loop: asyncio.BaseEventLoop):
     self.static = pathlib.Path(rel_path('../frontend/build', check=False))
     self.loop = loop
     self.app = Starlette(routes=self.routes, on_shutdown=[self.exit])
     self.config = uvicorn.config.Config(self.app, log_config=None, host='0.0.0.0', port=7999)
     self.server = uvicorn.Server(config=self.config)
     self.serve_task = loop.create_task(self.server.serve())
     self.update_task = loop.create_task(self.update_loop())
     self.ws_clients = []
Exemple #10
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()
Exemple #11
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}")
Exemple #12
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}")
Exemple #13
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()
Exemple #14
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()
Exemple #15
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))
    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)
Exemple #17
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
    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
Exemple #19
0
 async def start_api_server(self):
     """
     Start blockchain application backend API server.
     """
     config = uvicorn.Config(self.app,
                             host=self.app.host,
                             port=self.app.port)
     server = uvicorn.Server(config)
     logger.info(
         f'[BlockchainApp] Blockchain API server running on port {self.app.port}.'
     )
     await server.serve()
Exemple #20
0
    async def run(self):
        sock = socket.socket()
        sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        sock.bind((self.host, self.port))
        sock.set_inheritable(True)

        config = uvicorn.Config(self.app, lifespan="on", 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])
Exemple #21
0
def run():
    loop = asyncio.get_event_loop()

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

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

    loop.create_task(server.serve()).add_done_callback(
        lambda *args, **kwargs: loop.stop()
    )
    loop.create_task(worker())
    loop.run_forever()
Exemple #22
0
    def login_with_browser(self, auth: Auth) -> None:
        app = FastAPI()
        port = find_free_network_port()
        url = self.get_auth_url(port)
        try:
            # check if server is reachable or catch any network errors
            requests.head(url)
        except requests.ConnectionError as e:
            raise requests.ConnectionError(
                f"No internet connection available. Please connect to a stable internet connection \n{e}"  # E501
            )
        except requests.RequestException as e:
            raise requests.RequestException(
                f"An error occurred with the request. Please report this issue to Lightning Team \n{e}"  # E501
            )

        logger.info(f"login started for lightning.ai, opening {url}")
        click.launch(url)

        @app.get("/login-complete")
        async def save_token(request: Request,
                             token="",
                             key="",
                             user_id: str = Query("", alias="userID")):
            if token:
                auth.save(token=token,
                          username=user_id,
                          user_id=user_id,
                          api_key=key)
                logger.info("Authentication Successful")
            else:
                logger.warning(
                    "Authentication Failed. This is most likely because you're using an older version of the CLI. \n"  # noqa E501
                    "Please try to update the CLI or open an issue with this information \n"  # E501
                    f"expected token in {request.query_params.items()}")

            # Include the credentials in the redirect so that UI will also be logged in
            params = urlencode(dict(token=token, key=key, userID=user_id))

            return RedirectResponse(
                url=f"{get_lightning_cloud_url()}/me/apps?{params}",
                # The response background task is being executed right after the server finished writing the response
                background=BackgroundTask(stop_server),
            )

        def stop_server():
            server.should_exit = True

        server = uvicorn.Server(
            config=uvicorn.Config(app, port=port, log_level="error"))
        server.run()
Exemple #23
0
 async def start(self):
     self.count_connections = 0
     self.connections = []
     routes = [
         starlette.routing.Route('/', self.root),
         starlette.routing.Route('/demo', self.demo),
         starlette.routing.Route('/status', self.status),
         starlette.routing.WebSocketRoute('/ws', self.websocket),
     ]
     app = starlette.applications.Starlette(debug=False, routes=routes)
     # assuming that this is executed in a container, and the port can be set in the container configuration
     config = uvicorn.Config(app=app, port=8004, host='0.0.0.0')
     server = uvicorn.Server(config)
     await server.serve()
Exemple #24
0
 async def run(self):
     # 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.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()
Exemple #25
0
 async def run(app=None,
               host=None,
               port=None,
               reload=None,
               logger=None):
     import multiprocessing
     __workers__ = (multiprocessing.cpu_count() * 2) + 1
     config = uvicorn.Config(app,
                             host=host,
                             port=port,
                             reload=reload,
                             workers=__workers__,
                             access_log=logger)
     server = uvicorn.Server(config=config)
     server.install_signal_handlers = lambda: None
     await server.serve()
Exemple #26
0
def preview_page() -> uvicorn.Server:
    """Preview page content.

    Returns:
        Uvicorn server (call run() method)
    """
    config = uvicorn.Config(
        "enerator.preview:app",
        host="0.0.0.0",
        lifespan="off",
        log_level="info",
        port=PORT,
        ssl_certfile="/home/jbowman/devel/localdev.bowmanjd.com+4.pem",
        ssl_keyfile="/home/jbowman/devel/localdev.bowmanjd.com+4-key.pem",
    )
    return uvicorn.Server(config=config)
Exemple #27
0
 def init_server(self,
                 host: str = "127.0.0.1",
                 port: int = 1234,
                 *,
                 reload_on_file_edit: bool = False):
     """Initializes the internal server for use"""
     if self.ready:
         raise errors.StateException(
             True, False, message="Server is already initialized")
     self.config = uvicorn.Config(app,
                                  host,
                                  port,
                                  use_colors=False,
                                  reload=reload_on_file_edit)
     self.server = uvicorn.Server(self.config)
     self.ready = True
     return True
Exemple #28
0
async def run(loop):
    global server

    # replace uvicorn run function with our own so it can be run alongside the discord bot
    uvicorn.Server.run = _run
    # remove uvicorn signal handlers installer so those can be handled in main.py
    uvicorn.Server.install_signal_handlers = lambda *a: None

    uvicorn_config = uvicorn.Config(app=app,
                                    loop=loop,
                                    host=config.CONFIG.host,
                                    port=int(config.CONFIG.port))
    uvicorn_server = uvicorn.Server(config=uvicorn_config)

    # store uvicorn server for use in main.py
    server = uvicorn_server
    uvicorn_server.run()
def dev(ctx):
    environ.update(DEV_ENV)
    port = environ.get("PORT", 8000)
    host = "0.0.0.0"  # nosec, it's not a mistake

    config = uvicorn.Config(app="main:app",
                            host=host,
                            port=int(port),
                            debug=True)
    server = uvicorn.Server(config)

    from app.log import logger  # noqa, must be imported before running supervisor

    supervisor = ChangeReload(config,
                              target=server.run,
                              sockets=[config.bind_socket()])
    supervisor.run()
Exemple #30
0
async def run_app(app, **kwargs):
    config = uvicorn.Config(app, **kwargs)
    server = uvicorn.Server(config=config)

    config = server.config
    if not config.loaded:
        config.load()

    server.logger = config.logger_instance
    server.lifespan = config.lifespan_class(config)

    server.logger.info("Started server process")
    await server.startup()
    if server.should_exit:
        return
    await server.main_loop()
    await server.shutdown()
    server.logger.info("Finished server process")