Skip to content

Instantly share code, notes, and snippets.

@Iucasmaia
Created March 17, 2026 23:52
Show Gist options
  • Select an option

  • Save Iucasmaia/91e12f6edc44df386be71ac3ef0e86f9 to your computer and use it in GitHub Desktop.

Select an option

Save Iucasmaia/91e12f6edc44df386be71ac3ef0e86f9 to your computer and use it in GitHub Desktop.
test-reconnect.sh
#!/bin/bash
set -e
QUERY="SELECT id, status, \"updatedAt\" FROM whatsapp_sessions ORDER BY \"updatedAt\" DESC LIMIT 5;"
DB_CMD="docker exec postgres_db psql -U maia -d postgresdb -c"
cleanup() {
iptables -D OUTPUT -p tcp --dport 443 -j DROP 2>/dev/null || true
}
trap cleanup EXIT
echo "=== [0/5] Ensuring port 443 is unblocked ==="
cleanup
echo "=== [1/5] Triggering tsx restart ==="
touch /root/projetos/maiats/src/modules/whatsapp/whatsapp-connection-handler.ts
echo "=== [2/5] Waiting for server to restart and session to open (~15s) ==="
sleep 15
echo "=== Status before blocking ==="
$DB_CMD "$QUERY"
echo ""
echo "=== [3/5] Blocking port 443 and killing existing TCP connection ==="
iptables -I OUTPUT -p tcp --dport 443 -j DROP
# Force immediate death of the already-established TCP connection (without this,
# the kernel retransmits for minutes before reporting an error to the process)
ss --kill state established dst :443 2>/dev/null || true
echo "Port 443 blocked and TCP connection terminated. Waiting for Baileys to detect (~10s)..."
sleep 10
echo "=== [4/5] Checking session status in database ==="
$DB_CMD "$QUERY"
echo ""
echo "=== [5/5] Unblocking port 443 and waiting for automatic reconnection (~15s) ==="
iptables -D OUTPUT -p tcp --dport 443 -j DROP
sleep 15
echo "=== Final session status ==="
$DB_CMD "$QUERY"
echo ""
echo "Expected result:"
echo " - Before blocking: status = 'open'"
echo " - During blocking: status = 'restarting'"
echo " - After unblocking: status = 'open' (reconnected automatically)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment