1313if [ -z " $SCRIPT_DIR " ]; then
1414 SCRIPT_DIR=" $( cd " $( dirname " ${BASH_SOURCE[0]} " ) " && pwd) "
1515fi
16- source " $SCRIPT_DIR /scripts/utils.sh"
17- source " $SCRIPT_DIR /scripts/config.sh"
16+
17+ # If SCRIPT_DIR is the root directory, we need to source from scripts subdirectory
18+ if [ " $( basename " $SCRIPT_DIR " ) " = " scripts" ]; then
19+ source " $SCRIPT_DIR /utils.sh"
20+ source " $SCRIPT_DIR /config.sh"
21+ else
22+ source " $SCRIPT_DIR /scripts/utils.sh"
23+ source " $SCRIPT_DIR /scripts/config.sh"
24+ fi
1825
1926# Configuration (now from config)
2027NAMESPACE=$( get_k8s_namespace)
@@ -153,9 +160,56 @@ wait_for_port_forward() {
153160
154161# Start router port forwarding
155162start_router_port_forward () {
156- start_port_forward " apollo-router" " $ROUTER_GRAPHQL_PORT "
157- if [ $? -eq 0 ]; then
163+ # Use a single port-forward command to forward both GraphQL and health ports
164+ local pid_file=$( get_pid_file " apollo-router" )
165+
166+ # Check if already running
167+ if is_port_forward_running " apollo-router" ; then
168+ local pid=$( cat " $pid_file " )
169+ print_warning " Port forwarding for apollo-router is already running (PID: $pid )"
170+ return 0
171+ fi
172+
173+ # Check if service exists
174+ if ! kubectl get svc " apollo-router-service" -n " $NAMESPACE " > /dev/null 2>&1 ; then
175+ print_error " Service apollo-router-service not found in namespace $NAMESPACE "
176+ return 1
177+ fi
178+
179+ # Start port forwarding for both ports
180+ print_status " Starting port forwarding for apollo-router (GraphQL: $ROUTER_GRAPHQL_PORT , Health: $ROUTER_HEALTH_PORT )..."
181+
182+ # Create a temporary log file for debugging
183+ local temp_log=$( mktemp)
184+
185+ # Forward both ports in a single command
186+ kubectl port-forward " svc/apollo-router-service" " $ROUTER_GRAPHQL_PORT :$ROUTER_GRAPHQL_PORT " " $ROUTER_HEALTH_PORT :$ROUTER_HEALTH_PORT " -n " $NAMESPACE " > " $temp_log " 2>&1 &
187+ local pid=$!
188+
189+ # Save PID
190+ echo " $pid " > " $pid_file "
191+
192+ # Wait a moment for port forward to establish
193+ sleep 3
194+
195+ # Verify it's working
196+ if kill -0 " $pid " 2> /dev/null; then
197+ print_success " Port forwarding for apollo-router started (PID: $pid )"
198+ rm -f " $temp_log "
199+
200+ # Wait for both ports to be listening
158201 wait_for_port_forward " apollo-router" " $ROUTER_GRAPHQL_PORT "
202+ wait_for_port_forward " apollo-router" " $ROUTER_HEALTH_PORT "
203+ else
204+ print_error " Failed to start port forwarding for apollo-router"
205+ print_error " Process died immediately. Checking logs..."
206+ if [ -f " $temp_log " ]; then
207+ print_error " Port forwarding logs:"
208+ cat " $temp_log " | head -10
209+ rm -f " $temp_log "
210+ fi
211+ rm -f " $pid_file "
212+ return 1
159213 fi
160214}
161215
@@ -180,10 +234,12 @@ show_port_forward_status() {
180234 print_status " Port Forwarding Status:"
181235 echo " "
182236
183- # Check router
237+ # Check router (both GraphQL and health)
184238 if is_port_forward_running " apollo-router" ; then
185239 local pid=$( cat $( get_pid_file " apollo-router" ) )
186- print_success " Router: Running (PID: $pid ) - http://localhost:$ROUTER_GRAPHQL_PORT "
240+ print_success " Router: Running (PID: $pid )"
241+ print_success " - GraphQL: http://localhost:$ROUTER_GRAPHQL_PORT "
242+ print_success " - Health: http://localhost:$ROUTER_HEALTH_PORT "
187243 else
188244 print_error " Router: Not running"
189245 fi
@@ -203,16 +259,15 @@ show_port_forward_status() {
203259 echo " - Status: source scripts/port-forward-utils.sh && show_port_forward_status"
204260}
205261
206- # Cleanup function for script exit
262+ # Cleanup function for script exit (only when explicitly stopping)
207263cleanup_on_exit () {
208- # Only cleanup if this script is being run directly
209- if [[ " ${BASH_SOURCE[0]} " == " ${0} " ]]; then
264+ # Only cleanup if this script is being run directly and explicitly stopping
265+ if [[ " ${BASH_SOURCE[0]} " == " ${0} " ]] && [[ " $1 " == " stop " ]] ; then
210266 stop_all_port_forward
211267 fi
212268}
213269
214- # Set trap for cleanup
215- trap cleanup_on_exit EXIT
270+ # Note: Removed automatic trap to prevent port forwarding from stopping immediately
216271
217272# If script is run directly, show status
218273if [[ " ${BASH_SOURCE[0]} " == " ${0} " ]]; then
0 commit comments