File size: 6,533 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
# AudioForge Production Startup Script
# Starts all services in production mode using Docker Compose

param(
    [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



Usage:

    .\scripts\prod_start.ps1 [-SkipBuild] [-Help]



Options:

    -SkipBuild    Skip building Docker images (use existing images)

    -Help         Show this help message



This script will:

1. Build Docker images (unless skipped)

2. Start all services in production mode

3. Wait for services to be ready

4. Display service URLs and status

"@
    exit 0
}

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

# Check if Docker is available
Write-Info "Checking Docker availability..."
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"
        exit 1
    }
} catch {
    Write-Error "Cannot connect to Docker daemon!"
    Write-Warning "Please start Docker Desktop and try again"
    exit 1
}

Write-Success "Docker is running"

# Step 1: Build Docker images (if not skipped)
if (-not $SkipBuild) {
    Write-Info "Building Docker images for production..."
    Write-Info "This may take several minutes on first run..."
    
    docker-compose build --no-cache
    
    if ($LASTEXITCODE -ne 0) {
        Write-Error "Failed to build Docker images!"
        exit 1
    }
    
    Write-Success "Docker images built successfully!"
} else {
    Write-Info "Skipping Docker build (using existing images)"
}

# Step 2: Start all services
Write-Info "Starting AudioForge in production mode..."
docker-compose up -d

if ($LASTEXITCODE -ne 0) {
    Write-Error "Failed to start services!"
    exit 1
}

Write-Success "Services started!"

# Step 3: Wait for services to be ready
Write-Info "Waiting for services to be ready..."
Start-Sleep -Seconds 10

# Check PostgreSQL
Write-Info "Checking PostgreSQL..."
$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 2
        $attempt++
        Write-Host "." -NoNewline
    }
}

Write-Host ""

if (-not $postgresReady) {
    Write-Warning "PostgreSQL health check timed out, but continuing..."
}

# Check Redis
Write-Info "Checking Redis..."
try {
    $result = 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..."
}

# Check Backend
Write-Info "Checking Backend API..."
$maxAttempts = 30
$attempt = 0
$backendReady = $false

while ($attempt -lt $maxAttempts -and -not $backendReady) {
    try {
        $response = Invoke-WebRequest -Uri "http://localhost:8000/health" -TimeoutSec 5 -UseBasicParsing -ErrorAction SilentlyContinue
        if ($response.StatusCode -eq 200) {
            $backendReady = $true
            Write-Success "Backend API is ready!"
        }
    } catch {
        # Continue waiting
    }
    
    if (-not $backendReady) {
        Start-Sleep -Seconds 2
        $attempt++
        Write-Host "." -NoNewline
    }
}

Write-Host ""

if (-not $backendReady) {
    Write-Warning "Backend health check timed out, but continuing..."
}

# Check Frontend
Write-Info "Checking Frontend..."
$maxAttempts = 20
$attempt = 0
$frontendReady = $false

while ($attempt -lt $maxAttempts -and -not $frontendReady) {
    try {
        $response = Invoke-WebRequest -Uri "http://localhost:3000" -TimeoutSec 5 -UseBasicParsing -ErrorAction SilentlyContinue
        if ($response.StatusCode -eq 200) {
            $frontendReady = $true
            Write-Success "Frontend is ready!"
        }
    } catch {
        # Continue waiting
    }
    
    if (-not $frontendReady) {
        Start-Sleep -Seconds 2
        $attempt++
        Write-Host "." -NoNewline
    }
}

Write-Host ""

if (-not $frontendReady) {
    Write-Warning "Frontend health check timed out, but continuing..."
}

# Step 4: Initialize database (if needed)
Write-Info "Initializing database..."
docker exec audioforge-backend python scripts/init_db.py

if ($LASTEXITCODE -eq 0) {
    Write-Success "Database initialized!"
} else {
    Write-Warning "Database initialization may have failed (might already be initialized)"
}

# 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"
Write-Success "Frontend: http://localhost:3000"
Write-Success "API Docs: http://localhost:8000/api/docs"
Write-Host ""
Write-Info "View logs: docker-compose logs -f"
Write-Info "Stop services: docker-compose down"
Write-Info "View status: docker-compose ps"
Write-Host ""
Write-Info "Ready to test music generation!"
Write-Host ""