Untitled
unknown
plain_text
3 years ago
2.9 kB
25
Indexable
from http import HTTPStatus
from dotenv import load_dotenv
from fastapi import APIRouter, HTTPException
import duckdb
import boto3
import psutil
import shutil
import tempfile
import botocore
import os
from src.app_functions.core.config import Settings
load_dotenv()
router = APIRouter(
prefix='/health',
tags=['Health']
)
@router.get("/")
def health():
# fetch the uptime of the server
uptime = psutil.boot_time()
# Get the operating system temporary folder
temp_folder = tempfile.gettempdir()
# Get the free space in the temporary folder in bytes
temp_folder_free_space = shutil.disk_usage(temp_folder).free
# If more than 400MB free
if( temp_folder_free_space >1024 * 1024 * 400 ):
{"status":"OK","diskSpaceFree": temp_folder_free_space}
else:
raise HTTPException(status_code=503, detail="Not enough free space in temporary folder.")
try:
# Connect to aws s3
session = boto3.Session(profile_name='gtndn-cloudop')
credentials = session.get_credentials()
client = boto3.client(
's3',
aws_access_key_id=credentials.access_key,
aws_secret_access_key=credentials.secret_key,
aws_session_token=credentials.token
)
# The head_bucket returns 200 Ok if the bucket exists and 403 forbidden if we have a wrong bucket_name
# The head_bucket is needed to test the client, beacuse we won't get an exeption if we have wrong credentials
client.head_bucket(Bucket=os.getenv('BUCKET_NAME'))
# Connect to duckDB
# settings=Settings()
# duckdb.connect(settings.DUCKDB_PATH)
settings=Settings()
database=settings.DUCKDB_PATH.as_posix()
duckdb.connect(database)
return {"status": "UP", "uptime": uptime, "checks": {"duckDB": "UP", "S3": "UP", "diskSpaceFree": temp_folder_free_space}, "HTTPresponse": HTTPStatus.OK}
except botocore.exceptions.ProfileNotFound:
return {"status": "ERROR", "uptime": uptime, "checks": {"duckDB": "UP", "S3": "DOWN", "diskSpaceFree": temp_folder_free_space}, "HTTP response": HTTPStatus.SERVICE_UNAVAILABLE}
except botocore.exceptions.ClientError:
return {"status": "UP", "uptime": uptime, "checks": {"duckDB": "UP", "S3": "DOWN", "diskSpaceFree": temp_folder_free_space}, "HTTP response": HTTPStatus.SERVICE_UNAVAILABLE}
except botocore.exceptions.UnknownServiceError :
return {"status": "ERROR", "uptime": uptime, "checks": {"duckDB": "UP", "S3": "DOWN", "diskSpaceFree": temp_folder_free_space}, "HTTP response": HTTPStatus.SERVICE_UNAVAILABLE}
except Exception :
return {"status": "ERROR", "uptime": uptime, "checks": {"duckDB": "DOWN", "S3": "UP", "diskSpaceFree": temp_folder_free_space}, "HTTP response": HTTPStatus.SERVICE_UNAVAILABLE}
Editor is loading...