Skip to content

Commit

Permalink
Add healthcheck command to Dockerfile
Browse files Browse the repository at this point in the history
  • Loading branch information
CollinHeist committed Oct 26, 2023
1 parent c5cfd34 commit c0df5fe
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 12 deletions.
22 changes: 12 additions & 10 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,27 @@ ENV TCM_PREFERENCES=/config/preferences.yml \
TCM_IS_DOCKER=TRUE \
TZ=UTC

# Delete setup files
# Create user and group to run the container
# Clean up apt cache
RUN set -eux && \
# Finalize setup
RUN \
# Create user and group to run TCM
set -eux && \
rm -f Pipfile Pipfile.lock && \
groupadd -g 314 titlecardmaker && \
useradd -u 314 -g 314 titlecardmaker && \
# Install imagemagick and curl (for healthcheck)
apt-get update && \
apt-get install -y --no-install-recommends imagemagick libmagickcore-6.q16-6-extra && \
rm -rf /var/lib/apt/lists/* && \
cp modules/ref/policy.xml /etc/ImageMagick-6/policy.xml
apt-get install -y --no-install-recommends curl imagemagick libmagickcore-6.q16-6-extra && \
cp modules/ref/policy.xml /etc/ImageMagick-6/policy.xml && \
# Remove apt cache and setup files
rm -rf /tmp/* /var/tmp/* /var/lib/apt/lists/*

# Expose TCM Port
EXPOSE 4242

# Healthcheck command
HEALTHCHECK --interval=5m --timeout=20s --start-period=3m --start-interval=10s \
CMD curl --fail http://localhost:4242/ || exit 1
HEALTHCHECK --interval=3m --timeout=10s --start-period=3m --start-interval=10s \
CMD curl --fail http://0.0.0.0:4242/api/healthcheck || exit 1

# Entrypoint
CMD ["python3", "-m", "uvicorn", "app-main:app", "--host", "0.0.0.0", "--port", "4242"]
ENTRYPOINT ["bash", "./start.sh"]
ENTRYPOINT ["bash", "./start.sh"]
26 changes: 25 additions & 1 deletion app/routers/api.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from fastapi import APIRouter
from fastapi import APIRouter, Depends, HTTPException, Request
from sqlalchemy import text
from sqlalchemy.orm import Session

from app.dependencies import get_database
from app.routers.auth import auth_router
from app.routers.availability import availablility_router
from app.routers.blueprint import blueprint_router
Expand Down Expand Up @@ -41,3 +44,24 @@
api_router.include_router(sync_router)
api_router.include_router(template_router)
api_router.include_router(translation_router)

# Create ping API endpoint
@api_router.get('/healthcheck')
def health_check(
request: Request,
db: Session = Depends(get_database),
) -> None:
"""
Check the health of the TCM server by attempting to perform a dummy
database operation; raising an HTTPException (500) if a connection
cannot be established.
"""

try:
db.execute(text('SELECT 1'))
except Exception as exc:
request.state.log.exception(f'Health check failed - {exc}', exc)
raise HTTPException(
status_code=500,
detail=f'Database returned some error - {exc}'
) from exc
2 changes: 1 addition & 1 deletion modules/ref/version_webui
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v2.0-alpha.6.0-webui120
v2.0-alpha.6.0-webui121

0 comments on commit c0df5fe

Please sign in to comment.