Skip to content

Use a Docker healthcheck for the Flask servers #2162

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

Merged
merged 2 commits into from
Jul 12, 2024
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
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,9 @@ cumulative_timing = false
[tool.coverage.run]

branch = true
omit = [
"src/mock_vws/_flask_server/healthcheck.py",
]

[tool.coverage.report]

Expand Down
1 change: 1 addition & 0 deletions src/mock_vws/_flask_server/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ RUN pip install --no-cache-dir uv==0.1.44 && \
uv pip install --no-cache-dir --upgrade --editable .
EXPOSE 5000
ENTRYPOINT ["python"]
HEALTHCHECK --interval=1s --timeout=10s --start-period=5s --retries=3 CMD ["python", "/app/src/mock_vws/_flask_server/healthcheck.py"]

FROM base as vws
ENV VWS_HOST=0.0.0.0
Expand Down
32 changes: 32 additions & 0 deletions src/mock_vws/_flask_server/healthcheck.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""
Health check for the Flask server.
"""

import http.client
import socket
import sys
from http import HTTPStatus


def flask_app_healthy(port: int) -> bool:
"""
Check if the Flask app is healthy.
"""
conn = http.client.HTTPConnection("localhost", port)
try:
conn.request("GET", "/some-random-endpoint")
response = conn.getresponse()
except (TimeoutError, http.client.HTTPException, socket.gaierror):
return False
finally:
conn.close()

return response.status in {
HTTPStatus.NOT_FOUND,
HTTPStatus.UNAUTHORIZED,
HTTPStatus.FORBIDDEN,
}


if __name__ == "__main__":
sys.exit(int(not flask_app_healthy(port=5000)))
30 changes: 12 additions & 18 deletions tests/mock_vws/test_docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,11 @@
import io
from collections.abc import Iterator

from docker.models.containers import Container
from docker.models.images import Image
from docker.models.networks import Network


# We do not cover this function because hitting particular branches depends on
# timing.
@retry(
wait=wait_fixed(wait=0.5),
stop=stop_after_delay(max_delay=20),
Expand All @@ -37,21 +36,18 @@
),
reraise=True,
)
def wait_for_flask_app_to_start(base_url: str) -> None: # pragma: no cover
def wait_for_health_check(container: Container) -> None:
"""
Wait for a server to start.

Args:
base_url: The base URL of the Flask app to wait for.
Wait for a container to pass its health check.
"""
url = f"{base_url}/{uuid.uuid4().hex}"
response = requests.get(url=url, timeout=30)
if response.status_code not in {
HTTPStatus.NOT_FOUND,
HTTPStatus.UNAUTHORIZED,
HTTPStatus.FORBIDDEN,
}:
error_message = f"Could not connect to {url}"
container.reload()
health_status = container.attrs["State"]["Health"]["Status"]

Check warning on line 44 in tests/mock_vws/test_docker.py

View check run for this annotation

Codecov / codecov/patch

tests/mock_vws/test_docker.py#L43-L44

Added lines #L43 - L44 were not covered by tests
# In theory this might not be hit by coverage.
# Let's keep it required by coverage for now.
if health_status != "healthy":
error_message = (

Check warning on line 48 in tests/mock_vws/test_docker.py

View check run for this annotation

Codecov / codecov/patch

tests/mock_vws/test_docker.py#L48

Added line #L48 was not covered by tests
f"Container {container.name} is not healthy: {health_status}"
)
raise ValueError(error_message)


Expand Down Expand Up @@ -186,6 +182,7 @@
)

for container in (target_manager_container, vws_container, vwq_container):
wait_for_health_check(container=container)

Check warning on line 185 in tests/mock_vws/test_docker.py

View check run for this annotation

Codecov / codecov/patch

tests/mock_vws/test_docker.py#L185

Added line #L185 was not covered by tests
container.reload()

target_manager_port_attrs = target_manager_container.attrs[
Expand Down Expand Up @@ -213,9 +210,6 @@
f"http://{target_manager_host_ip}:{target_manager_host_port}"
)

for base_url in (base_vws_url, base_vwq_url, base_target_manager_url):
wait_for_flask_app_to_start(base_url=base_url)

response = requests.post(
url=f"{base_target_manager_url}/databases",
json=database.to_dict(),
Expand Down