feat: add health check for server

This commit is contained in:
arkohut 2024-10-20 01:04:32 +08:00
parent a60488e80c
commit e2e4a903eb
4 changed files with 47 additions and 6 deletions

View File

@ -41,7 +41,7 @@ file_detector = Magika()
IS_THUMBNAIL = "is_thumbnail"
BASE_URL = f"http://{settings.server_host}:{settings.server_port}"
BASE_URL = settings.server_endpoint
include_files = [".jpg", ".jpeg", ".png", ".webp"]

View File

@ -3,7 +3,7 @@ import httpx
from tabulate import tabulate
from memos.config import settings
BASE_URL = f"http://{settings.server_host}:{settings.server_port}"
BASE_URL = settings.server_endpoint
plugin_app = typer.Typer()

View File

@ -26,9 +26,6 @@ from tabulate import tabulate
app = typer.Typer(context_settings={"help_option_names": ["-h", "--help"]})
app.add_typer(plugin_app, name="plugin")
app.add_typer(lib_app, name="lib")
BASE_URL = settings.server_endpoint
# Configure logging
@ -42,6 +39,44 @@ logging.getLogger("httpx").setLevel(logging.ERROR)
logging.getLogger("typer").setLevel(logging.ERROR)
def check_server_health():
"""Check if the server is running and healthy."""
try:
response = httpx.get(f"{BASE_URL}/health", timeout=5)
return response.status_code == 200
except httpx.RequestError:
return False
def callback(ctx: typer.Context):
"""Callback to check server health before running any command."""
# List of commands that require the server to be running
server_dependent_commands = [
"scan",
"typesense-index",
"reindex",
"watch",
"ls",
"create",
"add-folder",
"show",
"sync",
"bind",
"unbind",
]
if ctx.invoked_subcommand in server_dependent_commands:
if not check_server_health():
typer.echo("Error: Server is not running. Please start the server first.")
raise typer.Exit(code=1)
app.add_typer(plugin_app, name="plugin")
app.add_typer(lib_app, name="lib", callback=callback)
@app.command()
def serve():
"""Run the server after initializing if necessary."""
@ -207,7 +242,9 @@ def record(
"""
Record screenshots of the screen.
"""
base_dir = os.path.expanduser(base_dir) if base_dir else settings.resolved_screenshots_dir
base_dir = (
os.path.expanduser(base_dir) if base_dir else settings.resolved_screenshots_dir
)
previous_hashes = load_previous_hashes(base_dir)
if once:

View File

@ -90,6 +90,10 @@ app.mount(
"/_app", StaticFiles(directory=os.path.join(current_dir, "static/_app"), html=True)
)
@app.get("/health")
async def health():
return {"status": "ok"}
@app.get("/favicon.png", response_class=FileResponse)
async def favicon_png():