fix: serve cached server data before background sync
This commit is contained in:
@@ -7,6 +7,7 @@ const API_BASE = "/api/v1";
|
||||
const REFRESH_INTERVAL = 30000; // 30 secondi
|
||||
let activeServerId = null;
|
||||
let activeHost = null;
|
||||
let nextSyncTimeout = null;
|
||||
|
||||
// Formattare bytes
|
||||
function formatBytes(bytes) {
|
||||
@@ -99,6 +100,30 @@ async function fetchAllModelsShow(models) {
|
||||
return showByModel;
|
||||
}
|
||||
|
||||
async function fetchRunningModels() {
|
||||
if (!activeHost) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await fetch(buildApiUrl(`${API_BASE}/models/running`));
|
||||
if (!response.ok) {
|
||||
throw new Error("Failed to load running models");
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
return {
|
||||
models: data.models || [],
|
||||
total: data.total || (data.models || []).length,
|
||||
timestamp: new Date().toISOString(),
|
||||
serverId: activeServerId
|
||||
};
|
||||
} catch (error) {
|
||||
console.error("Error loading running models:", error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// Sincronizzare i dati
|
||||
async function syncData() {
|
||||
if (!activeHost) {
|
||||
@@ -113,6 +138,7 @@ async function syncData() {
|
||||
|
||||
const health = await fetchHealth();
|
||||
const modelsData = await fetchModels();
|
||||
const runningData = await fetchRunningModels();
|
||||
|
||||
if (modelsData && modelsData.models.length > 0) {
|
||||
modelsData.showByModel = await fetchAllModelsShow(modelsData.models);
|
||||
@@ -126,6 +152,7 @@ async function syncData() {
|
||||
type: "DATA_UPDATED",
|
||||
health,
|
||||
modelsData,
|
||||
runningData,
|
||||
serverId: activeServerId
|
||||
});
|
||||
}
|
||||
@@ -136,18 +163,41 @@ function buildApiUrl(path) {
|
||||
return `${url.pathname}${url.search}`;
|
||||
}
|
||||
|
||||
// Pianificare aggiornamenti periodici
|
||||
setInterval(syncData, REFRESH_INTERVAL);
|
||||
function clearNextSync() {
|
||||
if (nextSyncTimeout) {
|
||||
clearTimeout(nextSyncTimeout);
|
||||
nextSyncTimeout = null;
|
||||
}
|
||||
}
|
||||
|
||||
function scheduleNextSync(lastSyncTimestamp = 0) {
|
||||
clearNextSync();
|
||||
|
||||
const ageMs = lastSyncTimestamp ? Math.max(0, Date.now() - lastSyncTimestamp) : REFRESH_INTERVAL;
|
||||
const delayMs = Math.max(0, REFRESH_INTERVAL - ageMs);
|
||||
|
||||
nextSyncTimeout = setTimeout(async () => {
|
||||
await syncData();
|
||||
scheduleNextSync(Date.now());
|
||||
}, delayMs);
|
||||
}
|
||||
|
||||
// Gestire messaggi dal main thread
|
||||
self.onmessage = (event) => {
|
||||
if (event.data.type === "SET_SERVER") {
|
||||
activeServerId = event.data.serverId || null;
|
||||
activeHost = event.data.host || null;
|
||||
syncData();
|
||||
const lastSyncTimestamp = Number(event.data.lastSyncTimestamp || 0);
|
||||
|
||||
if (event.data.syncImmediately) {
|
||||
syncData().finally(() => scheduleNextSync(Date.now()));
|
||||
return;
|
||||
}
|
||||
|
||||
scheduleNextSync(lastSyncTimestamp);
|
||||
}
|
||||
|
||||
if (event.data.type === "SYNC_NOW") {
|
||||
syncData();
|
||||
syncData().finally(() => scheduleNextSync(Date.now()));
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user