Coverage for seedboxsync/cli/commands/cmd_health.py: 95%
38 statements
« prev ^ index » next coverage.py v7.16.1, created at 2026-09-26 17:14 +0000
« prev ^ index » next coverage.py v7.16.1, created at 2026-09-26 17:14 +0000
1#
2# Copyright (C) 2015-2026 Guillaume Kulakowski <guillaume@kulakowski.fr>
3#
4# For the full copyright and license information, please view the LICENSE
5# file that was distributed with this source code.
6#
7"""All commands related to health checks in SeedboxSync."""
9from datetime import datetime, timedelta
10from typing import Any
11from urllib.error import URLError
12from urllib.request import urlopen
13import click
14from seedboxsync.__version__ import __version__ as version
15from seedboxsync.cli import Context, pass_context
16from seedboxsync.core import utils
17from seedboxsync.core.database.models import TaskStatus, typed_peewee_dict
20@click.command("health")
21@pass_context
22def cli(ctx: Context) -> None:
23 """
24 Show the health status of the SeedboxSync CLI and web service.
26 Args:
27 ctx (Context): The SeedboxSync Click context.
28 """
29 exit_code = 0
31 # CLI part
32 click.echo(f"Version: {version}")
33 click.secho("CLI - OK", fg="green")
35 # Task manager part
36 heartbeat: dict[str, Any] | None = None
37 try:
38 heartbeat = typed_peewee_dict(
39 TaskStatus.select(
40 TaskStatus.key,
41 TaskStatus.running,
42 TaskStatus.started,
43 TaskStatus.finished,
44 )
45 .where(TaskStatus.key == "heartbeat")
46 .dicts()
47 .first()
48 )
49 except TaskStatus.DoesNotExist: # type: ignore[attr-defined]
50 exit_code = 1
52 if heartbeat is None:
53 click.secho("Task manager - NOK", fg="red")
54 exit_code = 2
55 elif datetime.now() - heartbeat["finished"] > timedelta(minutes=5):
56 click.secho("Task manager - NOK", fg="red")
57 exit_code = 3
58 else:
59 click.secho("Task manager - OK", fg="green")
61 # Flask WebUI part
62 health_url = utils.get_web_healthcheck_url()
63 try:
64 with urlopen(health_url, timeout=5) as response:
65 if response.status == 200:
66 click.secho("WebUI - OK", fg="green")
67 else:
68 click.secho("WebUI - NOK", fg="red")
69 exit_code = 5
70 except URLError:
71 click.secho("WebUI - NOK", fg="red")
72 exit_code = 6
74 ctx.exit(exit_code)