From 5994d513c8fe03d396314d4ac6ca3bfe5fb08e13 Mon Sep 17 00:00:00 2001 From: WildSoft Date: Mon, 16 Nov 2026 18:59:45 +0300 Subject: [PATCH] Update public source --- src/request_lifecycle.py | 62 +++++++++++++++++++++++++++++----------- 1 file changed, 45 insertions(+), 17 deletions(-) diff --git a/src/request_lifecycle.py b/src/request_lifecycle.py index 2ef613c..196019e 100644 --- a/src/request_lifecycle.py +++ b/src/request_lifecycle.py @@ -13,6 +13,13 @@ class RequestLifecycleMiddleware: ("POST", "/gps-test/stop"), } + UNTRACKED_DURING_SHUTDOWN = { + ("GET", "/"), + ("HEAD", "/"), + ("GET", "/api/status"), + ("GET", "/api/gps-test"), + } + STATIC_PREFIXES = ( "/static/", "/reports/static/", @@ -99,31 +106,52 @@ class RequestLifecycleMiddleware: method = environ.get("REQUEST_METHOD", "GET").upper() path = environ.get("PATH_INFO", "/") + track_request = True + with self._condition: - if ( - self._shutdown_requested - and not self._is_allowed_during_shutdown( + if self._shutdown_requested: + + if not self._is_allowed_during_shutdown( method, path - ) - ): + ): - body = b"Application is shutting down.\n" + body = b"Application is shutting down.\n" - start_response( - "503 Service Unavailable", - [ - ("Content-Type", "text/plain; charset=utf-8"), - ("Content-Length", str(len(body))), - ("Retry-After", "5"), - ("Cache-Control", "no-store"), - ] - ) + start_response( + "503 Service Unavailable", + [ + ("Content-Type", "text/plain; charset=utf-8"), + ("Content-Length", str(len(body))), + ("Retry-After", "5"), + ("Cache-Control", "no-store"), + ] + ) - return [body] + return [body] - self._active_requests += 1 + if ( + (method, path) in self.UNTRACKED_DURING_SHUTDOWN + or ( + method in ("GET", "HEAD") + and any( + path.startswith(prefix) + for prefix in self.STATIC_PREFIXES + ) + ) + ): + + track_request = False + + if track_request: + self._active_requests += 1 + + if not track_request: + return self.application( + environ, + start_response + ) try: