add /health path which checks DB availability

Signed-off-by: Jakub Sokołowski <jakub@status.im>
This commit is contained in:
Jakub Sokołowski 2023-07-04 20:54:54 +02:00
parent 63cfa96a07
commit 3ff878b84a
No known key found for this signature in database
GPG Key ID: FE65CD384D5BF7B4
5 changed files with 1268 additions and 1272 deletions

View File

@ -1,13 +1,16 @@
import os import os
import logging
import warnings import warnings
from uuid import uuid4 from uuid import uuid4
from flask import Flask, render_template from flask import Flask, render_template, url_for
from flask_sqlalchemy import SQLAlchemy from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager from flask_login import LoginManager
from werkzeug.exceptions import HTTPException from werkzeug.exceptions import HTTPException
from flask_migrate import Migrate from flask_migrate import Migrate
from flask_admin import Admin from flask_admin import Admin
from sqlalchemy.sql import text
from healthcheck import HealthCheck
from app.logger import log from app.logger import log
@ -16,7 +19,6 @@ login_manager = LoginManager()
db = SQLAlchemy() db = SQLAlchemy()
migration = Migrate() migration = Migrate()
def create_app(environment="development"): def create_app(environment="development"):
from config import config from config import config
from app.views import ( from app.views import (
@ -36,6 +38,18 @@ def create_app(environment="development"):
# Instantiate app. # Instantiate app.
app = Flask(__name__) app = Flask(__name__)
app.logger.setLevel(logging.DEBUG)
# Add healthcheck
def db_available():
try:
db.session.execute(text('SELECT 1'))
except Exception as e:
return False, str(e)
return True, 'database is ok'
health = HealthCheck(app, '/health')
health.add_check(db_available)
# Set app config. # Set app config.
env = os.environ.get("APP_ENV", environment) env = os.environ.get("APP_ENV", environment)

View File

@ -25,6 +25,11 @@ services:
- db - db
ports: ports:
- '127.0.0.1:${LOCAL_WEB_PORT:-8000}:${LOCAL_WEB_PORT:-8000}' - '127.0.0.1:${LOCAL_WEB_PORT:-8000}:${LOCAL_WEB_PORT:-8000}'
healthcheck:
test: ["CMD", "wget", "-qO-", "http://localhost:${LOCAL_WEB_PORT:-8000}/health"]
interval: 30s
timeout: 10s
retries: 3
volumes: volumes:
db_data: db_data:

2512
poetry.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -3,7 +3,7 @@
APP_ENV=production APP_ENV=production
APP_NAME=Open Law APP_NAME=Open Law
SERVER_NAME=127.0.0.1:8000 SERVER_NAME=localhost:8000
SECRET_KEY=set_here_secret SECRET_KEY=set_here_secret
# Database # Database

View File

@ -5,7 +5,7 @@ description = ""
authors = ["Simple2b https://www.simple2b.net/"] authors = ["Simple2b https://www.simple2b.net/"]
[tool.poetry.dependencies] [tool.poetry.dependencies]
python = "^3.11" python = "^3.10"
flask = "^2.2.3" flask = "^2.2.3"
flask-migrate = "^4.0.4" flask-migrate = "^4.0.4"
flask-mail = "^0.9.1" flask-mail = "^0.9.1"
@ -19,6 +19,7 @@ siwe = "^2.2.0"
flask-admin = "^1.6.1" flask-admin = "^1.6.1"
wtforms = "^3.0.1" wtforms = "^3.0.1"
flask-wtf = "^1.1.1" flask-wtf = "^1.1.1"
healthcheck = "^1.3.3"
[tool.poetry.dev-dependencies] [tool.poetry.dev-dependencies]
pytest = "^7.1.1" pytest = "^7.1.1"