From ff4e2fa6fee5ea18f053bdbd3f70ef461c79aa6f Mon Sep 17 00:00:00 2001 From: MoritzWeber Date: Tue, 28 May 2024 09:55:01 +0200 Subject: [PATCH] fix: Termination of sessions The automatic termination of idle sessions stopped working with #1573. Reason is that the app label was removed from Prometheus. This commit migrates the old app references to the new session_id label. --- backend/capellacollab/sessions/idletimeout.py | 2 +- backend/capellacollab/sessions/injection.py | 2 +- backend/tests/sessions/test_session_idletimeout.py | 4 +++- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/backend/capellacollab/sessions/idletimeout.py b/backend/capellacollab/sessions/idletimeout.py index 164b0f9e1..f3a410628 100644 --- a/backend/capellacollab/sessions/idletimeout.py +++ b/backend/capellacollab/sessions/idletimeout.py @@ -29,7 +29,7 @@ def terminate_idle_session(): log.error("Could not collect idle sessions from Prometheus") return for metric in response.json()["data"]["result"]: - if session_id := metric.get("metric", {}).get("app"): + if session_id := metric.get("metric", {}).get("session_id"): log.info("Terminating idle session %s", session_id) with database.SessionLocal() as db: if session := crud.get_session_by_id(db, session_id): diff --git a/backend/capellacollab/sessions/injection.py b/backend/capellacollab/sessions/injection.py index 5ed6d45e2..06effd3f7 100644 --- a/backend/capellacollab/sessions/injection.py +++ b/backend/capellacollab/sessions/injection.py @@ -28,7 +28,7 @@ def get_last_seen(sid: str) -> str: response.raise_for_status() for session in response.json()["data"]["result"]: - if sid == session["metric"]["app"]: + if sid == session["metric"]["session_id"]: return _get_last_seen(float(session["value"][1])) log.error("No session was found.") diff --git a/backend/tests/sessions/test_session_idletimeout.py b/backend/tests/sessions/test_session_idletimeout.py index 611cd9465..bc5c55420 100644 --- a/backend/tests/sessions/test_session_idletimeout.py +++ b/backend/tests/sessions/test_session_idletimeout.py @@ -53,7 +53,9 @@ def test_idle_sessions(monkeypatch): monkeypatch.setattr( requests, "get", - lambda *args, **kwargs: MockResponse({"metric": {"app": session_id}}), + lambda *args, **kwargs: MockResponse( + {"metric": {"session_id": session_id}} + ), ) monkeypatch.setattr( capellacollab.sessions.operators,