Files
django-vue3-admin/scripts/start-backend.ps1
T
lewis_yan f6313a93ce feat: add cross-platform service management scripts
Add comprehensive scripts for managing PIS project services:
- start-backend.ps1/.sh: Start Django backend with uvicorn/gunicorn
- start-frontend.ps1/.sh: Start Vue frontend dev server
- start-celery.ps1/.sh: Start Celery worker and beat
- stop-all.ps1/.sh: Stop all services gracefully
- stop-celery.ps1/.sh: Stop Celery services only
- view-logs.ps1/.sh: View and tail log files
- check-status.ps1/.sh: Check service status and ports

Cross-platform support:
- Windows PowerShell scripts with English output (UTF-8 safe)
- Unix/Linux/macOS Bash scripts with Chinese output
- WMI queries for PowerShell 5.1 compatibility
- Unified Makefile and make.ps1 orchestrators

Also add scripts-documentation.md with comprehensive usage guide.
2026-04-01 19:11:45 +08:00

79 lines
2.8 KiB
PowerShell

# ============================================================
# Start Backend Service (Windows PowerShell)
# Usage: .\start-backend.ps1 [dev|prod]
# ============================================================
$ErrorActionPreference = "Stop"
$ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
$ProjectRoot = Split-Path -Parent $ScriptDir
$BackendDir = Join-Path $ProjectRoot "backend"
$PID_DIR = Join-Path $ProjectRoot ".pids"
$LOG_DIR = Join-Path $BackendDir "logs"
$ENV = if ($args[0]) { $args[0] } else { "dev" }
# Create directories
if (!(Test-Path $PID_DIR)) { New-Item -ItemType Directory -Path $PID_DIR -Force | Out-Null }
if (!(Test-Path $LOG_DIR)) { New-Item -ItemType Directory -Path $LOG_DIR -Force | Out-Null }
$PID_FILE = Join-Path $PID_DIR "backend.pid"
$LOG_FILE = Join-Path $LOG_DIR "backend.log"
# Check if already running
if (Test-Path $PID_FILE) {
$PID = Get-Content $PID_FILE -ErrorAction SilentlyContinue
if ($PID -and (Get-Process -Id $PID -ErrorAction SilentlyContinue)) {
Write-Host "[Backend] Already running (PID: $PID), skipping"
exit 0
} else {
Write-Host "[Backend] Cleaning stale PID file"
Remove-Item $PID_FILE -Force -ErrorAction SilentlyContinue
}
}
Set-Location $BackendDir
# Check dependencies
try {
python -c "import uvicorn" 2>$null
if ($LASTEXITCODE -ne 0) { throw "uvicorn not found" }
} catch {
Write-Host "[Error] uvicorn not installed. Run: cd backend && pip install uvicorn"
exit 1
}
# Set environment
$env:DJANGO_SETTINGS_MODULE = "application.settings"
Write-Host "[Backend] Starting service (ENV=$ENV)..."
# Select startup method based on environment
if ($ENV -eq "prod") {
Write-Host "[Backend] Using gunicorn (production mode)"
Start-Process -FilePath "python" -ArgumentList "-m gunicorn application.asgi:application -c gunicorn_conf.py --daemon -p $PID_FILE" -WorkingDirectory $BackendDir -RedirectStandardOutput $LOG_FILE -RedirectStandardError $LOG_FILE -WindowStyle Hidden
} else {
Write-Host "[Backend] Using uvicorn (development mode with hot reload)"
$proc = Start-Process -FilePath "python" -ArgumentList "main.py" -WorkingDirectory $BackendDir -PassThru -NoNewWindow
$proc.Id | Set-Content $PID_FILE
}
# Wait for startup
Start-Sleep -Seconds 3
# Check if started successfully
if (Test-Path $PID_FILE) {
$PID = Get-Content $PID_FILE
if (Get-Process -Id $PID -ErrorAction SilentlyContinue) {
Write-Host "[Backend] Started successfully (PID: $PID)"
Write-Host "[Backend] Log file: $LOG_FILE"
} else {
Write-Host "[Backend] Failed to start, check log: $LOG_FILE"
Remove-Item $PID_FILE -Force -ErrorAction SilentlyContinue
}
} else {
Write-Host "[Backend] Failed to start, check log: $LOG_FILE"
}
exit 0