-
Notifications
You must be signed in to change notification settings - Fork 138
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore: address deprecations caused by updating packages #570
Changes from all commits
abf43c9
5854bde
8a5de64
e96da4b
0a90aa9
4808bb8
591950c
7393e2d
afde0ab
c42bf81
3b9d354
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,10 @@ | |
import asyncio | ||
import os | ||
import pprint | ||
from collections.abc import AsyncIterator | ||
from contextlib import asynccontextmanager | ||
from typing import Any | ||
from typing import Never | ||
|
||
import starlette.routing | ||
from fastapi import FastAPI | ||
|
@@ -64,6 +67,57 @@ def openapi(self) -> dict[str, Any]: | |
return self.openapi_schema | ||
|
||
|
||
@asynccontextmanager | ||
async def lifespan(asgi_app: BanchoAPI) -> AsyncIterator[Never]: | ||
app.state.loop = asyncio.get_running_loop() | ||
if app.utils.is_running_as_admin(): | ||
log( | ||
"Running the server with root privileges is not recommended.", | ||
Ansi.LRED, | ||
) | ||
|
||
await app.state.services.database.connect() | ||
await app.state.services.redis.initialize() | ||
|
||
if app.state.services.datadog is not None: | ||
app.state.services.datadog.start( | ||
flush_in_thread=True, | ||
flush_interval=15, | ||
) | ||
app.state.services.datadog.gauge("bancho.online_players", 0) | ||
|
||
app.state.services.ip_resolver = app.state.services.IPResolver() | ||
|
||
await app.state.services.run_sql_migrations() | ||
|
||
async with app.state.services.database.connection() as db_conn: | ||
await collections.initialize_ram_caches(db_conn) | ||
|
||
await app.bg_loops.initialize_housekeeping_tasks() | ||
|
||
log("Startup process complete.", Ansi.LGREEN) | ||
log( | ||
f"Listening @ {app.settings.APP_HOST}:{app.settings.APP_PORT}", | ||
Ansi.LMAGENTA, | ||
) | ||
|
||
yield # type: ignore | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. mypy will scream about not returning anything with we could also yield a dictionary e.g. yield {
"db": db,
"redis": redis,
"crypto_model": crypto_model,
"osu_service": osu_service,
"stats_service": stats_service,
} and then it would be available under There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just want to make sure, is this a 1:1 copy other than the yield? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep |
||
|
||
# we want to attempt to gracefully finish any ongoing connections | ||
# and shut down any of the housekeeping tasks running in the background. | ||
await app.state.sessions.cancel_housekeeping_tasks() | ||
|
||
# shutdown services | ||
|
||
await app.state.services.http_client.aclose() | ||
await app.state.services.database.disconnect() | ||
await app.state.services.redis.aclose() | ||
|
||
if app.state.services.datadog is not None: | ||
app.state.services.datadog.stop() | ||
app.state.services.datadog.flush() | ||
|
||
|
||
def init_exception_handlers(asgi_app: BanchoAPI) -> None: | ||
@asgi_app.exception_handler(RequestValidationError) | ||
async def handle_validation_error( | ||
|
@@ -110,61 +164,6 @@ async def http_middleware( | |
raise exc | ||
|
||
|
||
def init_events(asgi_app: BanchoAPI) -> None: | ||
"""Initialize our app's event handlers.""" | ||
|
||
@asgi_app.on_event("startup") | ||
async def on_startup() -> None: | ||
app.state.loop = asyncio.get_running_loop() | ||
|
||
if app.utils.is_running_as_admin(): | ||
log( | ||
"Running the server with root privileges is not recommended.", | ||
Ansi.LRED, | ||
) | ||
|
||
await app.state.services.database.connect() | ||
await app.state.services.redis.initialize() | ||
|
||
if app.state.services.datadog is not None: | ||
app.state.services.datadog.start( | ||
flush_in_thread=True, | ||
flush_interval=15, | ||
) | ||
app.state.services.datadog.gauge("bancho.online_players", 0) | ||
|
||
app.state.services.ip_resolver = app.state.services.IPResolver() | ||
|
||
await app.state.services.run_sql_migrations() | ||
|
||
async with app.state.services.database.connection() as db_conn: | ||
await collections.initialize_ram_caches(db_conn) | ||
|
||
await app.bg_loops.initialize_housekeeping_tasks() | ||
|
||
log("Startup process complete.", Ansi.LGREEN) | ||
log( | ||
f"Listening @ {app.settings.APP_HOST}:{app.settings.APP_PORT}", | ||
Ansi.LMAGENTA, | ||
) | ||
|
||
@asgi_app.on_event("shutdown") | ||
async def on_shutdown() -> None: | ||
# we want to attempt to gracefully finish any ongoing connections | ||
# and shut down any of the housekeeping tasks running in the background. | ||
await app.state.sessions.cancel_housekeeping_tasks() | ||
|
||
# shutdown services | ||
|
||
await app.state.services.http_client.aclose() | ||
await app.state.services.database.disconnect() | ||
await app.state.services.redis.close() | ||
|
||
if app.state.services.datadog is not None: | ||
app.state.services.datadog.stop() | ||
app.state.services.datadog.flush() | ||
|
||
|
||
def init_routes(asgi_app: BanchoAPI) -> None: | ||
"""Initialize our app's route endpoints.""" | ||
for domain in ("ppy.sh", app.settings.DOMAIN): | ||
|
@@ -180,11 +179,10 @@ def init_routes(asgi_app: BanchoAPI) -> None: | |
|
||
def init_api() -> BanchoAPI: | ||
"""Create & initialize our app.""" | ||
asgi_app = BanchoAPI() | ||
asgi_app = BanchoAPI(lifespan=lifespan) | ||
|
||
init_middlewares(asgi_app) | ||
init_exception_handlers(asgi_app) | ||
init_events(asgi_app) | ||
init_routes(asgi_app) | ||
|
||
return asgi_app | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -239,5 +239,5 @@ async def test_score_submission( | |
assert response.status_code == status.HTTP_200_OK | ||
assert ( | ||
response.read() | ||
== b"beatmapId:315|beatmapSetId:141|beatmapPlaycount:1|beatmapPasscount:1|approvedDate:2014-05-18 15:41:48|\n|chartId:beatmap|chartUrl:https://osu.cmyui.xyz/s/141|chartName:Beatmap Ranking|rankBefore:|rankAfter:1|rankedScoreBefore:|rankedScoreAfter:26810|totalScoreBefore:|totalScoreAfter:26810|maxComboBefore:|maxComboAfter:52|accuracyBefore:|accuracyAfter:81.94|ppBefore:|ppAfter:9.56523|onlineScoreId:1|\n|chartId:overall|chartUrl:https://cmyui.xyz/u/3|chartName:Overall Ranking|rankBefore:|rankAfter:1|rankedScoreBefore:|rankedScoreAfter:26810|totalScoreBefore:|totalScoreAfter:26810|maxComboBefore:|maxComboAfter:52|accuracyBefore:|accuracyAfter:81.94|ppBefore:|ppAfter:10|achievements-new:osu-skill-pass-4+Insanity Approaches+You're not twitching, you're just ready./all-intro-hidden+Blindsight+I can see just perfectly" | ||
== b"beatmapId:315|beatmapSetId:141|beatmapPlaycount:1|beatmapPasscount:1|approvedDate:2014-05-18 15:41:48|\n|chartId:beatmap|chartUrl:https://osu.cmyui.xyz/s/141|chartName:Beatmap Ranking|rankBefore:|rankAfter:1|rankedScoreBefore:|rankedScoreAfter:26810|totalScoreBefore:|totalScoreAfter:26810|maxComboBefore:|maxComboAfter:52|accuracyBefore:|accuracyAfter:81.94|ppBefore:|ppAfter:10.04103|onlineScoreId:1|\n|chartId:overall|chartUrl:https://cmyui.xyz/u/3|chartName:Overall Ranking|rankBefore:|rankAfter:1|rankedScoreBefore:|rankedScoreAfter:26810|totalScoreBefore:|totalScoreAfter:26810|maxComboBefore:|maxComboAfter:52|accuracyBefore:|accuracyAfter:81.94|ppBefore:|ppAfter:10|achievements-new:osu-skill-pass-4+Insanity Approaches+You're not twitching, you're just ready./all-intro-hidden+Blindsight+I can see just perfectly" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. updated value due to changed pp calcs There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should prolly use snapshot tests for tests like these There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also the test suite runs on the docker-compose db rn which is super weird - we should be spinning up a temp db & running migrations every time we run tests |
||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -263,7 +263,7 @@ async def main(argv: Sequence[str] | None = None) -> int: | |
|
||
await app.state.services.http_client.aclose() | ||
await db.disconnect() | ||
await redis.close() | ||
await redis.aclose() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
return 0 | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should this be AsyncIterator[None]?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i remember trying that and it failing mypy