Skip to content
This repository was archived by the owner on Jan 12, 2022. It is now read-only.

Commit 6a94c0f

Browse files
committed
handle health checks in middleware
1 parent 907d9aa commit 6a94c0f

2 files changed

Lines changed: 29 additions & 1 deletion

File tree

multicore_runtime/middleware.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
import logging
1818
import os
1919

20+
from werkzeug.wrappers import Request
21+
from werkzeug.wrappers import Response
22+
2023

2124
RESERVED_ENV_KEYS = {
2225
'AUTH_DOMAIN': 'gmail.com', # Default auth domain must be set.
@@ -160,3 +163,23 @@ def get_env_to_hide_service_bridge(wsgi_env):
160163
'SERVER_PORT', https)
161164

162165
return output
166+
167+
168+
def health_check_middleware(app):
169+
"""Intercept requests to /_ah/health and respond healthy (200 OK).
170+
171+
Args:
172+
app: The WSGI app to wrap.
173+
174+
Returns:
175+
The wrapped app, also a WSGI app.
176+
"""
177+
178+
def health_check_intercept_wrapper(wsgi_env, start_response): # pylint: disable=missing-docstring
179+
request = Request(wsgi_env)
180+
if request.path == '/_ah/health': # Only intercept exact matches.
181+
return Response('healthy', status=200)(wsgi_env, start_response)
182+
else:
183+
return app(wsgi_env, start_response)
184+
185+
return health_check_intercept_wrapper

multicore_runtime/wsgi.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
from dispatcher import dispatcher
2727
from middleware import reset_environment_middleware
28+
from middleware import health_check_middleware
2829
from wsgi_config import env_vars_from_env_config
2930
from wsgi_config import get_module_config
3031
from wsgi_config import get_module_config_filename
@@ -83,7 +84,11 @@
8384
# layer (the middleware code that will process a request first). Inside the
8485
# innermost layer is the actual dispatcher, above.
8586

87+
# Intercept health check requests on /_ah/health. This is a temporary measure
88+
# until container-level health check handlers are in place and turned on.
89+
meta_app = health_check_middleware(meta_app)
90+
8691
# Reset os.environ to the frozen state and add request-specific data.
8792
meta_app = reset_environment_middleware(meta_app, frozen_environment,
8893
frozen_user_env,
89-
frozen_env_config_env)
94+
frozen_env_config_env)

0 commit comments

Comments
 (0)