Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
@app.listener("after_server_start")
async def do_after_server_start(*args, **kwargs):
nonlocal after_server_start
after_server_start = True
@app.listener("before_server_stop")
async def do_before_server_stop(*args, **kwargs):
nonlocal before_server_stop
before_server_stop = True
@app.listener("after_server_stop")
async def do_after_server_stop(*args, **kwargs):
nonlocal after_server_stop
after_server_stop = 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):
server.run()
for task in asyncio.Task.all_tasks():
task.cancel()
assert before_server_start
assert after_server_start
assert before_server_stop
assert after_server_stop
if self.should_bcast_check():
bcast_up = await is_broadcast_up()
if not bcast_up:
logger.critical(
"run@api_server.py - Broadcast (queue) not up error. please check logs"
)
return self.exit(1)
if self.option("db_seed_env", False):
self.seed_from_env()
# taken from uvicorn/main.py:run
logger.debug("run@api_server.py - Building Uvicorn Config and Server")
config = UvicornConfig(app, log_config=self.get_uvicorn_logging(), **kwargs)
server = UvicornServer(config=config)
if self.option("force_exit"):
server.force_exit = True
if isinstance(app, str) and (config.debug or config.reload):
logger.warning(f"run@api_server.py - Running boucanpy api in dev mode...")
sock = config.bind_socket()
supervisor = StatReload(config)
return supervisor.run(server.run, sockets=[sock])
elif config.workers > 1:
sock = config.bind_socket()
supervisor = Multiprocess(config)
logger.warning(
f"run@api_server.py - Running boucanpy api in worker mode..."
)
return supervisor.run(server.run, sockets=[sock])
else:
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 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()