Skip to content
86 changes: 66 additions & 20 deletions collector_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -52,38 +52,84 @@ for host in "${hosts[@]}"; do
fi
done

# Test container status
# Test container status - prioritize existing namespace containers
container_name="snapshotter-lite-local-collector-${FULL_NAMESPACE}"
if ! docker ps | grep -q "$container_name"; then
echo "❌ Collector container not found: $container_name"
test_namespace=false
else
echo "✅ Collector container running: $container_name"
echo "✅ Collector container already running for namespace ${FULL_NAMESPACE}: $container_name"
test_namespace=true

# If namespace container exists, use it regardless of port configuration
if [ "$test_ping" = false ]; then
echo "ℹ️ Existing collector container found for namespace ${FULL_NAMESPACE} but not reachable on configured port ${LOCAL_COLLECTOR_PORT}"
echo "ℹ️ Detecting actual port used by running collector $container_name..."

# Get all port mappings from the running container to find the actual port
actual_port=$(docker port "$container_name" 2>/dev/null | grep '/tcp' | head -1 | cut -d':' -f2)

if [ -n "$actual_port" ] && [ "$actual_port" != "$LOCAL_COLLECTOR_PORT" ]; then
echo "ℹ️ Collector $container_name is running on port $actual_port, updating env file"
sed -i".backup" "s/^LOCAL_COLLECTOR_PORT=.*/LOCAL_COLLECTOR_PORT=${actual_port}/" "${ENV_FILE}"
export LOCAL_COLLECTOR_PORT="$actual_port"
elif [ -z "$actual_port" ]; then
echo "❌ FATAL: Could not detect port for existing collector container ${container_name}"
echo "❌ Cannot proceed: snapshotter won't know which port to connect to, and spawning a new collector will cause container name conflicts"
echo "❌ Please manually stop the existing collector or fix its port configuration"
exit 1
else
echo "ℹ️ Collector $container_name confirmed running on configured port ${LOCAL_COLLECTOR_PORT}"
fi
fi
echo "✅ Using existing collector container for namespace ${FULL_NAMESPACE} on port ${LOCAL_COLLECTOR_PORT}"
exit 100
fi

# Final status check
if [ "$test_ping" = true ] && [ "$test_namespace" = true ]; then
echo "✅ Collector is running and reachable"
echo "✅ Collector is running and reachable on port ${LOCAL_COLLECTOR_PORT} and namespace ${FULL_NAMESPACE}"
exit 100
else
echo "⚠️ No active collector found - searching for available ports..."
for port in {50051..51050}; do
port_is_free=false
if [[ "$PORT_CHECK_CMD" == *"curl"* ]]; then
if ! $PORT_CHECK_CMD "localhost:$port" 2>/dev/null; then
port_is_free=true
echo "⚠️ No active collector found"

# Only search for alternative port if configured port is in use (test_ping=true)
if [ "$test_ping" = true ]; then
echo "⚠️ Configured port ${LOCAL_COLLECTOR_PORT} is in use - searching for other available ports..."
for port in {50051..51050}; do
if [ "$port" = "$LOCAL_COLLECTOR_PORT" ]; then
continue # Skip the port we know is in use
fi
else
if ! $PORT_CHECK_CMD "localhost" "$port" 2>/dev/null; then
port_is_free=true

port_is_free=true
for host in "${hosts[@]}"; do
if command -v nc &> /dev/null; then
if nc -z "${host}" "${port}" 2>/dev/null; then
port_is_free=false
break
fi
elif command -v netcat &> /dev/null; then
if netcat -z "${host}" "${port}" 2>/dev/null; then
port_is_free=false
break
fi
else
if timeout 1 bash -c "</dev/tcp/${host}/${port}" 2>/dev/null; then
port_is_free=false
break
fi
fi
done

if [ "$port_is_free" = true ]; then
echo "✅ Found available port: $port (replacing conflicting configured port ${LOCAL_COLLECTOR_PORT}) for namespace ${FULL_NAMESPACE}"
sed -i".backup" "s/^LOCAL_COLLECTOR_PORT=.*/LOCAL_COLLECTOR_PORT=${port}/" "${ENV_FILE}"
break
fi
fi

if [ "$port_is_free" = true ]; then
echo "✅ Found available port: $port"
sed -i".backup" "s/^LOCAL_COLLECTOR_PORT=.*/LOCAL_COLLECTOR_PORT=${port}/" "${ENV_FILE}"
break
fi
done
done
else
echo "✅ Configured port ${LOCAL_COLLECTOR_PORT} is available for namespace ${FULL_NAMESPACE} - will use it for new collector"
fi

exit 101
fi
3 changes: 1 addition & 2 deletions configure-environment.sh
Original file line number Diff line number Diff line change
Expand Up @@ -741,9 +741,8 @@ handle_existing_env_file() {
fi
export DATA_MARKET_CONTRACT="$uniswap_v2_dm_contract"
fi
update_common_config "$ENV_FILE_PATH"
fi

update_common_config "$ENV_FILE_PATH"
}

# Function to create new default environment file
Expand Down