-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart-all.sh
More file actions
executable file
·288 lines (233 loc) · 8.43 KB
/
Copy pathstart-all.sh
File metadata and controls
executable file
·288 lines (233 loc) · 8.43 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
#!/bin/bash
#
# Start all Product-FARM components: DGraph, Backend, Frontend
# Fails if any required port is already in use.
#
set -e
# ==============================================================================
# Configuration
# ==============================================================================
# Directories
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
INFRA_DIR="$SCRIPT_DIR/infrastructure"
BACKEND_DIR="$SCRIPT_DIR/backend"
FRONTEND_DIR="$SCRIPT_DIR/frontend"
DGRAPH_DATA_DIR="$INFRA_DIR/dgraph-data"
# DGraph ports
DGRAPH_ZERO_PORT=5080 # Zero server management
DGRAPH_ALPHA_INTERNAL=7080 # Alpha internal communication
DGRAPH_ALPHA_HTTP=8080 # Alpha HTTP API
DGRAPH_ALPHA_GRPC=9080 # Alpha gRPC (used by backend)
# Backend port
BACKEND_HTTP_PORT=8081 # Backend REST API (8081 to avoid conflict with DGraph)
# Frontend port
FRONTEND_PORT=5173 # Vite dev server
# PIDs for cleanup
DGRAPH_ZERO_PID=""
DGRAPH_ALPHA_PID=""
BACKEND_PID=""
FRONTEND_PID=""
# Log files
LOG_DIR="$SCRIPT_DIR/logs"
mkdir -p "$LOG_DIR"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# ==============================================================================
# Helper Functions
# ==============================================================================
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"
}
# Check if a port is in use
check_port() {
local port=$1
local service=$2
if ss -tuln 2>/dev/null | grep -q ":${port} " || \
netstat -tuln 2>/dev/null | grep -q ":${port} "; then
log_error "Port $port ($service) is already in use!"
return 1
fi
return 0
}
# Check all required ports
check_all_ports() {
log_info "Checking if required ports are available..."
local failed=0
check_port $DGRAPH_ZERO_PORT "DGraph Zero" || failed=1
check_port $DGRAPH_ALPHA_INTERNAL "DGraph Alpha Internal" || failed=1
check_port $DGRAPH_ALPHA_HTTP "DGraph Alpha HTTP" || failed=1
check_port $DGRAPH_ALPHA_GRPC "DGraph Alpha gRPC" || failed=1
check_port $BACKEND_HTTP_PORT "Backend HTTP" || failed=1
check_port $FRONTEND_PORT "Frontend" || failed=1
if [ $failed -eq 1 ]; then
log_error "One or more ports are in use. Please free them and try again."
echo ""
echo "You can check what's using a port with:"
echo " lsof -i :<port>"
echo " ss -tlnp | grep <port>"
exit 1
fi
log_success "All ports are available"
}
# Wait for a service to be ready
wait_for_service() {
local url=$1
local service=$2
local max_attempts=${3:-30}
local attempt=1
log_info "Waiting for $service to be ready..."
while [ $attempt -le $max_attempts ]; do
if curl -s "$url" > /dev/null 2>&1; then
log_success "$service is ready"
return 0
fi
sleep 1
attempt=$((attempt + 1))
done
log_error "$service failed to start within $max_attempts seconds"
return 1
}
# Wait for a port to be listening
wait_for_port() {
local port=$1
local service=$2
local max_attempts=${3:-30}
local attempt=1
log_info "Waiting for $service (port $port) to be ready..."
while [ $attempt -le $max_attempts ]; do
if ss -tuln 2>/dev/null | grep -q ":${port} " || \
netstat -tuln 2>/dev/null | grep -q ":${port} "; then
log_success "$service is listening on port $port"
return 0
fi
sleep 1
attempt=$((attempt + 1))
done
log_error "$service failed to start on port $port within $max_attempts seconds"
return 1
}
# Cleanup function
cleanup() {
echo ""
log_info "Shutting down all services..."
# Kill frontend
if [ -n "$FRONTEND_PID" ] && kill -0 "$FRONTEND_PID" 2>/dev/null; then
log_info "Stopping Frontend (PID: $FRONTEND_PID)..."
kill "$FRONTEND_PID" 2>/dev/null || true
fi
# Kill backend
if [ -n "$BACKEND_PID" ] && kill -0 "$BACKEND_PID" 2>/dev/null; then
log_info "Stopping Backend (PID: $BACKEND_PID)..."
kill "$BACKEND_PID" 2>/dev/null || true
fi
# Kill DGraph Alpha
if [ -n "$DGRAPH_ALPHA_PID" ] && kill -0 "$DGRAPH_ALPHA_PID" 2>/dev/null; then
log_info "Stopping DGraph Alpha (PID: $DGRAPH_ALPHA_PID)..."
kill "$DGRAPH_ALPHA_PID" 2>/dev/null || true
fi
# Kill DGraph Zero
if [ -n "$DGRAPH_ZERO_PID" ] && kill -0 "$DGRAPH_ZERO_PID" 2>/dev/null; then
log_info "Stopping DGraph Zero (PID: $DGRAPH_ZERO_PID)..."
kill "$DGRAPH_ZERO_PID" 2>/dev/null || true
fi
# Also kill by process name as backup
pkill -f "dgraph zero" 2>/dev/null || true
pkill -f "dgraph alpha" 2>/dev/null || true
log_success "All services stopped"
exit 0
}
# ==============================================================================
# Main Script
# ==============================================================================
echo "=============================================="
echo " Product-FARM - Start All Components"
echo "=============================================="
echo ""
echo "Ports:"
echo " DGraph Zero: $DGRAPH_ZERO_PORT"
echo " DGraph Alpha HTTP: $DGRAPH_ALPHA_HTTP"
echo " DGraph Alpha gRPC: $DGRAPH_ALPHA_GRPC"
echo " Backend REST API: $BACKEND_HTTP_PORT"
echo " Frontend: $FRONTEND_PORT"
echo ""
# Set up cleanup trap
trap cleanup SIGINT SIGTERM EXIT
# Check all ports first
check_all_ports
# ==============================================================================
# Start DGraph Zero
# ==============================================================================
log_info "Starting DGraph Zero..."
cd "$INFRA_DIR"
"$INFRA_DIR/dgraph" zero \
--my="localhost:$DGRAPH_ZERO_PORT" \
--replicas=1 \
--raft="idx=1" \
> "$LOG_DIR/dgraph-zero.log" 2>&1 &
DGRAPH_ZERO_PID=$!
wait_for_port $DGRAPH_ZERO_PORT "DGraph Zero" 30 || exit 1
sleep 2 # Give Zero a moment to fully initialize
# ==============================================================================
# Start DGraph Alpha
# ==============================================================================
log_info "Starting DGraph Alpha..."
"$INFRA_DIR/dgraph" alpha \
--my="localhost:$DGRAPH_ALPHA_INTERNAL" \
--zero="localhost:$DGRAPH_ZERO_PORT" \
> "$LOG_DIR/dgraph-alpha.log" 2>&1 &
DGRAPH_ALPHA_PID=$!
wait_for_service "http://localhost:$DGRAPH_ALPHA_HTTP/health" "DGraph Alpha" 30 || exit 1
# ==============================================================================
# Start Backend
# ==============================================================================
log_info "Starting Backend (Rust)... (building in debug mode)"
cd "$BACKEND_DIR"
RUST_LOG=product_farm_api=info cargo run -p product-farm-api -- 0 $BACKEND_HTTP_PORT \
> "$LOG_DIR/backend.log" 2>&1 &
BACKEND_PID=$!
wait_for_port $BACKEND_HTTP_PORT "Backend HTTP" 120 || exit 1
# ==============================================================================
# Start Frontend
# ==============================================================================
log_info "Starting Frontend (Vite)..."
cd "$FRONTEND_DIR"
VITE_API_URL="http://localhost:$BACKEND_HTTP_PORT" npm run dev > "$LOG_DIR/frontend.log" 2>&1 &
FRONTEND_PID=$!
wait_for_port $FRONTEND_PORT "Frontend" 30 || exit 1
# ==============================================================================
# Summary
# ==============================================================================
echo ""
echo "=============================================="
log_success "All services started successfully!"
echo "=============================================="
echo ""
echo "Services:"
echo " DGraph Zero: http://localhost:$DGRAPH_ZERO_PORT (PID: $DGRAPH_ZERO_PID)"
echo " DGraph Alpha: http://localhost:$DGRAPH_ALPHA_HTTP (PID: $DGRAPH_ALPHA_PID)"
echo " Backend REST: http://localhost:$BACKEND_HTTP_PORT (PID: $BACKEND_PID)"
echo " Frontend: http://localhost:$FRONTEND_PORT (PID: $FRONTEND_PID)"
echo ""
echo "Logs:"
echo " DGraph Zero: $LOG_DIR/dgraph-zero.log"
echo " DGraph Alpha: $LOG_DIR/dgraph-alpha.log"
echo " Backend: $LOG_DIR/backend.log"
echo " Frontend: $LOG_DIR/frontend.log"
echo ""
echo "Press Ctrl+C to stop all services"
echo ""
# Keep script running and forward to services
wait