Skip to content

Commit

Permalink
Add healthcheck
Browse files Browse the repository at this point in the history
  • Loading branch information
atrakic committed Jun 13, 2024
1 parent 9fe8f88 commit bfaa7aa
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 13 deletions.
9 changes: 7 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
MAKEFLAGS += --silent
.DEFAULT_GOAL := help

compose: ## Run with docker compose
APP ?= app

up: ## Run with docker compose
COMPOSE_DOCKER_CLI_BUILD=1 DOCKER_BUILDKIT=1 docker-compose up --build --remove-orphans --force-recreate -d

healthcheck:
docker inspect $(APP) --format "{{ (index (.State.Health.Log) 0).Output }}"

pylint: ## Run pylint
pylint $(shell git ls-files '*.py')

Expand All @@ -29,6 +34,6 @@ help:
clean: ## Clean up
docker-compose rm -s -a -v -f || true

.PHONY: compose pylint clean help release
.PHONY: up pylint clean help release

-include include.mk
11 changes: 8 additions & 3 deletions compose.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
version: "3.8"

services:
app:
build:
context: ./src
container_name: app
healthcheck:
test:
- CMD-SHELL
- python -c 'import urllib.request; print(urllib.request.urlopen("http://localhost:3000/healthcheck").read());'
interval: 10s
timeout: 3s
retries: 3
ports:
- 3000:3000
container_name: fastapi-htmx-todo
14 changes: 12 additions & 2 deletions src/database.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
"""Db module."""

import os
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker

SQLALCHEMY_DATABASE_URL = os.getenv("SQLALCHEMY_DATABASE_URL", "sqlite:////tmp/db.sqlite")

SQLALCHEMY_DATABASE_URL = os.getenv(
"SQLALCHEMY_DATABASE_URL", "sqlite:////tmp/db.sqlite"
)
engine = create_engine(
SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False}
)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)

Base = declarative_base()


def get_db():
db_name = SessionLocal()
try:
yield db_name
finally:
db_name.close()
10 changes: 4 additions & 6 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from database import Base
from database import SessionLocal
from database import engine
from database import get_db

from models import create_todo, delete_todo, get_todo, get_todos, update_todo

Expand All @@ -24,12 +25,9 @@
templates = Jinja2Templates(directory="templates")


def get_db():
db_name = SessionLocal()
try:
yield db_name
finally:
db_name.close()
@app.get("/healthcheck")
def root():
return {"message": "Healthy"}


@app.get("/", response_class=HTMLResponse)
Expand Down

0 comments on commit bfaa7aa

Please sign in to comment.