← back to Dw Pairs Well

deploy/reload-visual-search.sh

48 lines

#!/bin/bash
# Restart the pm2 process that ACTUALLY listens on :9914 so it loads the freshly
# rsynced search_service.py (new POST /pairwise handler + ACTIVE+published load() join).
# block2-clip-flip.sh resolved the process by name substring "visual", which wrongly
# matched paint-visualizer-agent (visuali-ZER) and restarted the wrong service. This
# resolves by the listening port — deterministic, no name guessing.
# RUN FROM MAC2:  bash ~/Projects/dw-pairs-well/deploy/reload-visual-search.sh
set -euo pipefail

ssh root@45.61.58.125 'set -e
  # PID bound to :9914 (try ss, fall back to lsof)
  PORTPID=$(ss -ltnpH "sport = :9914" 2>/dev/null | grep -oE "pid=[0-9]+" | head -1 | cut -d= -f2)
  [ -z "${PORTPID:-}" ] && PORTPID=$(lsof -tiTCP:9914 -sTCP:LISTEN 2>/dev/null | head -1)
  [ -n "${PORTPID:-}" ] || { echo "ABORT: nothing is listening on :9914"; exit 1; }
  echo ":9914 is served by pid $PORTPID"

  # map that pid (or its pm2 wrapper) to a pm2 process name
  PROC=$(pm2 jlist | PORTPID="$PORTPID" python3 -c "
import sys, json, os
want = int(os.environ[\"PORTPID\"])
for p in json.load(sys.stdin):
    if p.get(\"pid\") == want:
        print(p[\"name\"]); break
")
  # fall back: match by cwd/script path containing the visual-search dir
  if [ -z "${PROC:-}" ]; then
    PROC=$(pm2 jlist | python3 -c "
import sys, json
for p in json.load(sys.stdin):
    env = p.get(\"pm2_env\", {})
    path = (env.get(\"pm_cwd\",\"\") + \" \" + env.get(\"pm_exec_path\",\"\")).lower()
    if \"visual-search\" in path and \"paint\" not in p[\"name\"].lower():
        print(p[\"name\"]); break
")
  fi
  [ -n "${PROC:-}" ] || { echo "ABORT: could not map :9914 pid $PORTPID to a pm2 process — pm2 ls and restart manually"; exit 1; }
  echo "restarting pm2 process on :9914 -> $PROC"
  pm2 restart "$PROC"
  sleep 25
  echo -n "health after reload: "; curl -sf http://127.0.0.1:9914/health || { echo "ABORT: :9914 unhealthy after restart"; exit 1; }
  echo
  # confirm the new endpoint is live now
  echo -n "/pairwise present: "
  curl -s -o /dev/null -w "%{http_code}\n" -X POST http://127.0.0.1:9914/pairwise \
    -H "Content-Type: application/json" -d "{\"dw_sku\":\"BGA-47951\",\"candidates\":[\"BGA-47951\"]}"
'
echo "done. 200/4xx (not 404) on /pairwise means the new service is loaded."