dongsik22 commited on
Commit
68f4bf5
ยท
1 Parent(s): 4c70bc8

self-heal: SOCKS5 watchdog + Space self-restart on tvchak failure

Browse files
Files changed (2) hide show
  1. entrypoint.sh +23 -0
  2. server.js +39 -0
entrypoint.sh CHANGED
@@ -67,6 +67,29 @@ if [ -n "$NAS_SSH_KEY" ]; then
67
  done
68
  # server.js๊ฐ€ NAS pushํ•  ์ˆ˜ ์žˆ๋„๋ก ํ™˜๊ฒฝ๋ณ€์ˆ˜ ๋…ธ์ถœ
69
  export NAS_FAVS_ENABLED=1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
  else
71
  echo "[entrypoint] NAS_SSH_KEY not set โ€” SOCKS5 tunnel disabled"
72
  unset SOCKS5_PROXY
 
67
  done
68
  # server.js๊ฐ€ NAS pushํ•  ์ˆ˜ ์žˆ๋„๋ก ํ™˜๊ฒฝ๋ณ€์ˆ˜ ๋…ธ์ถœ
69
  export NAS_FAVS_ENABLED=1
70
+
71
+ # SOCKS5 ์›Œ์น˜๋…: 30์ดˆ๋งˆ๋‹ค ํฌํŠธ ์‚ด์•„์žˆ๋Š”์ง€ ํ™•์ธ, ์ฃฝ์—ˆ์œผ๋ฉด autossh ์žฌ๊ธฐ๋™
72
+ (
73
+ while true; do
74
+ sleep 30
75
+ if ! (echo > /dev/tcp/127.0.0.1/11080) >/dev/null 2>&1; then
76
+ echo "[watchdog $(date +%H:%M:%S)] SOCKS5 port dead โ€” restarting autossh"
77
+ pkill -f "autossh.*11080" 2>/dev/null
78
+ pkill -f "ssh.*-D 127.0.0.1:11080" 2>/dev/null
79
+ sleep 2
80
+ AUTOSSH_GATETIME=0 autossh -M 0 -f -N \
81
+ -o "ServerAliveInterval=30" -o "ServerAliveCountMax=3" \
82
+ -o "ExitOnForwardFailure=yes" -o "StrictHostKeyChecking=no" \
83
+ -o "UserKnownHostsFile=/dev/null" -o "TCPKeepAlive=yes" \
84
+ -i "$KEY_PATH" -p "${NAS_SSH_PORT:-22}" \
85
+ -D 127.0.0.1:11080 \
86
+ "${NAS_SSH_USER}@${NAS_SSH_HOST}" \
87
+ && echo "[watchdog] autossh respawned" \
88
+ || echo "[watchdog] autossh respawn failed"
89
+ fi
90
+ done
91
+ ) &
92
+ echo "[entrypoint] SOCKS5 watchdog launched (PID $!)"
93
  else
94
  echo "[entrypoint] NAS_SSH_KEY not set โ€” SOCKS5 tunnel disabled"
95
  unset SOCKS5_PROXY
server.js CHANGED
@@ -1485,6 +1485,45 @@ server.listen(PORT, () => {
1485
  setInterval(() => {
1486
  prefetchRankings().catch((e) => console.warn('[prefetch] failed:', e.message));
1487
  }, 60 * 60 * 1000);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1488
  });
1489
 
1490
  // ๋ถ€ํŒ… ์งํ›„ ๋˜๋Š” ์ฃผ๊ธฐ์ ์œผ๋กœ ํ˜ธ์ถœ๋˜๋Š” ์›Œ๋ฐ์—…:
 
1485
  setInterval(() => {
1486
  prefetchRankings().catch((e) => console.warn('[prefetch] failed:', e.message));
1487
  }, 60 * 60 * 1000);
1488
+
1489
+ // self-restart ํ—ฌ์Šค ์›Œ์น˜๋…: tvchak ๊ฒฝ๋กœ๊ฐ€ ์—ฐ์† ์‹คํŒจํ•˜๋ฉด HF Space ์ž์ฒด ์žฌ์‹œ์ž‘.
1490
+ // entrypoint์˜ SOCKS5 ์›Œ์น˜๋…์œผ๋กœ ๋ชป ์‚ด๋ฆฌ๋Š” ๊ฒฝ์šฐ(Playwright ์ข€๋น„ ๋“ฑ)์˜ ์ตœํ›„ ์ˆ˜๋‹จ.
1491
+ // HF_SPACE_ID + HF_TOKEN ์‹œํฌ๋ฆฟ์ด ์žˆ์„ ๋•Œ๋งŒ ํ™œ์„ฑ. 5๋ถ„์— 1ํšŒ ์ฒดํฌ, 3ํšŒ ์—ฐ์† ์‹คํŒจ ์‹œ ๋ฐœ์‚ฌ.
1492
+ if (process.env.HF_SPACE_ID && process.env.HF_TOKEN) {
1493
+ let healthFails = 0;
1494
+ const HEALTH_THRESHOLD = 3;
1495
+ const HEALTH_INTERVAL = 5 * 60 * 1000;
1496
+ let restartCooldown = 0;
1497
+ setInterval(async () => {
1498
+ try {
1499
+ const d = await findLiveDomain();
1500
+ if (!d.domain) throw new Error('no live domain');
1501
+ const r = await tvFetch(d.domain, '/');
1502
+ if (!r.ok || r.status !== 200) throw new Error(`tvFetch ${r.status || r.error}`);
1503
+ if (healthFails > 0) console.log(`[self-heal] recovered (was ${healthFails} fails)`);
1504
+ healthFails = 0;
1505
+ } catch (e) {
1506
+ healthFails++;
1507
+ console.warn(`[self-heal] check failed (${healthFails}/${HEALTH_THRESHOLD}): ${e.message}`);
1508
+ if (healthFails >= HEALTH_THRESHOLD && Date.now() > restartCooldown) {
1509
+ restartCooldown = Date.now() + 15 * 60 * 1000; // 15๋ถ„ ์ฟจ๋‹ค์šด (์žฌ์‹œ์ž‘ ํญ์ฃผ ๋ฐฉ์ง€)
1510
+ healthFails = 0;
1511
+ console.warn(`[self-heal] threshold reached โ€” triggering HF Space restart`);
1512
+ try {
1513
+ const restartUrl = `https://huggingface.co/api/spaces/${process.env.HF_SPACE_ID}/restart`;
1514
+ const resp = await fetch(restartUrl, {
1515
+ method: 'POST',
1516
+ headers: { 'Authorization': `Bearer ${process.env.HF_TOKEN}` },
1517
+ });
1518
+ console.warn(`[self-heal] restart API responded ${resp.status}`);
1519
+ } catch (err) {
1520
+ console.error(`[self-heal] restart API call failed:`, err.message);
1521
+ }
1522
+ }
1523
+ }
1524
+ }, HEALTH_INTERVAL);
1525
+ console.log(`[self-heal] watchdog armed (every ${HEALTH_INTERVAL/1000}s, threshold ${HEALTH_THRESHOLD})`);
1526
+ }
1527
  });
1528
 
1529
  // ๋ถ€ํŒ… ์งํ›„ ๋˜๋Š” ์ฃผ๊ธฐ์ ์œผ๋กœ ํ˜ธ์ถœ๋˜๋Š” ์›Œ๋ฐ์—…: