mirror of
https://github.com/tcsenpai/pensieve.git
synced 2025-06-06 19:25:24 +00:00
feat: add health check for server
This commit is contained in:
parent
a60488e80c
commit
e2e4a903eb
@ -41,7 +41,7 @@ file_detector = Magika()
|
|||||||
|
|
||||||
IS_THUMBNAIL = "is_thumbnail"
|
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"]
|
include_files = [".jpg", ".jpeg", ".png", ".webp"]
|
||||||
|
|
||||||
|
@ -3,7 +3,7 @@ import httpx
|
|||||||
from tabulate import tabulate
|
from tabulate import tabulate
|
||||||
from memos.config import settings
|
from memos.config import settings
|
||||||
|
|
||||||
BASE_URL = f"http://{settings.server_host}:{settings.server_port}"
|
BASE_URL = settings.server_endpoint
|
||||||
|
|
||||||
|
|
||||||
plugin_app = typer.Typer()
|
plugin_app = typer.Typer()
|
||||||
|
@ -26,9 +26,6 @@ from tabulate import tabulate
|
|||||||
|
|
||||||
app = typer.Typer(context_settings={"help_option_names": ["-h", "--help"]})
|
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
|
BASE_URL = settings.server_endpoint
|
||||||
|
|
||||||
# Configure logging
|
# Configure logging
|
||||||
@ -42,6 +39,44 @@ logging.getLogger("httpx").setLevel(logging.ERROR)
|
|||||||
logging.getLogger("typer").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()
|
@app.command()
|
||||||
def serve():
|
def serve():
|
||||||
"""Run the server after initializing if necessary."""
|
"""Run the server after initializing if necessary."""
|
||||||
@ -207,7 +242,9 @@ def record(
|
|||||||
"""
|
"""
|
||||||
Record screenshots of the screen.
|
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)
|
previous_hashes = load_previous_hashes(base_dir)
|
||||||
|
|
||||||
if once:
|
if once:
|
||||||
|
@ -90,6 +90,10 @@ app.mount(
|
|||||||
"/_app", StaticFiles(directory=os.path.join(current_dir, "static/_app"), html=True)
|
"/_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)
|
@app.get("/favicon.png", response_class=FileResponse)
|
||||||
async def favicon_png():
|
async def favicon_png():
|
||||||
|
Loading…
x
Reference in New Issue
Block a user