Skip to content

Commit

Permalink
add: health check
Browse files Browse the repository at this point in the history
  • Loading branch information
MagicTheDev committed Dec 29, 2024
1 parent 9b5e7db commit 562c903
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,7 @@ RUN pip install --no-cache-dir -r requirements.txt
# Now copy the rest of the application code into the container
COPY . .

HEALTHCHECK --interval=30s --timeout=5s --start-period=5s CMD curl -f http://127.0.0.1:8000/health || exit 1

# Command to run the application
CMD ["python3", "main.py"]
24 changes: 24 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
import traceback

import asyncio
import uvicorn
import disnake
import platform
import sentry_sdk
from apscheduler.schedulers.asyncio import AsyncIOScheduler
from pymongo import MongoClient
from pytz import utc

from fastapi import FastAPI
import threading
from classes.bot import CustomClient
from utility.startup import create_config, get_cluster_breakdown, load_cogs, sentry_filter

if platform.system() != "Windows":
import uvloop
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())

scheduler = AsyncIOScheduler(timezone=utc)
scheduler.start()

Expand Down Expand Up @@ -56,8 +65,23 @@
'background.logs.war',
"background.features.refresh_boards"
]
health_app = FastAPI()
@health_app.get("/health")
async def health_check():
if bot.is_ready():
return {"status": "ok", "details": "Bot is running and ready"}
else:
return {"status": "error", "details": "Bot is not ready"}

def run_health_check_server():
uvicorn.run(health_app, host="127.0.0.1", port=8000)

if __name__ == '__main__':

# Start FastAPI server in a background thread
threading.Thread(target=run_health_check_server, daemon=True).start()


sentry_sdk.init(
dsn=config.sentry_dsn,
traces_sample_rate=1.0,
Expand Down
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ pytz==2021.3
redis==4.6.0
sentry_sdk==1.25.0
stringcase==1.2.0
uvicorn==0.34.0
uvloop==0.21.0
Wand==0.6.7
websockets==10.3
XlsxWriter==3.0.3

0 comments on commit 562c903

Please sign in to comment.