Skip to content

Commit 3389892

Browse files
authored
Merge pull request #147 from Flagsmith/chore/warnings
feat: Replace deprecated FastAPI on_event with lifespan event handlers
2 parents 115c56b + 7481537 commit 3389892

File tree

5 files changed

+19
-201
lines changed

5 files changed

+19
-201
lines changed

pyproject.toml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,6 @@ dev-dependencies = [
3636
[tool.ruff]
3737
line-length = 88
3838

39-
[tool.ruff.lint.isort]
40-
known-first-party = ['fastapi_utils']
41-
4239
[tool.ruff.lint.mccabe]
4340
max-complexity = 10
4441

src/edge_proxy/server.py

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
from datetime import datetime, timedelta
2+
import asyncio
23

34
import httpx
4-
import structlog
5+
from contextlib import asynccontextmanager
56
from fastapi import FastAPI, Header
67
from fastapi.middleware.cors import CORSMiddleware
78
from fastapi.middleware.gzip import GZipMiddleware
89
from fastapi.responses import ORJSONResponse, Response
910

1011
from edge_proxy.health_check.responses import HealthCheckResponse
11-
from fastapi_utils.tasks import repeat_every
1212

1313
from edge_proxy.cache import LocalMemEnvironmentsCache
1414
from edge_proxy.environments import EnvironmentService
@@ -24,7 +24,23 @@
2424
httpx.AsyncClient(timeout=settings.api_poll_timeout_seconds),
2525
settings,
2626
)
27-
app = FastAPI()
27+
28+
29+
async def poll_environments():
30+
while True:
31+
await environment_service.refresh_environment_caches()
32+
await asyncio.sleep(settings.api_poll_frequency_seconds)
33+
34+
35+
@asynccontextmanager
36+
async def lifespan(app: FastAPI):
37+
await environment_service.refresh_environment_caches()
38+
poll = asyncio.create_task(poll_environments())
39+
yield
40+
poll.cancel()
41+
42+
43+
app = FastAPI(lifespan=lifespan)
2844

2945

3046
@app.exception_handler(FlagsmithUnknownKeyError)
@@ -109,16 +125,6 @@ async def get_identities(
109125
return ORJSONResponse(data)
110126

111127

112-
@app.on_event("startup")
113-
@repeat_every(
114-
seconds=settings.api_poll_frequency_seconds,
115-
raise_exceptions=True,
116-
logger=structlog.get_logger(__name__),
117-
)
118-
async def refresh_cache():
119-
await environment_service.refresh_environment_caches()
120-
121-
122128
app.add_middleware(
123129
CORSMiddleware,
124130
allow_origins=settings.allow_origins,

src/fastapi_utils/LICENSE

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/fastapi_utils/tasks.py

Lines changed: 0 additions & 77 deletions
This file was deleted.

src/fastapi_utils/test_tasks.py

Lines changed: 0 additions & 87 deletions
This file was deleted.

0 commit comments

Comments
 (0)