askrobots / dbbasic-cli
Public
Make a complete, running app (a CRM, a shop, a blog) with one command, such as dbbasic crm.
status.py · 3.0 KB · 99 lines
| 1 | """Check status of DBBasic services""" |
| 2 | |
| 3 | import socket |
| 4 | import typer |
| 5 | from rich.console import Console |
| 6 | from rich.table import Table |
| 7 | import psutil |
| 8 | import httpx |
| 9 | |
| 10 | from .config import SERVICES, get_pid |
| 11 | |
| 12 | console = Console() |
| 13 | |
| 14 | def check_port(port): |
| 15 | """Check if port is open""" |
| 16 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 17 | result = sock.connect_ex(('localhost', port)) |
| 18 | sock.close() |
| 19 | return result == 0 |
| 20 | |
| 21 | def get_process_info(pid): |
| 22 | """Get process information""" |
| 23 | try: |
| 24 | process = psutil.Process(pid) |
| 25 | return { |
| 26 | "cpu": f"{process.cpu_percent()}%", |
| 27 | "memory": f"{process.memory_info().rss / 1024 / 1024:.1f}MB", |
| 28 | "status": "Running" |
| 29 | } |
| 30 | except: |
| 31 | return None |
| 32 | |
| 33 | def status(verbose: bool = typer.Option(False, "--verbose", "-v", help="Show detailed information")): |
| 34 | """Check status of DBBasic services""" |
| 35 | |
| 36 | table = Table(title="DBBasic Service Status") |
| 37 | table.add_column("Service", style="cyan") |
| 38 | table.add_column("Port", style="magenta") |
| 39 | table.add_column("Status", style="bold") |
| 40 | |
| 41 | if verbose: |
| 42 | table.add_column("PID") |
| 43 | table.add_column("CPU") |
| 44 | table.add_column("Memory") |
| 45 | |
| 46 | all_running = True |
| 47 | |
| 48 | for svc_name, svc in SERVICES.items(): |
| 49 | pid = get_pid(svc_name) |
| 50 | port_open = check_port(svc['port']) |
| 51 | |
| 52 | if pid and psutil.pid_exists(pid) and port_open: |
| 53 | status_icon = "✅ Running" |
| 54 | status_color = "green" |
| 55 | |
| 56 | if verbose: |
| 57 | info = get_process_info(pid) |
| 58 | table.add_row( |
| 59 | svc['name'], |
| 60 | str(svc['port']), |
| 61 | f"[{status_color}]{status_icon}[/{status_color}]", |
| 62 | str(pid), |
| 63 | info['cpu'] if info else "N/A", |
| 64 | info['memory'] if info else "N/A" |
| 65 | ) |
| 66 | else: |
| 67 | table.add_row( |
| 68 | svc['name'], |
| 69 | str(svc['port']), |
| 70 | f"[{status_color}]{status_icon}[/{status_color}]" |
| 71 | ) |
| 72 | else: |
| 73 | all_running = False |
| 74 | status_icon = "❌ Stopped" |
| 75 | status_color = "red" |
| 76 | |
| 77 | if verbose: |
| 78 | table.add_row( |
| 79 | svc['name'], |
| 80 | str(svc['port']), |
| 81 | f"[{status_color}]{status_icon}[/{status_color}]", |
| 82 | "-", |
| 83 | "-", |
| 84 | "-" |
| 85 | ) |
| 86 | else: |
| 87 | table.add_row( |
| 88 | svc['name'], |
| 89 | str(svc['port']), |
| 90 | f"[{status_color}]{status_icon}[/{status_color}]" |
| 91 | ) |
| 92 | |
| 93 | console.print(table) |
| 94 | |
| 95 | if all_running: |
| 96 | console.print("\n[green]✅ All services operational![/green]") |
| 97 | else: |
| 98 | console.print("\n[yellow]⚠️ Some services are not running[/yellow]") |
| 99 | console.print("Run [bold]dbbasic start[/bold] to start all services") |