askrobots / dbbasic-cli
Public
Make a complete, running app (a CRM, a shop, a blog) with one command, such as dbbasic crm.
start.py · 2.8 KB · 75 lines
| 1 | """Start DBBasic services""" |
| 2 | |
| 3 | import subprocess |
| 4 | import time |
| 5 | from pathlib import Path |
| 6 | import typer |
| 7 | from rich.console import Console |
| 8 | from rich.progress import Progress, SpinnerColumn, TextColumn |
| 9 | import psutil |
| 10 | |
| 11 | from .config import SERVICES, DBBASIC_DIR, save_pid, get_pid |
| 12 | |
| 13 | console = Console() |
| 14 | |
| 15 | def start(service: str = typer.Argument(None, help="Service to start (omit for all)")): |
| 16 | """Start DBBasic services""" |
| 17 | |
| 18 | if service and service not in SERVICES: |
| 19 | console.print(f"[red]Unknown service: {service}[/red]") |
| 20 | console.print(f"Available: {', '.join(SERVICES.keys())}") |
| 21 | raise typer.Exit(1) |
| 22 | |
| 23 | services_to_start = [service] if service else list(SERVICES.keys()) |
| 24 | |
| 25 | with Progress( |
| 26 | SpinnerColumn(), |
| 27 | TextColumn("[progress.description]{task.description}"), |
| 28 | console=console, |
| 29 | ) as progress: |
| 30 | |
| 31 | for svc_name in services_to_start: |
| 32 | svc = SERVICES[svc_name] |
| 33 | task = progress.add_task(f"Starting {svc['name']}...", total=1) |
| 34 | |
| 35 | # Check if already running |
| 36 | pid = get_pid(svc_name) |
| 37 | if pid and psutil.pid_exists(pid): |
| 38 | progress.update(task, description=f"[yellow]{svc['name']} already running[/yellow]") |
| 39 | progress.advance(task) |
| 40 | continue |
| 41 | |
| 42 | # Start the service |
| 43 | try: |
| 44 | process = subprocess.Popen( |
| 45 | ["python", f"{svc['module']}.py"], |
| 46 | cwd=str(DBBASIC_DIR), |
| 47 | stdout=subprocess.DEVNULL, |
| 48 | stderr=subprocess.DEVNULL, |
| 49 | start_new_session=True |
| 50 | ) |
| 51 | |
| 52 | # Save PID |
| 53 | save_pid(svc_name, process.pid) |
| 54 | |
| 55 | # Wait a moment to check if it started |
| 56 | time.sleep(1) |
| 57 | |
| 58 | if process.poll() is None: |
| 59 | progress.update(task, description=f"[green]✅ {svc['name']} started on port {svc['port']}[/green]") |
| 60 | else: |
| 61 | progress.update(task, description=f"[red]❌ {svc['name']} failed to start[/red]") |
| 62 | |
| 63 | except Exception as e: |
| 64 | progress.update(task, description=f"[red]❌ {svc['name']}: {str(e)}[/red]") |
| 65 | |
| 66 | progress.advance(task) |
| 67 | |
| 68 | # Show summary |
| 69 | if not service: |
| 70 | console.print("\n[bold green]DBBasic services started![/bold green]") |
| 71 | console.print("\nAccess points:") |
| 72 | console.print(f" 📊 Dashboard: http://localhost:{SERVICES['monitor']['port']}") |
| 73 | console.print(f" 🔧 CRUD Engine: http://localhost:{SERVICES['crud']['port']}") |
| 74 | console.print(f" 📚 API Docs: http://localhost:{SERVICES['crud']['port']}/docs") |
| 75 | console.print("\nRun [bold]dbbasic status[/bold] to check service health") |