Skip to content

Commit 327484f

Browse files
authored
Merge pull request #153 from powerloom/fix/local-collector-env-override
fix: collector port availability check in collector_test.sh
2 parents 0b0f29d + 35516e3 commit 327484f

File tree

2 files changed

+67
-22
lines changed

2 files changed

+67
-22
lines changed

collector_test.sh

Lines changed: 66 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -52,38 +52,84 @@ for host in "${hosts[@]}"; do
5252
fi
5353
done
5454

55-
# Test container status
55+
# Test container status - prioritize existing namespace containers
5656
container_name="snapshotter-lite-local-collector-${FULL_NAMESPACE}"
5757
if ! docker ps | grep -q "$container_name"; then
5858
echo "❌ Collector container not found: $container_name"
59+
test_namespace=false
5960
else
60-
echo "✅ Collector container running: $container_name"
61+
echo "✅ Collector container already running for namespace ${FULL_NAMESPACE}: $container_name"
6162
test_namespace=true
63+
64+
# If namespace container exists, use it regardless of port configuration
65+
if [ "$test_ping" = false ]; then
66+
echo "ℹ️ Existing collector container found for namespace ${FULL_NAMESPACE} but not reachable on configured port ${LOCAL_COLLECTOR_PORT}"
67+
echo "ℹ️ Detecting actual port used by running collector $container_name..."
68+
69+
# Get all port mappings from the running container to find the actual port
70+
actual_port=$(docker port "$container_name" 2>/dev/null | grep '/tcp' | head -1 | cut -d':' -f2)
71+
72+
if [ -n "$actual_port" ] && [ "$actual_port" != "$LOCAL_COLLECTOR_PORT" ]; then
73+
echo "ℹ️ Collector $container_name is running on port $actual_port, updating env file"
74+
sed -i".backup" "s/^LOCAL_COLLECTOR_PORT=.*/LOCAL_COLLECTOR_PORT=${actual_port}/" "${ENV_FILE}"
75+
export LOCAL_COLLECTOR_PORT="$actual_port"
76+
elif [ -z "$actual_port" ]; then
77+
echo "❌ FATAL: Could not detect port for existing collector container ${container_name}"
78+
echo "❌ Cannot proceed: snapshotter won't know which port to connect to, and spawning a new collector will cause container name conflicts"
79+
echo "❌ Please manually stop the existing collector or fix its port configuration"
80+
exit 1
81+
else
82+
echo "ℹ️ Collector $container_name confirmed running on configured port ${LOCAL_COLLECTOR_PORT}"
83+
fi
84+
fi
85+
echo "✅ Using existing collector container for namespace ${FULL_NAMESPACE} on port ${LOCAL_COLLECTOR_PORT}"
86+
exit 100
6287
fi
6388

6489
# Final status check
6590
if [ "$test_ping" = true ] && [ "$test_namespace" = true ]; then
66-
echo "✅ Collector is running and reachable"
91+
echo "✅ Collector is running and reachable on port ${LOCAL_COLLECTOR_PORT} and namespace ${FULL_NAMESPACE}"
6792
exit 100
6893
else
69-
echo "⚠️ No active collector found - searching for available ports..."
70-
for port in {50051..51050}; do
71-
port_is_free=false
72-
if [[ "$PORT_CHECK_CMD" == *"curl"* ]]; then
73-
if ! $PORT_CHECK_CMD "localhost:$port" 2>/dev/null; then
74-
port_is_free=true
94+
echo "⚠️ No active collector found"
95+
96+
# Only search for alternative port if configured port is in use (test_ping=true)
97+
if [ "$test_ping" = true ]; then
98+
echo "⚠️ Configured port ${LOCAL_COLLECTOR_PORT} is in use - searching for other available ports..."
99+
for port in {50051..51050}; do
100+
if [ "$port" = "$LOCAL_COLLECTOR_PORT" ]; then
101+
continue # Skip the port we know is in use
75102
fi
76-
else
77-
if ! $PORT_CHECK_CMD "localhost" "$port" 2>/dev/null; then
78-
port_is_free=true
103+
104+
port_is_free=true
105+
for host in "${hosts[@]}"; do
106+
if command -v nc &> /dev/null; then
107+
if nc -z "${host}" "${port}" 2>/dev/null; then
108+
port_is_free=false
109+
break
110+
fi
111+
elif command -v netcat &> /dev/null; then
112+
if netcat -z "${host}" "${port}" 2>/dev/null; then
113+
port_is_free=false
114+
break
115+
fi
116+
else
117+
if timeout 1 bash -c "</dev/tcp/${host}/${port}" 2>/dev/null; then
118+
port_is_free=false
119+
break
120+
fi
121+
fi
122+
done
123+
124+
if [ "$port_is_free" = true ]; then
125+
echo "✅ Found available port: $port (replacing conflicting configured port ${LOCAL_COLLECTOR_PORT}) for namespace ${FULL_NAMESPACE}"
126+
sed -i".backup" "s/^LOCAL_COLLECTOR_PORT=.*/LOCAL_COLLECTOR_PORT=${port}/" "${ENV_FILE}"
127+
break
79128
fi
80-
fi
81-
82-
if [ "$port_is_free" = true ]; then
83-
echo "✅ Found available port: $port"
84-
sed -i".backup" "s/^LOCAL_COLLECTOR_PORT=.*/LOCAL_COLLECTOR_PORT=${port}/" "${ENV_FILE}"
85-
break
86-
fi
87-
done
129+
done
130+
else
131+
echo "✅ Configured port ${LOCAL_COLLECTOR_PORT} is available for namespace ${FULL_NAMESPACE} - will use it for new collector"
132+
fi
133+
88134
exit 101
89135
fi

configure-environment.sh

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -741,9 +741,8 @@ handle_existing_env_file() {
741741
fi
742742
export DATA_MARKET_CONTRACT="$uniswap_v2_dm_contract"
743743
fi
744+
update_common_config "$ENV_FILE_PATH"
744745
fi
745-
746-
update_common_config "$ENV_FILE_PATH"
747746
}
748747

749748
# Function to create new default environment file

0 commit comments

Comments
 (0)