Skip to content

Commit f1ef2c5

Browse files
committed
Add middleware to check service status (e.g., "read-only", "down for maintenance") and provide suitable error responses
1 parent b48f05d commit f1ef2c5

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

api/simqueue/main.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22
from contextlib import asynccontextmanager
3-
from fastapi import FastAPI
3+
from fastapi import FastAPI, Request
4+
from fastapi.responses import JSONResponse
45
from fastapi.staticfiles import StaticFiles
56
from starlette.middleware.sessions import SessionMiddleware
67
from starlette.middleware.cors import CORSMiddleware
@@ -50,6 +51,12 @@ async def lifespan(app: FastAPI):
5051
await database.disconnect()
5152

5253

54+
service_status = getattr(settings, "SERVICE_STATUS", "ok")
55+
if service_status != "ok":
56+
warning_message = f"---\n> ⚠️ **_NOTE:_** _{service_status}_\n---\n\n"
57+
description = warning_message + description
58+
59+
5360
app = FastAPI(
5461
title="EBRAINS Neuromorphic Computing Job Queue API",
5562
description=description,
@@ -66,6 +73,21 @@ async def lifespan(app: FastAPI):
6673
allow_headers=["*"],
6774
)
6875

76+
77+
@app.middleware("http")
78+
async def check_service_status(request: Request, call_next):
79+
if request.url.path != "/" and (
80+
"down" in service_status
81+
or ("read-only" in service_status and request.method in ("POST", "PUT", "PATCH"))
82+
):
83+
return JSONResponse(
84+
status_code=503,
85+
content={"error": service_status},
86+
)
87+
response = await call_next(request)
88+
return response
89+
90+
6991
app.include_router(for_users.router, tags=["For all users"])
7092
app.include_router(for_providers.router, tags=["For use by computing system providers"])
7193
app.include_router(for_admins.router, tags=["For use by administrators"])

api/simqueue/resources/for_users.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,11 @@ async def _check_auth_for_list(token, api_key, collab, user_id, hardware_platfor
9292

9393
@router.get("/")
9494
def about_this_api():
95+
service_status = getattr(settings, "SERVICE_STATUS", "ok")
9596
return {
9697
"about": "This is the EBRAINS Neuromorphic Computing Job Queue API.",
9798
"version": "3",
99+
"status": service_status,
98100
"links": {"documentation": "/docs"},
99101
"authentication": {
100102
"server": settings.EBRAINS_IAM_SERVICE_URL,

api/simqueue/settings.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,7 @@
2424
EMAIL_SENDER = "[email protected]"
2525
EMAIL_PASSWORD = os.environ.get("NMPI_EMAIL_PASSWORD", None)
2626
ADMIN_EMAIL = os.environ.get("NMPI_ADMIN_EMAIL")
27+
28+
# SERVICE_STATUS = "The service is currently in read-only mode for maintenance"
29+
# SERVICE_STATUS = "The service is currently down for maintenance. We expect service to be restored on 4th December 2025"
30+
SERVICE_STATUS = os.environ.get("NMPI_SERVICE_STATUS", "ok")

0 commit comments

Comments
 (0)