-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.sh
More file actions
executable file
·730 lines (613 loc) · 24.2 KB
/
Copy pathrun.sh
File metadata and controls
executable file
·730 lines (613 loc) · 24.2 KB
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
#!/usr/bin/env bash
set -euo pipefail
# ============================================================================
# Photobook Full Stack Runner
# Starts: Redis, Backend, Geo-Service Worker, Face-Service, Frontend
# ============================================================================
ROOT_DIR="$(cd "$(dirname "$0")" && pwd)"
BACKEND_DIR="$ROOT_DIR/backend"
GEO_SERVICE_DIR="$ROOT_DIR/services/geo-service"
VLM_SERVICE_DIR="$ROOT_DIR/services/vlm-service"
FACE_SERVICE_DIR="$ROOT_DIR/services/face-service"
SAM_SERVICE_DIR="$ROOT_DIR/services/sam-service"
FRONTEND_DIR="$ROOT_DIR/frontend"
LOGS_DIR="$ROOT_DIR/logs"
BACKEND_PORT=${BACKEND_PORT:-8020}
GEO_API_PORT=${GEO_API_PORT:-8030}
VLM_API_PORT=${VLM_API_PORT:-8031}
FACE_API_PORT=${FACE_API_PORT:-8033}
SAM_API_PORT=${SAM_API_PORT:-8032}
FRONTEND_PORT=${FRONTEND_PORT:-5173}
# Mode: dev (with --reload) or prod (no reload)
MODE="${MODE:-dev}"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
NC='\033[0m' # No Color
log_info() { echo -e "${BLUE}[INFO]${NC} $1"; }
log_success() { echo -e "${GREEN}[OK]${NC} $1"; }
log_warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }
log_error() { echo -e "${RED}[ERROR]${NC} $1"; }
log_header() { echo -e "${CYAN}==> $1${NC}"; }
# ============================================================================
# Parse Command Line Arguments
# ============================================================================
show_usage() {
echo "Usage: $0 [OPTIONS]"
echo ""
echo "Options:"
echo " --dev Run in development mode (with --reload, default)"
echo " --prod Run in production mode (no reload, optimized)"
echo " --no-frontend Skip starting the frontend"
echo " --help Show this help message"
echo ""
echo "Environment Variables:"
echo " BACKEND_PORT Backend API port (default: 8020)"
echo " GEO_API_PORT Geo service API port (default: 8030)"
echo " VLM_API_PORT VLM service API port (default: 8031)"
echo " FACE_API_PORT Face service API port (default: 8033)"
echo " FRONTEND_PORT Frontend dev server port (default: 5173)"
echo ""
}
START_FRONTEND=true
while [[ $# -gt 0 ]]; do
case $1 in
--dev)
MODE="dev"
shift
;;
--prod)
MODE="prod"
shift
;;
--no-frontend)
START_FRONTEND=false
shift
;;
--help|-h)
show_usage
exit 0
;;
*)
log_error "Unknown option: $1"
show_usage
exit 1
;;
esac
done
# Set uvicorn reload flag based on mode
if [ "$MODE" = "dev" ]; then
UVICORN_RELOAD="--reload"
log_info "Running in DEVELOPMENT mode (hot reload enabled)"
else
UVICORN_RELOAD=""
log_info "Running in PRODUCTION mode (no hot reload)"
fi
# Ensure logs directory exists, and rotate anything left from previous runs.
# Services append via `tee -a`, so without this the files grow without bound
# (backend.log had reached 588 MB).
LOG_KEEP=${LOG_KEEP:-3} # generations to retain per log
LOG_MAX_MB=${LOG_MAX_MB:-100} # rotate a log once it exceeds this size
rotate_log() {
local f=$1
[ -f "$f" ] || return 0
local size_mb=$(( $(wc -c < "$f") / 1048576 ))
[ "$size_mb" -lt "$LOG_MAX_MB" ] && return 0
# Shift older generations up: .2 -> .3, .1 -> .2, current -> .1
local i=$LOG_KEEP
while [ "$i" -gt 1 ]; do
[ -f "$f.$((i-1))" ] && mv -f "$f.$((i-1))" "$f.$i"
i=$((i-1))
done
mv -f "$f" "$f.1"
log_info "Rotated $(basename "$f") (was ${size_mb}MB)"
}
setup_logs() {
mkdir -p "$LOGS_DIR"
for f in "$LOGS_DIR"/*.log; do
rotate_log "$f"
done
# Drop generations beyond LOG_KEEP
find "$LOGS_DIR" -name "*.log.[0-9]*" -type f | while read -r old_log; do
local_gen=${old_log##*.}
[ "$local_gen" -gt "$LOG_KEEP" ] 2>/dev/null && rm -f "$old_log"
done
log_info "Logs directory: $LOGS_DIR (rotate >${LOG_MAX_MB}MB, keep $LOG_KEEP)"
}
# Kill existing processes on our ports (macOS compatible)
kill_existing_processes() {
log_info "Killing any existing processes on our ports..."
# Helper to kill process on port (macOS compatible)
kill_port() {
local port=$1
local pids=$(lsof -ti:$port 2>/dev/null)
if [ -n "$pids" ]; then
echo "$pids" | xargs kill -9 2>/dev/null || true
log_info "Killed process on port $port"
fi
}
# Kill processes on all service ports
kill_port $BACKEND_PORT
kill_port $VLM_API_PORT
kill_port $GEO_API_PORT
kill_port $FACE_API_PORT
kill_port $SAM_API_PORT
kill_port $FRONTEND_PORT
# Kill any arq workers for our project
pkill -f "arq.*worker" 2>/dev/null || true
pkill -f "python.*arq" 2>/dev/null || true
# Give processes time to die
sleep 1
log_success "Cleaned up existing processes"
}
# Cleanup function
cleanup() {
log_info "Shutting down services..."
# Kill all background jobs (macOS compatible)
local job_pids=$(jobs -p 2>/dev/null)
if [ -n "$job_pids" ]; then
echo "$job_pids" | xargs kill 2>/dev/null || true
fi
if [ -n "${REDIS_PID:-}" ]; then
kill "$REDIS_PID" 2>/dev/null || true
fi
exit 0
}
trap cleanup SIGINT SIGTERM
# ============================================================================
# Environment Loading (Consolidated)
# ============================================================================
load_geo_service_env() {
# Load API key from .env if not set in environment (only once)
if [ -z "${GOOGLE_PLACES_API_KEY:-}" ] && [ -f "$GEO_SERVICE_DIR/.env" ]; then
export GOOGLE_PLACES_API_KEY=$(grep -E "^GOOGLE_PLACES_API_KEY=" "$GEO_SERVICE_DIR/.env" | cut -d'=' -f2-)
if [ -n "$GOOGLE_PLACES_API_KEY" ]; then
log_success "Loaded GOOGLE_PLACES_API_KEY from geo-service/.env"
fi
fi
# Check for Google API key
if [ -z "${GOOGLE_PLACES_API_KEY:-}" ]; then
log_warn "GOOGLE_PLACES_API_KEY not set - geo-service will fail API calls"
log_info "Set it with: export GOOGLE_PLACES_API_KEY='your-key'"
else
log_success "GOOGLE_PLACES_API_KEY is configured"
fi
}
# ============================================================================
# Check Prerequisites
# ============================================================================
check_prerequisites() {
log_header "Checking prerequisites..."
# Check Python
if [ ! -x "$BACKEND_DIR/.venv/bin/python" ]; then
log_error "Backend environment not found — run ./setup.sh first"
exit 1
fi
# Check configuration
if [ ! -f "$BACKEND_DIR/.env" ]; then
log_error "backend/.env not found — run ./setup.sh first"
exit 1
fi
if ! grep -qE "^SECRET_KEY=.+" "$BACKEND_DIR/.env"; then
log_error "SECRET_KEY is not set in backend/.env"
log_info "Generate one: python3 -c \"import secrets; print(secrets.token_urlsafe(48))\""
exit 1
fi
# Check geo-service venv (create if needed)
if [ ! -x "$GEO_SERVICE_DIR/.venv/bin/python" ]; then
log_warn "Geo-service venv not found, creating..."
python3 -m venv "$GEO_SERVICE_DIR/.venv"
"$GEO_SERVICE_DIR/.venv/bin/pip" install -r "$GEO_SERVICE_DIR/requirements.txt"
fi
# Check vlm-service venv (create if needed)
if [ ! -x "$VLM_SERVICE_DIR/.venv/bin/python" ]; then
log_warn "VLM-service venv not found, creating..."
python3 -m venv "$VLM_SERVICE_DIR/.venv"
"$VLM_SERVICE_DIR/.venv/bin/pip" install -r "$VLM_SERVICE_DIR/requirements.txt"
fi
# Check face-service venv (create if needed)
if [ ! -d "$FACE_SERVICE_DIR/venv" ] && [ ! -x "$FACE_SERVICE_DIR/venv/bin/python" ]; then
log_warn "Face-service venv not found, creating..."
python3 -m venv "$FACE_SERVICE_DIR/venv"
"$FACE_SERVICE_DIR/venv/bin/pip" install -r "$FACE_SERVICE_DIR/requirements.txt"
fi
# Check Redis
if ! command -v redis-server >/dev/null 2>&1; then
log_warn "redis-server not found. Install with: brew install redis"
log_info "Trying to use Docker for Redis..."
if ! command -v docker >/dev/null 2>&1; then
log_error "Neither redis-server nor docker found. Please install Redis."
exit 1
fi
USE_DOCKER_REDIS=true
else
USE_DOCKER_REDIS=false
fi
# Check npm for frontend
if [ "$START_FRONTEND" = true ]; then
if ! command -v npm >/dev/null 2>&1; then
log_warn "npm not found - frontend will not start"
START_FRONTEND=false
elif [ ! -d "$FRONTEND_DIR/node_modules" ]; then
log_warn "Frontend node_modules not found. Installing..."
(cd "$FRONTEND_DIR" && npm install)
fi
fi
log_success "Prerequisites OK"
}
# ============================================================================
# Service Health Check Utilities
# ============================================================================
wait_for_redis() {
log_info "Waiting for Redis to be ready..."
for i in {1..30}; do
if redis-cli ping 2>/dev/null | grep -q PONG; then
log_success "Redis is ready"
return 0
fi
sleep 0.5
done
log_error "Redis failed to become ready"
return 1
}
wait_for_service() {
local name="$1"
local url="$2"
local max_attempts="${3:-30}"
for i in $(seq 1 $max_attempts); do
if curl -s --connect-timeout 3 --max-time 5 "$url" >/dev/null 2>&1; then
return 0
fi
sleep 0.5
done
return 1
}
# ============================================================================
# Consolidated Health Check Function
# ============================================================================
check_all_services_health() {
log_header "Checking all services health..."
echo ""
local all_healthy=true
local status_table=""
# Redis
if redis-cli ping 2>/dev/null | grep -q PONG; then
status_table+=" Redis: ${GREEN}HEALTHY${NC}\n"
else
status_table+=" Redis: ${RED}UNHEALTHY${NC}\n"
all_healthy=false
fi
# Backend
if curl -s --connect-timeout 3 --max-time 5 "http://127.0.0.1:$BACKEND_PORT/health" >/dev/null 2>&1; then
status_table+=" Backend API: ${GREEN}HEALTHY${NC} (http://127.0.0.1:$BACKEND_PORT)\n"
else
status_table+=" Backend API: ${RED}UNHEALTHY${NC}\n"
all_healthy=false
fi
# Geo API
if curl -s --connect-timeout 3 --max-time 5 "http://127.0.0.1:$GEO_API_PORT/health" >/dev/null 2>&1; then
status_table+=" Geo Service API: ${GREEN}HEALTHY${NC} (http://127.0.0.1:$GEO_API_PORT)\n"
else
status_table+=" Geo Service API: ${YELLOW}DEGRADED${NC}\n"
fi
# VLM Service
if curl -s --connect-timeout 3 --max-time 5 "http://127.0.0.1:$VLM_API_PORT/health" >/dev/null 2>&1; then
status_table+=" VLM Service: ${GREEN}HEALTHY${NC} (http://127.0.0.1:$VLM_API_PORT)\n"
else
status_table+=" VLM Service: ${YELLOW}DEGRADED${NC}\n"
fi
# Face Service
if curl -s --connect-timeout 3 --max-time 5 "http://127.0.0.1:$FACE_API_PORT/health" >/dev/null 2>&1; then
status_table+=" Face Service: ${GREEN}HEALTHY${NC} (http://127.0.0.1:$FACE_API_PORT)\n"
else
status_table+=" Face Service: ${YELLOW}DEGRADED${NC}\n"
fi
# Workers (check if PIDs are running)
if [ -n "${BACKEND_WORKER_PID:-}" ] && kill -0 "$BACKEND_WORKER_PID" 2>/dev/null; then
status_table+=" Backend Worker: ${GREEN}RUNNING${NC} (PID: $BACKEND_WORKER_PID)\n"
else
status_table+=" Backend Worker: ${YELLOW}NOT RUNNING${NC}\n"
fi
if [ -n "${GEO_WORKER_PID:-}" ] && kill -0 "$GEO_WORKER_PID" 2>/dev/null; then
status_table+=" Geo Worker: ${GREEN}RUNNING${NC} (PID: $GEO_WORKER_PID)\n"
else
status_table+=" Geo Worker: ${YELLOW}NOT RUNNING${NC}\n"
fi
# Frontend
if [ "$START_FRONTEND" = true ]; then
if curl -s --connect-timeout 3 --max-time 5 "http://127.0.0.1:$FRONTEND_PORT" >/dev/null 2>&1; then
status_table+=" Frontend: ${GREEN}RUNNING${NC} (http://127.0.0.1:$FRONTEND_PORT)\n"
else
status_table+=" Frontend: ${YELLOW}STARTING${NC}\n"
fi
else
status_table+=" Frontend: ${BLUE}SKIPPED${NC}\n"
fi
# Llama servers (optional)
local llama1_status="${RED}NOT DETECTED${NC}"
local llama2_status="${RED}NOT DETECTED${NC}"
if curl -s --connect-timeout 3 --max-time 5 "http://localhost:${LLAMA_PORT_1:-8090}/health" >/dev/null 2>&1; then
llama1_status="${GREEN}HEALTHY${NC}"
fi
if curl -s --connect-timeout 3 --max-time 5 "http://localhost:${LLAMA_PORT_2:-8091}/health" >/dev/null 2>&1; then
llama2_status="${GREEN}HEALTHY${NC}"
fi
status_table+=" Llama Server 1: $llama1_status (port ${LLAMA_PORT_1:-8089})\n"
status_table+=" Llama Server 2: $llama2_status (port ${LLAMA_PORT_2:-8090})\n"
echo -e "$status_table"
if [ "$all_healthy" = true ]; then
log_success "All core services are healthy!"
else
log_warn "Some services are not healthy"
fi
return 0
}
# ============================================================================
# Start Services
# ============================================================================
start_redis() {
log_header "Starting Redis..."
# Check if Redis is already running
if redis-cli ping 2>/dev/null | grep -q PONG; then
log_success "Redis already running"
return
fi
if [ "${USE_DOCKER_REDIS:-false}" = true ]; then
docker run -d --name photobook-redis -p 6379:6379 redis:alpine 2>/dev/null || \
docker start photobook-redis 2>/dev/null || true
else
redis-server --daemonize yes
fi
# Wait for Redis with proper dependency check
if ! wait_for_redis; then
log_error "Failed to start Redis"
exit 1
fi
}
start_backend() {
log_header "Starting Backend on port $BACKEND_PORT..."
cd "$BACKEND_DIR"
"$BACKEND_DIR/.venv/bin/python" -m uvicorn main:app --host 0.0.0.0 $UVICORN_RELOAD --port "$BACKEND_PORT" 2>&1 | tee -a "$LOGS_DIR/backend.log" &
BACKEND_PID=$!
# Wait for backend to be ready
if wait_for_service "Backend" "http://127.0.0.1:$BACKEND_PORT/health" 30; then
log_success "Backend started (PID: $BACKEND_PID)"
else
log_warn "Backend may still be starting..."
fi
}
start_geo_worker() {
log_header "Starting Geo-Service Worker..."
# Ensure Redis is ready before starting worker
if ! redis-cli ping 2>/dev/null | grep -q PONG; then
log_warn "Redis not ready, waiting..."
wait_for_redis || { log_error "Cannot start geo worker without Redis"; return 1; }
fi
cd "$GEO_SERVICE_DIR"
# Pass the API key to the worker via environment (already loaded by load_geo_service_env)
GOOGLE_PLACES_API_KEY="${GOOGLE_PLACES_API_KEY:-}" \
"$GEO_SERVICE_DIR/.venv/bin/python" -m arq worker.WorkerSettings 2>&1 | tee -a "$LOGS_DIR/geo-service.log" &
GEO_WORKER_PID=$!
sleep 1
if kill -0 "$GEO_WORKER_PID" 2>/dev/null; then
log_success "Geo-Service Worker started (PID: $GEO_WORKER_PID)"
else
log_warn "Geo-Service Worker may have failed to start"
fi
}
start_geo_api() {
log_header "Starting Geo-Service API on port $GEO_API_PORT..."
cd "$GEO_SERVICE_DIR"
# API key already loaded by load_geo_service_env
GOOGLE_PLACES_API_KEY="${GOOGLE_PLACES_API_KEY:-}" \
"$GEO_SERVICE_DIR/.venv/bin/python" -m uvicorn api:app --host 0.0.0.0 --port "$GEO_API_PORT" $UVICORN_RELOAD 2>&1 | tee -a "$LOGS_DIR/geo-api.log" &
GEO_API_PID=$!
# Wait for geo API to be ready
if wait_for_service "Geo API" "http://127.0.0.1:$GEO_API_PORT/health" 20; then
log_success "Geo-Service API started (PID: $GEO_API_PID)"
else
if kill -0 "$GEO_API_PID" 2>/dev/null; then
log_success "Geo-Service API started (PID: $GEO_API_PID)"
else
log_warn "Geo-Service API may have failed to start"
fi
fi
}
start_backend_worker() {
log_header "Starting Backend Worker (VLM/Location tasks)..."
# Ensure Redis is ready before starting worker
if ! redis-cli ping 2>/dev/null | grep -q PONG; then
log_warn "Redis not ready, waiting..."
wait_for_redis || { log_error "Cannot start backend worker without Redis"; return 1; }
fi
cd "$BACKEND_DIR"
"$BACKEND_DIR/.venv/bin/python" -m arq jobs.worker.WorkerSettings 2>&1 | tee -a "$LOGS_DIR/backend-worker.log" &
BACKEND_WORKER_PID=$!
sleep 1
if kill -0 "$BACKEND_WORKER_PID" 2>/dev/null; then
log_success "Backend Worker started (PID: $BACKEND_WORKER_PID)"
else
log_warn "Backend Worker may have failed to start"
fi
}
start_vlm_service() {
log_header "Starting VLM Service on port $VLM_API_PORT..."
cd "$VLM_SERVICE_DIR"
# Check for dual llama.cpp servers (2x RTX 4090 setup)
LLAMA_PORT_1=${LLAMA_PORT_1:-8090}
LLAMA_PORT_2=${LLAMA_PORT_2:-8091}
local llama_count=0
if curl -s --connect-timeout 3 --max-time 5 "http://localhost:$LLAMA_PORT_1/health" >/dev/null 2>&1; then
log_success "llama.cpp server 1 detected on port $LLAMA_PORT_1"
llama_count=$((llama_count + 1))
else
log_warn "llama.cpp server 1 not detected on port $LLAMA_PORT_1"
fi
if curl -s --connect-timeout 3 --max-time 5 "http://localhost:$LLAMA_PORT_2/health" >/dev/null 2>&1; then
log_success "llama.cpp server 2 detected on port $LLAMA_PORT_2"
llama_count=$((llama_count + 1))
else
log_warn "llama.cpp server 2 not detected on port $LLAMA_PORT_2"
fi
if [ "$llama_count" -eq 0 ]; then
log_error "No llama.cpp servers detected!"
log_info "Start them with: cd $VLM_SERVICE_DIR && ./start-dual-llama.sh"
log_info "Or single server: llama-server --model ./models/Qwen3-VL-32B-Q4_K_M.gguf --mmproj ./models/Qwen3-VL-32B-mmproj-f16.gguf --port 8089"
elif [ "$llama_count" -eq 1 ]; then
log_warn "Only 1 llama.cpp server running (2x 4090 setup expects 2)"
else
log_success "Dual llama.cpp servers detected (2x RTX 4090 mode)"
fi
# Start VLM service with dual server config
LLAMA_SERVER_URLS="http://localhost:$LLAMA_PORT_1,http://localhost:$LLAMA_PORT_2" \
SERVICE_PORT="$VLM_API_PORT" \
"$VLM_SERVICE_DIR/.venv/bin/python" -m uvicorn api:app --host 0.0.0.0 --port "$VLM_API_PORT" $UVICORN_RELOAD 2>&1 | tee -a "$LOGS_DIR/vlm-service.log" &
VLM_SERVICE_PID=$!
# Wait for VLM service to be ready
if wait_for_service "VLM Service" "http://127.0.0.1:$VLM_API_PORT/health" 20; then
log_success "VLM Service started (PID: $VLM_SERVICE_PID)"
else
if kill -0 "$VLM_SERVICE_PID" 2>/dev/null; then
log_success "VLM Service started (PID: $VLM_SERVICE_PID)"
else
log_warn "VLM Service may have failed to start"
fi
fi
}
start_face_service() {
log_header "Starting Face Service on port $FACE_API_PORT..."
cd "$FACE_SERVICE_DIR"
# Use venv (face-service uses 'venv' not '.venv')
local FACE_PYTHON="$FACE_SERVICE_DIR/venv/bin/python"
if [ ! -x "$FACE_PYTHON" ]; then
log_warn "Face-service venv not found, creating..."
python3 -m venv "$FACE_SERVICE_DIR/venv"
"$FACE_SERVICE_DIR/venv/bin/pip" install -r "$FACE_SERVICE_DIR/requirements.txt"
fi
"$FACE_PYTHON" -m uvicorn api:app --host 0.0.0.0 --port "$FACE_API_PORT" $UVICORN_RELOAD 2>&1 | tee -a "$LOGS_DIR/face-service.log" &
FACE_SERVICE_PID=$!
# Wait for face service to be ready
if wait_for_service "Face Service" "http://127.0.0.1:$FACE_API_PORT/health" 30; then
log_success "Face Service started (PID: $FACE_SERVICE_PID)"
else
if kill -0 "$FACE_SERVICE_PID" 2>/dev/null; then
log_success "Face Service started (PID: $FACE_SERVICE_PID) - model may still be loading"
else
log_warn "Face Service may have failed to start"
fi
fi
}
start_sam_service() {
log_header "Starting SAM Service on port $SAM_API_PORT..."
cd "$SAM_SERVICE_DIR"
local SAM_PYTHON="$SAM_SERVICE_DIR/venv/bin/python"
if [ ! -x "$SAM_PYTHON" ]; then
log_warn "SAM-service venv not found, creating..."
python3 -m venv "$SAM_SERVICE_DIR/venv"
"$SAM_SERVICE_DIR/venv/bin/pip" install -r "$SAM_SERVICE_DIR/requirements.txt"
fi
"$SAM_PYTHON" -m uvicorn api:app --host 0.0.0.0 --port "$SAM_API_PORT" $UVICORN_RELOAD 2>&1 | tee -a "$LOGS_DIR/sam-service.log" &
SAM_SERVICE_PID=$!
if wait_for_service "SAM Service" "http://127.0.0.1:$SAM_API_PORT/health" 60; then
log_success "SAM Service started (PID: $SAM_SERVICE_PID)"
else
if kill -0 "$SAM_SERVICE_PID" 2>/dev/null; then
log_success "SAM Service started (PID: $SAM_SERVICE_PID) - model may still be loading"
else
log_warn "SAM Service may have failed to start"
fi
fi
}
start_frontend() {
log_header "Starting Frontend..."
if [ ! -d "$FRONTEND_DIR/node_modules" ]; then
log_info "Installing frontend dependencies..."
(cd "$FRONTEND_DIR" && npm install)
fi
cd "$FRONTEND_DIR"
if [ "$MODE" = "dev" ]; then
npm run dev -- --port "$FRONTEND_PORT" &
else
# In production mode, build and serve
log_info "Building frontend for production..."
npm run build
npm run preview -- --port "$FRONTEND_PORT" &
fi
FRONTEND_PID=$!
sleep 3
if kill -0 "$FRONTEND_PID" 2>/dev/null; then
log_success "Frontend started (PID: $FRONTEND_PID)"
else
log_warn "Frontend may have failed to start"
fi
}
# ============================================================================
# Main
# ============================================================================
main() {
echo ""
echo "=========================================="
echo " Photobook Application Launcher"
echo " Mode: $(echo "$MODE" | tr '[:lower:]' '[:upper:]')"
echo "=========================================="
echo ""
setup_logs
kill_existing_processes
check_prerequisites
# Load environment variables once (fixes duplicate loading)
load_geo_service_env
# Start services in dependency order
# 1. Redis first (required by workers)
start_redis
# 2. APIs (can start in parallel with Redis confirmation)
start_backend
# 3. Workers (require Redis to be confirmed running)
start_backend_worker
start_geo_worker
# 4. Additional APIs
start_geo_api
start_vlm_service
start_face_service
start_sam_service
# 5. Frontend (optional)
if [ "$START_FRONTEND" = true ]; then
start_frontend
fi
echo ""
echo "=========================================="
echo " Services Starting Complete"
echo "=========================================="
echo ""
# Give services a moment to stabilize
sleep 2
# Run consolidated health check
check_all_services_health
echo ""
echo "=========================================="
echo " Service URLs"
echo "=========================================="
echo " Backend: http://127.0.0.1:$BACKEND_PORT"
echo " Backend Docs: http://127.0.0.1:$BACKEND_PORT/docs"
echo " Redis: redis://localhost:6379"
echo " Geo API: http://127.0.0.1:$GEO_API_PORT"
echo " VLM Service: http://127.0.0.1:$VLM_API_PORT"
echo " Face Service: http://127.0.0.1:$FACE_API_PORT"
if [ "$START_FRONTEND" = true ]; then
echo " Frontend: http://127.0.0.1:$FRONTEND_PORT"
else
echo " Frontend: (not started - run: cd frontend && npm run dev)"
fi
echo ""
echo " Llama 1: http://localhost:${LLAMA_PORT_1:-8089} (GPU 0)"
echo " Llama 2: http://localhost:${LLAMA_PORT_2:-8090} (GPU 1)"
echo "=========================================="
echo ""
echo "Press Ctrl+C to stop all services"
echo ""
# Wait for background jobs
wait
}
main "$@"