File size: 9,521 Bytes
09fa60b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# AudioForge Production Startup Script (Manual - No Docker)
# Starts PostgreSQL/Redis, initializes DB, then starts backend and frontend in production mode

param(
    [switch]$SkipServices = $false,
    [switch]$SkipBuild = $false,
    [switch]$Help = $false
)

$Colors = @{
    Red = "Red"
    Green = "Green"
    Yellow = "Yellow"
    Blue = "Cyan"
    Cyan = "Cyan"
}

function Write-Info { Write-Host "[INFO] $args" -ForegroundColor $Colors.Blue }
function Write-Success { Write-Host "[SUCCESS] $args" -ForegroundColor $Colors.Green }
function Write-Warning { Write-Host "[WARNING] $args" -ForegroundColor $Colors.Yellow }
function Write-Error { Write-Host "[ERROR] $args" -ForegroundColor $Colors.Red }

if ($Help) {
    Write-Host @"

AudioForge Production Startup Script (Manual)



Usage:

    .\scripts\prod_start_manual.ps1 [-SkipServices] [-SkipBuild] [-Help]



Options:

    -SkipServices    Skip starting PostgreSQL/Redis (assume they're already running)

    -SkipBuild       Skip building frontend (use existing build)

    -Help           Show this help message



This script will:

1. Start PostgreSQL and Redis (if not skipped)

2. Wait for services to be ready

3. Initialize the database

4. Build frontend for production (if not skipped)

5. Start backend server in production mode (4 workers)

6. Start frontend server in production mode

"@
    exit 0
}

$ProjectRoot = Split-Path -Parent $PSScriptRoot
Set-Location $ProjectRoot

# Step 1: Start PostgreSQL and Redis
if (-not $SkipServices) {
    Write-Info "Starting PostgreSQL and Redis..."
    
    # Check if Docker is available and running
    try {
        $null = docker ps 2>&1 | Out-Null
        if ($LASTEXITCODE -ne 0) {
            Write-Error "Docker Desktop is not running!"
            Write-Warning "Please start Docker Desktop and try again"
            Write-Info "You can skip this step with -SkipServices if services are already running"
            exit 1
        }
    } catch {
        Write-Error "Cannot connect to Docker daemon!"
        Write-Warning "Please start Docker Desktop and try again"
        Write-Info "You can skip this step with -SkipServices if services are already running"
        exit 1
    }
    
    # Start only postgres and redis services
    Write-Info "Starting PostgreSQL and Redis containers..."
    docker-compose up -d postgres redis
    
    if ($LASTEXITCODE -ne 0) {
        Write-Error "Failed to start services!"
        Write-Info "Make sure Docker Desktop is running"
        exit 1
    }
    
    Write-Info "Waiting for PostgreSQL to be ready..."
    $maxAttempts = 30
    $attempt = 0
    $postgresReady = $false
    
    while ($attempt -lt $maxAttempts -and -not $postgresReady) {
        try {
            $result = docker exec audioforge-postgres pg_isready -U postgres 2>&1
            if ($LASTEXITCODE -eq 0) {
                $postgresReady = $true
                Write-Success "PostgreSQL is ready!"
            }
        } catch {
            # Continue waiting
        }
        
        if (-not $postgresReady) {
            Start-Sleep -Seconds 1
            $attempt++
            Write-Host "." -NoNewline
        }
    }
    
    Write-Host ""
    
    if (-not $postgresReady) {
        Write-Error "PostgreSQL failed to start within timeout!"
        exit 1
    }
    
    Write-Info "Waiting for Redis to be ready..."
    Start-Sleep -Seconds 2
    
    try {
        $null = docker exec audioforge-redis redis-cli ping 2>&1
        if ($LASTEXITCODE -eq 0) {
            Write-Success "Redis is ready!"
        }
    } catch {
        Write-Warning "Redis health check failed, but continuing..."
    }
} else {
    Write-Info "Skipping service startup (assuming PostgreSQL/Redis are running)"
}

# Step 2: Setup Backend Environment
Write-Info "Setting up backend environment..."
Set-Location "$ProjectRoot\backend"

# Check for Python 3.11 venv first (for ML support), then fall back to regular venv
$venvPath = if (Test-Path ".venv311") { ".venv311" } elseif (Test-Path ".venv") { ".venv" } else { $null }

if ($null -eq $venvPath) {
    Write-Warning "Virtual environment not found! Creating .venv..."
    python -m venv .venv
    $venvPath = ".venv"
    & "$venvPath\Scripts\Activate.ps1"
    pip install -e ".[dev]"
} else {
    Write-Info "Using virtual environment: $venvPath"
    # Activate venv for subsequent commands
    & "$venvPath\Scripts\Activate.ps1"
}

# Step 3: Initialize Database
Write-Info "Initializing database..."

# Check if .env exists
if (-not (Test-Path ".env")) {
    Write-Warning ".env file not found! Running setup_env.py..."
    Set-Location $ProjectRoot
    python scripts/setup_env.py
    Set-Location "$ProjectRoot\backend"
}

# Use venv's Python explicitly for database initialization
& "$venvPath\Scripts\python.exe" scripts/init_db.py

if ($LASTEXITCODE -ne 0) {
    Write-Error "Database initialization failed!"
    Write-Info "Make sure PostgreSQL is running and DATABASE_URL is correct in backend/.env"
    exit 1
}

Write-Success "Database initialized successfully!"

# Step 4: Build Frontend for Production
if (-not $SkipBuild) {
    Write-Info "Building frontend for production..."
    Set-Location "$ProjectRoot\frontend"
    
    # Check if node_modules exists
    if (-not (Test-Path "node_modules")) {
        Write-Warning "Frontend dependencies not installed! Installing..."
        pnpm install
    }
    
    # Ensure .env.local exists
    if (-not (Test-Path ".env.local")) {
        Write-Info "Creating frontend .env.local..."
        "NEXT_PUBLIC_API_URL=http://localhost:8000" | Out-File -FilePath ".env.local" -Encoding UTF8
    }
    
    # Set production environment
    $env:NODE_ENV = "production"
    
    Write-Info "Running production build (this may take a few minutes)..."
    pnpm build
    
    if ($LASTEXITCODE -ne 0) {
        Write-Error "Frontend build failed!"
        exit 1
    }
    
    Write-Success "Frontend built successfully!"
} else {
    Write-Info "Skipping frontend build (using existing build)"
}

# Step 5: Start Backend in Production Mode
Write-Info "Starting backend server in production mode..."
Set-Location "$ProjectRoot\backend"

# Check for Python 3.11 venv first (for ML support), then fall back to regular venv
$venvPath = if (Test-Path ".venv311") { ".venv311" } elseif (Test-Path ".venv") { ".venv" } else { $null }

if ($null -eq $venvPath) {
    Write-Warning "Virtual environment not found! Creating .venv..."
    python -m venv .venv
    $venvPath = ".venv"
    & "$venvPath\Scripts\Activate.ps1"
    pip install -e ".[dev]"
} else {
    Write-Info "Using virtual environment: $venvPath"
}

# Set production environment variables
$env:ENVIRONMENT = "production"
$env:LOG_LEVEL = "info"

# Start backend in production mode with 4 workers
$backendJob = Start-Process powershell -ArgumentList @(
    "-NoExit",
    "-Command",
    "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"
) -PassThru

Write-Success "Backend starting in production mode (PID: $($backendJob.Id))"
Write-Info "Backend will be available at http://localhost:8000"

# Step 6: Start Frontend in Production Mode
Write-Info "Starting frontend server in production mode..."
Set-Location "$ProjectRoot\frontend"

# Ensure .env.local exists
if (-not (Test-Path ".env.local")) {
    Write-Info "Creating frontend .env.local..."
    "NEXT_PUBLIC_API_URL=http://localhost:8000" | Out-File -FilePath ".env.local" -Encoding UTF8
}

# Set production environment
$env:NODE_ENV = "production"

# Start frontend in production mode
$frontendJob = Start-Process powershell -ArgumentList @(
    "-NoExit",
    "-Command",
    "cd '$ProjectRoot\frontend'; `$env:NODE_ENV='production'; pnpm start"
) -PassThru

Write-Success "Frontend starting in production mode (PID: $($frontendJob.Id))"
Write-Info "Frontend will be available at http://localhost:3000"

# Wait a bit for services to start
Write-Info "Waiting for services to initialize..."
Start-Sleep -Seconds 10

# Final status
Write-Host ""
Write-Host "================================================================" -ForegroundColor Green
Write-Host "                                                               " -ForegroundColor Green
Write-Host "      AudioForge Production Mode Started!                      " -ForegroundColor Green
Write-Host "                                                               " -ForegroundColor Green
Write-Host "================================================================" -ForegroundColor Green
Write-Host ""
Write-Success "Backend:  http://localhost:8000 (4 workers)"
Write-Success "Frontend: http://localhost:3000 (production build)"
Write-Success "API Docs: http://localhost:8000/api/docs"
Write-Host ""
Write-Info "Backend and Frontend are running in separate PowerShell windows"
Write-Info "Close those windows to stop the servers"
Write-Host ""
Write-Info "To stop PostgreSQL/Redis: docker-compose down"
Write-Host ""
Write-Info "Ready to test music generation!"
Write-Host ""