|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# Cleans up orphaned Docker containers and stale processes so that |
| 4 | +# `yarn start` can bind all required ports cleanly. |
| 5 | +# |
| 6 | +# Usage: |
| 7 | +# ./_scripts/clean-start.sh # clean up only |
| 8 | +# ./_scripts/clean-start.sh --start # clean up then run yarn start |
| 9 | +# |
| 10 | + |
| 11 | +set -euo pipefail |
| 12 | + |
| 13 | +# ---------- Docker container names used by FXA infrastructure ---------- |
| 14 | +DOCKER_CONTAINERS=( |
| 15 | + mydb # MySQL (port 3306) |
| 16 | + redis-server # Redis (port 6379) |
| 17 | + goaws # SNS/GoAWS (port 4100) |
| 18 | + firebase-tools # Firestore (ports 4400,4500,8085,9090,9299) |
| 19 | + sync # Sync server (port 8000) |
| 20 | + cloud-tasks-emulator # (port 8123) |
| 21 | + cirrus # Cirrus (port 8001) |
| 22 | +) |
| 23 | + |
| 24 | +# ---------- Ports used by FXA (infrastructure + application services) ---------- |
| 25 | +ALL_PORTS=( |
| 26 | + # Infrastructure |
| 27 | + 3306 # MySQL |
| 28 | + 6379 # Redis |
| 29 | + 4100 # SNS / GoAWS |
| 30 | + 4400 # Firestore |
| 31 | + 4500 # Firestore |
| 32 | + 8085 # Firestore |
| 33 | + 9090 # Firestore |
| 34 | + 9299 # Firestore |
| 35 | + 8000 # Sync |
| 36 | + 8123 # Cloud Tasks Emulator |
| 37 | + 8001 # Cirrus |
| 38 | + # Application services |
| 39 | + 3030 # Content server |
| 40 | + 3031 # Payments server |
| 41 | + 3032 # Payments React dev |
| 42 | + 3035 # Payments Next |
| 43 | + 3000 # Settings React (webpack dev server) |
| 44 | + 8080 # 123done |
| 45 | + 8091 # Admin panel |
| 46 | + 9000 # Auth server |
| 47 | + 9001 # Mail helper |
| 48 | + 1111 # Profile server |
| 49 | + 1112 # Profile static |
| 50 | + 1113 # Profile worker |
| 51 | +) |
| 52 | + |
| 53 | +RED='\033[0;31m' |
| 54 | +GREEN='\033[0;32m' |
| 55 | +YELLOW='\033[0;33m' |
| 56 | +NC='\033[0m' # No color |
| 57 | + |
| 58 | +info() { echo -e "${GREEN}[clean]${NC} $*"; } |
| 59 | +warn() { echo -e "${YELLOW}[clean]${NC} $*"; } |
| 60 | +err() { echo -e "${RED}[clean]${NC} $*"; } |
| 61 | + |
| 62 | +# ------------------------------------------------------------------ # |
| 63 | +# 1. Kill PM2 daemon (stops all managed processes) |
| 64 | +# ------------------------------------------------------------------ # |
| 65 | +info "Stopping PM2 daemon..." |
| 66 | +if command -v pm2 &>/dev/null; then |
| 67 | + pm2 kill 2>/dev/null || true |
| 68 | + info "PM2 killed." |
| 69 | +else |
| 70 | + warn "pm2 not found, skipping." |
| 71 | +fi |
| 72 | + |
| 73 | +# ------------------------------------------------------------------ # |
| 74 | +# 2. Remove orphaned Docker containers |
| 75 | +# ------------------------------------------------------------------ # |
| 76 | +info "Removing orphaned Docker containers..." |
| 77 | +for name in "${DOCKER_CONTAINERS[@]}"; do |
| 78 | + if docker ps -a --format '{{.Names}}' 2>/dev/null | grep -qx "$name"; then |
| 79 | + warn " Removing container: $name" |
| 80 | + docker rm -f "$name" 2>/dev/null || true |
| 81 | + fi |
| 82 | +done |
| 83 | +info "Docker containers cleaned." |
| 84 | + |
| 85 | +# ------------------------------------------------------------------ # |
| 86 | +# 3. Kill any processes still holding FXA ports |
| 87 | +# ------------------------------------------------------------------ # |
| 88 | +info "Checking for processes holding FXA ports..." |
| 89 | +KILLED_SOMETHING=false |
| 90 | +for port in "${ALL_PORTS[@]}"; do |
| 91 | + PIDS=$(lsof -ti ":$port" 2>/dev/null || true) |
| 92 | + if [ -n "$PIDS" ]; then |
| 93 | + for pid in $PIDS; do |
| 94 | + CMD=$(ps -p "$pid" -o command= 2>/dev/null || echo "unknown") |
| 95 | + warn " Port $port held by PID $pid ($CMD) — killing" |
| 96 | + kill "$pid" 2>/dev/null || true |
| 97 | + KILLED_SOMETHING=true |
| 98 | + done |
| 99 | + fi |
| 100 | +done |
| 101 | + |
| 102 | +# Give processes a moment to exit gracefully, then force-kill stragglers |
| 103 | +if [ "$KILLED_SOMETHING" = true ]; then |
| 104 | + sleep 2 |
| 105 | + for port in "${ALL_PORTS[@]}"; do |
| 106 | + PIDS=$(lsof -ti ":$port" 2>/dev/null || true) |
| 107 | + if [ -n "$PIDS" ]; then |
| 108 | + for pid in $PIDS; do |
| 109 | + err " Port $port still held by PID $pid — force killing" |
| 110 | + kill -9 "$pid" 2>/dev/null || true |
| 111 | + done |
| 112 | + fi |
| 113 | + done |
| 114 | +fi |
| 115 | +info "Ports are free." |
| 116 | + |
| 117 | +# ------------------------------------------------------------------ # |
| 118 | +# 4. Verify all ports are available |
| 119 | +# ------------------------------------------------------------------ # |
| 120 | +BLOCKED=false |
| 121 | +for port in "${ALL_PORTS[@]}"; do |
| 122 | + if lsof -ti ":$port" &>/dev/null; then |
| 123 | + err " Port $port is STILL in use!" |
| 124 | + BLOCKED=true |
| 125 | + fi |
| 126 | +done |
| 127 | + |
| 128 | +if [ "$BLOCKED" = true ]; then |
| 129 | + err "Some ports could not be freed. You may need to restart Docker Desktop." |
| 130 | + err " killall com.docker.backend && open -a Docker" |
| 131 | + exit 1 |
| 132 | +fi |
| 133 | + |
| 134 | +echo "" |
| 135 | +info "All clean. Ready for yarn start." |
| 136 | + |
| 137 | +# ------------------------------------------------------------------ # |
| 138 | +# 5. Optionally start FXA |
| 139 | +# ------------------------------------------------------------------ # |
| 140 | +if [ "${1:-}" = "--start" ]; then |
| 141 | + echo "" |
| 142 | + info "Running yarn start..." |
| 143 | + exec yarn start |
| 144 | +fi |
0 commit comments