Exemplo n.º 1
0
class HTTPRunner:
    def __init__(self, http_app: Callable):
        self._http_app = http_app
        self.log = logging.getLogger(self.__class__.__name__)
        self._uv_server = None

    def start_http(self):
        self.log.info("Starting the HTTP thread")
        self._http_thread = threading.Thread(target=self._run_uvicorn,
                                             name="HTTP-Thread")
        self._http_thread.start()
        self.log.info("HTTP thread started")

    def stop_http(self):
        self.log.info("Stopping the HTTP thread ")
        self._uv_server.handle_exit(None, None)
        self.log.info("Waiting for the HTTP thread to finish")
        self._http_thread.join()

    def _run_uvicorn(self):
        self.log.info("Starting a new event loop in current thread")
        loop = uvloop.new_event_loop()
        #loop = asyncio.new_event_loop()
        asyncio.set_event_loop(loop)
        self.log.info("Running the uvicorn")
        from uvicorn import Server, Config
        uvconfig = Config(self._http_app,
                          port=5000,
                          log_level="debug",
                          loop="asyncio")
        self._uv_server = Server(uvconfig)
        self._uv_server.run()
def parse_cli():
    """Run the FastAPI server"""
    # Find numerical value of log level
    try:
        level_num = logger.level(LOG_LEVEL).no
    except ValueError as err:
        logging.error(
            f"Unknown log level {err}. Use one of {', '.join(logger._core.levels)}"
        )
        raise SystemExit()

    # Set up server
    server = Server(
        Config(
            "api.main:app",
            host="0.0.0.0",
            log_level=level_num,
            port=5000,
            proxy_headers=True,
        ), )

    # Set up logging last, to make sure no library overwrites it (they
    # shouldn't, but it happens)
    log.init(LOG_LEVEL)

    # Start server
    server.run()
Exemplo n.º 3
0
def start_uvicorn():
    uvicorn_config = Config('src.app:app',
                            host=settings.SERVICE_HOST,
                            port=settings.SERVICE_PORT,
                            log_level=settings.SERVICE_LOG_LEVEL.lower(),
                            loop="asyncio")
    server = Server(config=uvicorn_config)
    server.run()
Exemplo n.º 4
0
def uvicorn_serve(app: 'FastAPI'):
    from uvicorn import Config, Server
    config = Config(app=app,
                    host=server_config.HOST,
                    port=server_config.PORT,
                    loop='uvloop',
                    log_level='error')
    server = Server(config=config)
    server.run()
Exemplo n.º 5
0
def _start_uvicorn(app: 'FastAPI'):
    config = Config(app=app,
                    host=jinad_args.host,
                    port=jinad_args.port_expose,
                    loop='uvloop',
                    log_level='error')
    server = Server(config=config)
    server.run()
    daemon_logger.info('Goodbye!')
Exemplo n.º 6
0
def _start_uvicorn(app: 'FastAPI'):
    from .config import server_config
    config = Config(app=app,
                    host=server_config.HOST,
                    port=server_config.PORT,
                    loop='uvloop',
                    log_level='error')
    server = Server(config=config)
    server.run()
    daemon_logger.info('Bye!')
Exemplo n.º 7
0
def _start_uvicorn(app: 'FastAPI'):
    config = Config(app=app,
                    host=jinad_args.host,
                    port=jinad_args.port_expose,
                    loop='uvloop',
                    log_level='error')
    server = Server(config=config)
    server.run()
    from jina import __stop_msg__
    daemon_logger.success(__stop_msg__)
Exemplo n.º 8
0
def _start_uvicorn(app: 'FastAPI'):
    config = Config(
        app=app,
        host=jinad_args.host,
        port=jinad_args.port,
        loop='uvloop',
        log_level='error',
    )
    server = Server(config=config)
    server.run()
Exemplo n.º 9
0
class SwaVanRestMockServer(SwaVanMockTask):
    def __init__(self, _mock: Mock):
        self._id = _mock.id
        self._mock = _mock
        _headers = [('server', 'swavan')]
        _config = Config(app=SwaVanHttp(_mock).app,
                         host="localhost",
                         headers=_headers,
                         port=int(_mock.port),
                         access_log=False)
        if _mock.enable_https:
            _key = full_path("data/__certs__/swavan.key")
            _crt = full_path("data/__certs__/swavan.crt")
            if _mock.use_default_cert:
                if os.path.isfile(_key) and os.path.isfile(_crt):
                    _config.ssl_keyfile = _key
                    _config.ssl_certfile = _crt
                    SwaVanLogRecorder.send_log(f"Using default cert")
            else:
                if os.path.isfile(_mock.ssl_key_file_url) and os.path.isfile(
                        _mock.ssl_cert_file_url):
                    _config.ssl_keyfile = _mock.ssl_key_file_url
                    _config.ssl_certfile = _mock.ssl_cert_file_url
                    SwaVanLogRecorder.send_log(f"Using custom cert")
        self._core_server = Server(config=_config)

    def start(self):
        if self._core_server:
            self._core_server.run()

    def stop(self):
        self._core_server.should_exit = True

    def set_id(self, _id: str):
        self._id = _id

    def set_mock(self, _mock: Mock):
        self._mock = _mock

    @property
    def id(self) -> str:
        return self._mock.id
