CoolFace
Apppublic

OnyxMunk/AudioForge

sourceHugging Facemitupdated 8mo agoView on Hugging Face
0likes
prod_start_manual.ps1275 linesDownload Raw Back to scripts
1# AudioForge Production Startup Script (Manual - No Docker)
2# Starts PostgreSQL/Redis, initializes DB, then starts backend and frontend in production mode
3
4param(
5    [switch]$SkipServices = $false,
6    [switch]$SkipBuild = $false,
7    [switch]$Help = $false
8)
9
10$Colors = @{
11    Red = "Red"
12    Green = "Green"
13    Yellow = "Yellow"
14    Blue = "Cyan"
15    Cyan = "Cyan"
16}
17
18function Write-Info { Write-Host "[INFO] $args" -ForegroundColor $Colors.Blue }
19function Write-Success { Write-Host "[SUCCESS] $args" -ForegroundColor $Colors.Green }
20function Write-Warning { Write-Host "[WARNING] $args" -ForegroundColor $Colors.Yellow }
21function Write-Error { Write-Host "[ERROR] $args" -ForegroundColor $Colors.Red }
22
23if ($Help) {
24    Write-Host @"
25AudioForge Production Startup Script (Manual)
26
27Usage:
28    .\scripts\prod_start_manual.ps1 [-SkipServices] [-SkipBuild] [-Help]
29
30Options:
31    -SkipServices    Skip starting PostgreSQL/Redis (assume they're already running)
32    -SkipBuild       Skip building frontend (use existing build)
33    -Help           Show this help message
34
35This script will:
361. Start PostgreSQL and Redis (if not skipped)
372. Wait for services to be ready
383. Initialize the database
394. Build frontend for production (if not skipped)
405. Start backend server in production mode (4 workers)
416. Start frontend server in production mode
42"@
43    exit 0
44}
45
46$ProjectRoot = Split-Path -Parent $PSScriptRoot
47Set-Location $ProjectRoot
48
49# Step 1: Start PostgreSQL and Redis
50if (-not $SkipServices) {
51    Write-Info "Starting PostgreSQL and Redis..."
52    
53    # Check if Docker is available and running
54    try {
55        $null = docker ps 2>&1 | Out-Null
56        if ($LASTEXITCODE -ne 0) {
57            Write-Error "Docker Desktop is not running!"
58            Write-Warning "Please start Docker Desktop and try again"
59            Write-Info "You can skip this step with -SkipServices if services are already running"
60            exit 1
61        }
62    } catch {
63        Write-Error "Cannot connect to Docker daemon!"
64        Write-Warning "Please start Docker Desktop and try again"
65        Write-Info "You can skip this step with -SkipServices if services are already running"
66        exit 1
67    }
68    
69    # Start only postgres and redis services
70    Write-Info "Starting PostgreSQL and Redis containers..."
71    docker-compose up -d postgres redis
72    
73    if ($LASTEXITCODE -ne 0) {
74        Write-Error "Failed to start services!"
75        Write-Info "Make sure Docker Desktop is running"
76        exit 1
77    }
78    
79    Write-Info "Waiting for PostgreSQL to be ready..."
80    $maxAttempts = 30
81    $attempt = 0
82    $postgresReady = $false
83    
84    while ($attempt -lt $maxAttempts -and -not $postgresReady) {
85        try {
86            $result = docker exec audioforge-postgres pg_isready -U postgres 2>&1
87            if ($LASTEXITCODE -eq 0) {
88                $postgresReady = $true
89                Write-Success "PostgreSQL is ready!"
90            }
91        } catch {
92            # Continue waiting
93        }
94        
95        if (-not $postgresReady) {
96            Start-Sleep -Seconds 1
97            $attempt++
98            Write-Host "." -NoNewline
99        }
100    }
101    
102    Write-Host ""
103    
104    if (-not $postgresReady) {
105        Write-Error "PostgreSQL failed to start within timeout!"
106        exit 1
107    }
108    
109    Write-Info "Waiting for Redis to be ready..."
110    Start-Sleep -Seconds 2
111    
112    try {
113        $null = docker exec audioforge-redis redis-cli ping 2>&1
114        if ($LASTEXITCODE -eq 0) {
115            Write-Success "Redis is ready!"
116        }
117    } catch {
118        Write-Warning "Redis health check failed, but continuing..."
119    }
120} else {
121    Write-Info "Skipping service startup (assuming PostgreSQL/Redis are running)"
122}
123
124# Step 2: Setup Backend Environment
125Write-Info "Setting up backend environment..."
126Set-Location "$ProjectRoot\backend"
127
128# Check for Python 3.11 venv first (for ML support), then fall back to regular venv
129$venvPath = if (Test-Path ".venv311") { ".venv311" } elseif (Test-Path ".venv") { ".venv" } else { $null }
130
131if ($null -eq $venvPath) {
132    Write-Warning "Virtual environment not found! Creating .venv..."
133    python -m venv .venv
134    $venvPath = ".venv"
135    & "$venvPath\Scripts\Activate.ps1"
136    pip install -e ".[dev]"
137} else {
138    Write-Info "Using virtual environment: $venvPath"
139    # Activate venv for subsequent commands
140    & "$venvPath\Scripts\Activate.ps1"
141}
142
143# Step 3: Initialize Database
144Write-Info "Initializing database..."
145
146# Check if .env exists
147if (-not (Test-Path ".env")) {
148    Write-Warning ".env file not found! Running setup_env.py..."
149    Set-Location $ProjectRoot
150    python scripts/setup_env.py
151    Set-Location "$ProjectRoot\backend"
152}
153
154# Use venv's Python explicitly for database initialization
155& "$venvPath\Scripts\python.exe" scripts/init_db.py
156
157if ($LASTEXITCODE -ne 0) {
158    Write-Error "Database initialization failed!"
159    Write-Info "Make sure PostgreSQL is running and DATABASE_URL is correct in backend/.env"
160    exit 1
161}
162
163Write-Success "Database initialized successfully!"
164
165# Step 4: Build Frontend for Production
166if (-not $SkipBuild) {
167    Write-Info "Building frontend for production..."
168    Set-Location "$ProjectRoot\frontend"
169    
170    # Check if node_modules exists
171    if (-not (Test-Path "node_modules")) {
172        Write-Warning "Frontend dependencies not installed! Installing..."
173        pnpm install
174    }
175    
176    # Ensure .env.local exists
177    if (-not (Test-Path ".env.local")) {
178        Write-Info "Creating frontend .env.local..."
179        "NEXT_PUBLIC_API_URL=http://localhost:8000" | Out-File -FilePath ".env.local" -Encoding UTF8
180    }
181    
182    # Set production environment
183    $env:NODE_ENV = "production"
184    
185    Write-Info "Running production build (this may take a few minutes)..."
186    pnpm build
187    
188    if ($LASTEXITCODE -ne 0) {
189        Write-Error "Frontend build failed!"
190        exit 1
191    }
192    
193    Write-Success "Frontend built successfully!"
194} else {
195    Write-Info "Skipping frontend build (using existing build)"
196}
197
198# Step 5: Start Backend in Production Mode
199Write-Info "Starting backend server in production mode..."
200Set-Location "$ProjectRoot\backend"
201
202# Check for Python 3.11 venv first (for ML support), then fall back to regular venv
203$venvPath = if (Test-Path ".venv311") { ".venv311" } elseif (Test-Path ".venv") { ".venv" } else { $null }
204
205if ($null -eq $venvPath) {
206    Write-Warning "Virtual environment not found! Creating .venv..."
207    python -m venv .venv
208    $venvPath = ".venv"
209    & "$venvPath\Scripts\Activate.ps1"
210    pip install -e ".[dev]"
211} else {
212    Write-Info "Using virtual environment: $venvPath"
213}
214
215# Set production environment variables
216$env:ENVIRONMENT = "production"
217$env:LOG_LEVEL = "info"
218
219# Start backend in production mode with 4 workers
220$backendJob = Start-Process powershell -ArgumentList @(
221    "-NoExit",
222    "-Command",
223    "cd '$ProjectRoot\backend'; `$env:ENVIRONMENT='production'; `$env:LOG_LEVEL='info'; `$venv = if (Test-Path .venv311) { '.venv311' } elseif (Test-Path .venv) { '.venv' } else { `$null }; if (`$venv) { & `"`$venv\Scripts\Activate.ps1`" }; uvicorn app.main:app --host 0.0.0.0 --port 8000 --workers 4 --log-level info"
224) -PassThru
225
226Write-Success "Backend starting in production mode (PID: $($backendJob.Id))"
227Write-Info "Backend will be available at http://localhost:8000"
228
229# Step 6: Start Frontend in Production Mode
230Write-Info "Starting frontend server in production mode..."
231Set-Location "$ProjectRoot\frontend"
232
233# Ensure .env.local exists
234if (-not (Test-Path ".env.local")) {
235    Write-Info "Creating frontend .env.local..."
236    "NEXT_PUBLIC_API_URL=http://localhost:8000" | Out-File -FilePath ".env.local" -Encoding UTF8
237}
238
239# Set production environment
240$env:NODE_ENV = "production"
241
242# Start frontend in production mode
243$frontendJob = Start-Process powershell -ArgumentList @(
244    "-NoExit",
245    "-Command",
246    "cd '$ProjectRoot\frontend'; `$env:NODE_ENV='production'; pnpm start"
247) -PassThru
248
249Write-Success "Frontend starting in production mode (PID: $($frontendJob.Id))"
250Write-Info "Frontend will be available at http://localhost:3000"
251
252# Wait a bit for services to start
253Write-Info "Waiting for services to initialize..."
254Start-Sleep -Seconds 10
255
256# Final status
257Write-Host ""
258Write-Host "================================================================" -ForegroundColor Green
259Write-Host "                                                               " -ForegroundColor Green
260Write-Host "      AudioForge Production Mode Started!                      " -ForegroundColor Green
261Write-Host "                                                               " -ForegroundColor Green
262Write-Host "================================================================" -ForegroundColor Green
263Write-Host ""
264Write-Success "Backend:  http://localhost:8000 (4 workers)"
265Write-Success "Frontend: http://localhost:3000 (production build)"
266Write-Success "API Docs: http://localhost:8000/api/docs"
267Write-Host ""
268Write-Info "Backend and Frontend are running in separate PowerShell windows"
269Write-Info "Close those windows to stop the servers"
270Write-Host ""
271Write-Info "To stop PostgreSQL/Redis: docker-compose down"
272Write-Host ""
273Write-Info "Ready to test music generation!"
274Write-Host ""
275