Skip to content
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

Staging #279

Merged
merged 2 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 21 additions & 17 deletions openaq_api/openaq_api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
locations,
manufacturers,
measurements,
owners,
parameters,
providers,
sensors,
Expand Down Expand Up @@ -107,22 +108,6 @@ def render(self, content: Any) -> bytes:

redis_client = None # initialize for generalize_schema.py

if settings.RATE_LIMITING:
logger.debug("Connecting to redis")
from redis.asyncio.cluster import RedisCluster

try:
redis_client = RedisCluster(
host=settings.REDIS_HOST,
port=settings.REDIS_PORT,
decode_responses=True,
socket_timeout=5,
)
app.state.redis_client = redis_client
except Exception as e:
logging.error(InfrastructureErrorLog(detail=f"failed to connect to redis: {e}"))
logger.debug("Redis connected")


app.add_middleware(
CORSMiddleware,
Expand Down Expand Up @@ -207,7 +192,24 @@ async def startup_event():
logger.debug("initializing connection pool")
app.state.pool = await db_pool(None)
logger.debug("Connection pool established")

if not hasattr(app.state, "redis_client"):
if settings.RATE_LIMITING:
logger.debug("Connecting to redis")
from redis.asyncio.cluster import RedisCluster

try:
redis_client = RedisCluster(
host=settings.REDIS_HOST,
port=settings.REDIS_PORT,
decode_responses=True,
socket_timeout=5,
)
app.state.redis_client = redis_client
except Exception as e:
logging.error(
InfrastructureErrorLog(detail=f"failed to connect to redis: {e}")
)
logger.debug("Redis connected")
if hasattr(app.state, "counter"):
app.state.counter += 1
else:
Expand Down Expand Up @@ -244,6 +246,7 @@ def pong():
def favico():
return RedirectResponse("https://openaq.org/assets/graphics/meta/favicon.png")


# v3
app.include_router(instruments.router)
app.include_router(locations.router)
Expand All @@ -252,6 +255,7 @@ def favico():
app.include_router(countries.router)
app.include_router(manufacturers.router)
app.include_router(measurements.router)
app.include_router(owners.router)
app.include_router(trends.router)
app.include_router(providers.router)
app.include_router(sensors.router)
Expand Down
2 changes: 1 addition & 1 deletion openaq_api/openaq_api/v3/models/responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ class Provider(ProviderBase):


class Owner(OwnerBase):
entity: EntityBase
locations_count: int = Field(alias='locationsCount')


class Instrument(InstrumentBase):
Expand Down