Skip to content

Commit f049b82

Browse files
jrcoakona-agent
andcommitted
Improve startup script reliability and database initialization
- Replace problematic lsof commands with netstat + timeout to prevent hanging - Add comprehensive database readiness checks (container health, connection, schema) - Enhance database seeding with better error handling and fallback options - Increase timeout values for PostgreSQL initialization (90s for health check) - Add schema verification before seeding to ensure migrations completed - Improve logging and error reporting throughout startup process Co-authored-by: Ona <no-reply@ona.com>
1 parent 3ddcad1 commit f049b82

File tree

1 file changed

+46
-8
lines changed

1 file changed

+46
-8
lines changed

startup.sh

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ wait_for_service() {
5656
# Function to check if port is in use
5757
check_port() {
5858
local port=$1
59-
if lsof -Pi :$port -sTCP:LISTEN -t >/dev/null 2>&1; then
59+
if timeout 5 netstat -tlnp 2>/dev/null | grep -q ":$port "; then
6060
return 0
6161
else
6262
return 1
@@ -66,8 +66,8 @@ check_port() {
6666
# Function to kill process on port
6767
kill_port() {
6868
local port=$1
69-
local pid=$(lsof -ti:$port 2>/dev/null | head -1)
70-
if [ ! -z "$pid" ]; then
69+
local pid=$(timeout 5 netstat -tlnp 2>/dev/null | grep ":$port " | awk '{print $7}' | cut -d'/' -f1 | head -1)
70+
if [ ! -z "$pid" ] && [ "$pid" != "-" ]; then
7171
print_warning "Killing process on port $port (PID: $pid)"
7272
kill -9 $pid 2>/dev/null || true
7373
sleep 2
@@ -99,13 +99,33 @@ if ! docker-compose up -d; then
9999
exit 1
100100
fi
101101

102-
# Wait for PostgreSQL to be ready
103-
if ! wait_for_service "PostgreSQL" "PGPASSWORD=gitpod psql -h localhost -U gitpod -d gitpodflix -c 'SELECT 1'" 60 3; then
104-
print_error "PostgreSQL failed to start"
102+
# Wait for PostgreSQL container to be healthy
103+
print_status "Waiting for PostgreSQL container to be healthy..."
104+
if ! wait_for_service "PostgreSQL Container" "docker-compose ps postgres | grep -q 'healthy'" 90 5; then
105+
print_error "PostgreSQL container failed to become healthy"
105106
docker-compose logs
106107
exit 1
107108
fi
108109

110+
# Wait for database connection and schema to be ready
111+
if ! wait_for_service "PostgreSQL Database" "PGPASSWORD=gitpod psql -h localhost -U gitpod -d gitpodflix -c 'SELECT 1'" 30 2; then
112+
print_error "PostgreSQL database connection failed"
113+
docker-compose logs
114+
exit 1
115+
fi
116+
117+
# Verify database schema is ready (migrations have run)
118+
print_status "Verifying database schema..."
119+
if ! wait_for_service "Database Schema" "PGPASSWORD=gitpod psql -h localhost -U gitpod -d gitpodflix -c 'SELECT 1 FROM information_schema.tables WHERE table_name = '\''movies'\'''" 30 2; then
120+
print_error "Database schema not ready - migrations may not have completed"
121+
docker-compose logs
122+
exit 1
123+
fi
124+
125+
# Additional buffer time to ensure database is fully stable
126+
print_status "Allowing database to stabilize..."
127+
sleep 5
128+
109129
cd ../..
110130

111131
# Step 3: Install and start Backend Catalog Service
@@ -172,11 +192,29 @@ cd ..
172192
# Step 5: Seed the database
173193
print_status "Seeding database with sample data..."
174194

195+
# Double-check database is ready before seeding
196+
if ! PGPASSWORD=gitpod psql -h localhost -U gitpod -d gitpodflix -c "SELECT COUNT(*) FROM information_schema.tables WHERE table_name = 'movies'" >/dev/null 2>&1; then
197+
print_error "Database schema not ready for seeding"
198+
exit 1
199+
fi
200+
175201
if [ -f "database/main/seeds/movies_complete.sql" ]; then
176-
PGPASSWORD=gitpod psql -h localhost -U gitpod -d gitpodflix -f database/main/seeds/movies_complete.sql >/dev/null 2>&1
177-
print_success "Database seeded with sample movies"
202+
print_status "Seeding database from SQL file..."
203+
if PGPASSWORD=gitpod psql -h localhost -U gitpod -d gitpodflix -f database/main/seeds/movies_complete.sql >/dev/null 2>&1; then
204+
print_success "Database seeded with sample movies"
205+
else
206+
print_error "Failed to seed database from SQL file"
207+
# Try API fallback
208+
print_status "Attempting API seeding fallback..."
209+
if curl -s -X POST http://localhost:3001/api/movies/seed >/dev/null 2>&1; then
210+
print_success "Database seeded via API fallback"
211+
else
212+
print_warning "Could not seed database - continuing anyway"
213+
fi
214+
fi
178215
else
179216
# Fallback to API seeding
217+
print_status "SQL seed file not found, using API seeding..."
180218
if curl -s -X POST http://localhost:3001/api/movies/seed >/dev/null 2>&1; then
181219
print_success "Database seeded via API"
182220
else

0 commit comments

Comments
 (0)