Spaces:
Running
Running
fix: retry connect() forever instead of crashing the Space when pod is restarting
Browse files
server.ts
CHANGED
|
@@ -106,8 +106,14 @@ function broadcast() {
|
|
| 106 |
|
| 107 |
async function connect() {
|
| 108 |
status = 'connecting';
|
| 109 |
-
|
| 110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
|
| 112 |
const models = await fetch(`${ENDPOINT}/v1/models`).then(r => r.json()).catch(() => null);
|
| 113 |
remoteModelId = models?.data?.[0]?.id || REMOTE_MODEL;
|
|
|
|
| 106 |
|
| 107 |
async function connect() {
|
| 108 |
status = 'connecting';
|
| 109 |
+
// Retry the health check forever — if the upstream pod is restarting, we
|
| 110 |
+
// want to wait it out, not crash the Space and fall into RUNTIME_ERROR.
|
| 111 |
+
while (true) {
|
| 112 |
+
const health = await fetch(`${ENDPOINT}/health`).then(r => r.json()).catch(() => null);
|
| 113 |
+
if (health && health.status === 'ok') break;
|
| 114 |
+
trace({ type: 'connect_waiting', endpoint: ENDPOINT });
|
| 115 |
+
await new Promise(r => setTimeout(r, 5000));
|
| 116 |
+
}
|
| 117 |
|
| 118 |
const models = await fetch(`${ENDPOINT}/v1/models`).then(r => r.json()).catch(() => null);
|
| 119 |
remoteModelId = models?.data?.[0]?.id || REMOTE_MODEL;
|