Exemplo n.º 10
0
class UvicornServer:
    app: FastAPI
    config: Config
    request_state: RequestState
    prerun_tasks: list[Callable[[], Any]] = field(default_factory=list)

    def __post_init__(self):
        self.config.callback_notify = self.on_tick

    @classmethod
    def from_request(cls, request: UvicornServerRequest) -> UvicornServer:
        app = FastAPI()
        return cls(
            app=app,
            config=Config(
                app,
                host=request.address,
                port=request.port,
                timeout_notify=0.25,
                log_config=None,
            ),
            request_state=request.request_state,
        )

    def create_server(self) -> ExplorerServer:
        self.server = Server(config=self.config)
        return ExplorerServer(main=self.run)

    async def on_tick(self) -> None:
        if self.request_state.scheduler_session.is_cancelled:
            print()  # Linebreak after the echoed ^C on the terminal.
            logger.info(" => Exiting...")
            self.server.should_exit = True

    def run(self) -> ExitCode:
        logging.info("Starting the Explorer Web UI server...")

        for task in self.prerun_tasks:
            task()

        self.server.run()
        return 0
Exemplo n.º 11
0
def read_root():
    my_logger.debug(
        f"{datetime.datetime.now()} - {str(random.randint(0, 100))}")
    print("Printing to STDOUT")
    return "Hello World!"


example_app_address = "host.docker.internal"
example_sba_address = "localhost"

pyctuator = Pyctuator(
    app,
    "Example FastAPI",
    app_url=f"http://{example_app_address}:8000",
    pyctuator_endpoint_url=f"http://{example_app_address}:8000/pyctuator",
    registration_url=f"http://{example_sba_address}:8080/instances",
    app_description=app.description,
)

# Keep the console clear - configure uvicorn (FastAPI's WSGI web app) not to log the detail of every incoming request
uvicorn_logger = logging.getLogger("uvicorn")
uvicorn_logger.setLevel(logging.WARNING)

server = Server(config=(Config(
    app=app,
    loop="asyncio",
    host="0.0.0.0",
    logger=uvicorn_logger,
)))
server.run()
Exemplo n.º 12
0
def main():
    config = Config(app=app, host='0.0.0.0', port=int(PORT), debug=True)
    server = Server(config=config)
    server.run()
Exemplo n.º 13
0
def run_server(
    context: Context = None,
    client: dask.distributed.Client = None,
    host: str = "0.0.0.0",
    port: int = 8080,
    startup=False,
    log_level=None,
    blocking: bool = True,
    jdbc_metadata: bool = False,
):  # pragma: no cover
    """
    Run a HTTP server for answering SQL queries using ``dask-sql``.
    It uses the `Presto Wire Protocol <https://github.com/prestodb/presto/wiki/HTTP-Protocol>`_
    for communication.
    This means, it has a single POST endpoint `/v1/statement`, which answers
    SQL queries (as string in the body) with the output as a JSON
    (in the format described in the documentation above).
    Every SQL expression that ``dask-sql`` understands can be used here.

    See :ref:`server` for more information.

    Note:
        The presto protocol also includes some statistics on the query
        in the response.
        These statistics are currently only filled with placeholder variables.

    Args:
        context (:obj:`dask_sql.Context`): If set, use this context instead of an empty one.
        client (:obj:`dask.distributed.Client`): If set, use this dask client instead of a new one.
        host (:obj:`str`): The host interface to listen on (defaults to all interfaces)
        port (:obj:`int`): The port to listen on (defaults to 8080)
        startup (:obj:`bool`): Whether to wait until Apache Calcite was loaded
        log_level: (:obj:`str`): The log level of the server and dask-sql
        blocking: (:obj:`bool`): If running in an environment with an event loop (e.g. a jupyter notebook),
                do not block. The server can be stopped with `context.stop_server()` afterwards.
        jdbc_metadata: (:obj:`bool`): If enabled create JDBC metadata tables using schemas and tables in
                the current dask_sql context

    Example:
        It is possible to run an SQL server by using the CLI script ``dask-sql-server``
        or by calling this function directly in your user code:

        .. code-block:: python

            from dask_sql import run_server

            # Create your pre-filled context
            c = Context()
            ...

            run_server(context=c)

        After starting the server, it is possible to send queries to it, e.g. with the
        `presto CLI <https://prestosql.io/docs/current/installation/cli.html>`_
        or via sqlalchemy (e.g. using the `PyHive <https://github.com/dropbox/PyHive#sqlalchemy>`_ package):

        .. code-block:: python

            from sqlalchemy.engine import create_engine
            engine = create_engine('presto://localhost:8080/')

            import pandas as pd
            pd.read_sql_query("SELECT 1 + 1", con=engine)

        Of course, it is also possible to call the usual ``CREATE TABLE``
        commands.

        If in a jupyter notebook, you should run the following code

        .. code-block:: python

            from dask_sql import Context

            c = Context()
            c.run_server(blocking=False)

            ...

            c.stop_server()

        Note:
            When running in a jupyter notebook without blocking,
            it is not possible to access the SQL server from within the
            notebook, e.g. using sqlalchemy.
            Doing so will deadlock infinitely.

    """
    _init_app(app, context=context, client=client)
    if jdbc_metadata:
        create_meta_data(context)

    if startup:
        app.c.sql("SELECT 1 + 1").compute()

    config = Config(app, host=host, port=port, log_level=log_level)
    server = Server(config=config)

    loop = asyncio.get_event_loop()
    if blocking:
        if loop and loop.is_running():
            apply(loop=loop)

        server.run()
    else:
        if not loop or not loop.is_running():
            raise AttributeError(
                "blocking=True needs a running event loop (e.g. in a jupyter notebook)"
            )
        loop.create_task(server.serve())
        context.sql_server = server
Exemplo n.º 14
0
def main():
    server = Server(Config("fad.fad:app", host="0.0.0.0", port=5000), )

    server.run()
Exemplo n.º 15
0
def run_server():
    config = Config(app=app_main, host=APP_HOST, port=APP_PORT)
    server = Server(config=config)

    server.run()