Spaces:
Paused
Paused
self-heal: SOCKS5 watchdog + Space self-restart on tvchak failure
Browse files- entrypoint.sh +23 -0
- 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 |
// ๋ถํ
์งํ ๋๋ ์ฃผ๊ธฐ์ ์ผ๋ก ํธ์ถ๋๋ ์๋ฐ์
:
|