-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrestart.sh
More file actions
executable file
·157 lines (140 loc) · 4.83 KB
/
restart.sh
File metadata and controls
executable file
·157 lines (140 loc) · 4.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/env bash
# restart.sh — Kill and restart all Enso services (guardian + server + Vite + tunnel)
set -euo pipefail
ENSO_PORT="${ENSO_PORT:-3001}"
VITE_PORT="${VITE_PORT:-5173}"
ENSO_DIR="$(cd "$(dirname "$0")" && pwd)"
ENSO_HOME="$HOME/.enso"
GUARDIAN_PID_FILE="$ENSO_HOME/guardian.pid"
SERVER_PID_FILE="$ENSO_HOME/server.pid"
WATCHDOG_LABEL="ai.enso.watchdog"
UID_NUM="$(id -u)"
echo "=== Restarting Enso services ==="
# ── 0. Stop watchdog (prevent interference during restart) ──
echo "[watchdog] Stopping watchdog"
launchctl bootout "gui/$UID_NUM/$WATCHDOG_LABEL" 2>/dev/null || true
# ── 0b. Stop Cloudflare Tunnel ──
CF_PIDS=$(pgrep -f 'cloudflared tunnel' 2>/dev/null || true)
if [ -n "$CF_PIDS" ]; then
echo "[tunnel] Stopping (PIDs: $CF_PIDS)"
kill $CF_PIDS 2>/dev/null || true
sleep 1
else
echo "[tunnel] Not running"
fi
# ── 1. Stop Vite dev server ──
VITE_PIDS=$(pgrep -f "${ENSO_DIR}/node_modules/.bin/vite" 2>/dev/null || true)
if [ -n "$VITE_PIDS" ]; then
echo "[vite] Stopping (PIDs: $VITE_PIDS)"
kill $VITE_PIDS 2>/dev/null || true
sleep 1
else
echo "[vite] Not running"
fi
# ── 2. Stop guardian + server ──
echo "[enso] Stopping guardian + server"
# Kill by PID files first (most reliable)
for PIDFILE in "$GUARDIAN_PID_FILE" "$SERVER_PID_FILE"; do
if [ -f "$PIDFILE" ]; then
PID=$(cat "$PIDFILE" 2>/dev/null | tr -d '[:space:]')
if [ -n "$PID" ] && kill -0 "$PID" 2>/dev/null; then
echo "[enso] Killing PID $PID ($(basename "$PIDFILE"))"
kill "$PID" 2>/dev/null || true
fi
rm -f "$PIDFILE"
fi
done
# Also sweep for orphaned processes
sleep 1
ENSO_PIDS=$(pgrep -f 'standalone\.ts|guardian\.ts|enso.*server' 2>/dev/null || true)
if [ -n "$ENSO_PIDS" ]; then
echo "[enso] Killing orphaned processes: $ENSO_PIDS"
kill $ENSO_PIDS 2>/dev/null || true
sleep 1
FORCE_PIDS=$(pgrep -f 'standalone\.ts|guardian\.ts|enso.*server' 2>/dev/null || true)
if [ -n "$FORCE_PIDS" ]; then
echo "[enso] Force killing: $FORCE_PIDS"
kill -9 $FORCE_PIDS 2>/dev/null || true
fi
else
echo "[enso] No orphaned processes"
fi
sleep 1
# ── 3. Pull latest code ──
echo "[git] Pulling latest code"
cd "$ENSO_DIR"
git pull --ff-only 2>&1 | tail -1 || echo "[git] Pull failed or nothing to pull"
# ── 4. Start guardian (supervises the server) ──
echo "[enso] Starting guardian"
cd "$ENSO_DIR"
nohup npx tsx server/guardian.ts > /tmp/enso-guardian.log 2>&1 &
GUARDIAN_PID=$!
echo -n "[enso] Waiting for server"
for i in $(seq 1 30); do
if curl -sf "http://localhost:$ENSO_PORT/health" &>/dev/null; then
echo " ready (guardian PID: $GUARDIAN_PID)"
break
fi
echo -n "."
sleep 1
done
if ! curl -sf "http://localhost:$ENSO_PORT/health" &>/dev/null; then
echo " TIMEOUT (port $ENSO_PORT not responding)"
fi
# ── 5. Start Vite dev server ──
echo "[vite] Starting dev server (--host)"
cd "$ENSO_DIR"
nohup npm run dev -- --host > /tmp/enso-vite.log 2>&1 &
VITE_PID=$!
echo -n "[vite] Waiting for dev server"
for i in $(seq 1 10); do
if curl -sf "http://localhost:$VITE_PORT" &>/dev/null; then
echo " ready (PID: $VITE_PID)"
break
fi
echo -n "."
sleep 1
done
if ! curl -sf "http://localhost:$VITE_PORT" &>/dev/null; then
echo " TIMEOUT (port $VITE_PORT not responding)"
fi
# ── 6. Start Cloudflare Tunnel ──
if [ -f "$HOME/.cloudflared/config.yml" ]; then
echo "[tunnel] Starting cloudflared"
nohup cloudflared tunnel --protocol http2 run > /tmp/cloudflared.log 2>&1 &
CF_PID=$!
sleep 2
if kill -0 $CF_PID 2>/dev/null; then
echo "[tunnel] Running (PID: $CF_PID)"
else
echo "[tunnel] Failed to start (check /tmp/cloudflared.log)"
fi
else
echo "[tunnel] No config found (~/.cloudflared/config.yml)"
fi
# ── 7. Restart watchdog ──
WATCHDOG_PLIST="$HOME/Library/LaunchAgents/${WATCHDOG_LABEL}.plist"
if [ -f "$WATCHDOG_PLIST" ]; then
echo "[watchdog] Restarting watchdog"
launchctl bootstrap "gui/$UID_NUM" "$WATCHDOG_PLIST" 2>/dev/null || true
launchctl kickstart "gui/$UID_NUM/$WATCHDOG_LABEL" 2>/dev/null || true
else
echo "[watchdog] Not installed — run install-watchdog.sh to install"
fi
# ── Summary ──
echo ""
echo "=== Services ==="
echo " Guardian: supervised, auto-restart on crash"
echo " Server: http://localhost:$ENSO_PORT/health"
echo " Vite UI: http://localhost:$VITE_PORT"
IP=$(ipconfig getifaddr en0 2>/dev/null || hostname -I 2>/dev/null | awk '{print $1}' || echo "unknown")
echo " Network: http://$IP:$VITE_PORT"
if [ -f "$HOME/.cloudflared/config.yml" ]; then
TUNNEL_HOST=$(grep 'hostname:' "$HOME/.cloudflared/config.yml" | head -1 | awk '{print $3}')
[ -n "$TUNNEL_HOST" ] && echo " Tunnel: https://$TUNNEL_HOST"
fi
echo " Logs:"
echo " Guardian: /tmp/enso-guardian.log + ~/.enso/guardian.log"
echo " Vite: /tmp/enso-vite.log"
echo " Crashes: ~/.enso/crashes/"
echo ""