Works now pls test
This commit is contained in:
parent
69c151162e
commit
cc8b3ed638
2 changed files with 450 additions and 446 deletions
14
src/stats.ts
14
src/stats.ts
|
@ -46,7 +46,7 @@ return {};
|
||||||
|
|
||||||
function saveUptimeObject(): void {
|
function saveUptimeObject(): void {
|
||||||
fs.writeFile(
|
fs.writeFile(
|
||||||
`${__dirname}/uptime.json`,
|
path.join(__dirname, "..", "uptime.json"),
|
||||||
JSON.stringify(uptimeObject),
|
JSON.stringify(uptimeObject),
|
||||||
(error) => {
|
(error) => {
|
||||||
if (error) {
|
if (error) {
|
||||||
|
@ -86,6 +86,7 @@ export async function observe(instances: Instance[]): Promise<void> {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
activeInstances.add(instance.name);
|
activeInstances.add(instance.name);
|
||||||
|
await checkHealth(instance, api); // Ensure health is checked immediately
|
||||||
scheduleHealthCheck(instance, api);
|
scheduleHealthCheck(instance, api);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error resolving instance:", error);
|
console.error("Error resolving instance:", error);
|
||||||
|
@ -124,14 +125,16 @@ export async function observe(instances: Instance[]): Promise<void> {
|
||||||
tries = 0
|
tries = 0
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
try {
|
try {
|
||||||
const response = await fetch(`${api}ping`, { method: "HEAD" });
|
const response = await fetch(`${api}/ping`, { method: "HEAD" });
|
||||||
|
console.log(`Checking health for ${instance.name}: ${response.status}`);
|
||||||
if (response.ok || tries > 3) {
|
if (response.ok || tries > 3) {
|
||||||
|
console.log(`Setting status for ${instance.name} to ${response.ok}`);
|
||||||
setStatus(instance, response.ok);
|
setStatus(instance, response.ok);
|
||||||
} else {
|
} else {
|
||||||
retryHealthCheck(instance, api, tries);
|
retryHealthCheck(instance, api, tries);
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error checking health:", error);
|
console.error(`Error checking health for ${instance.name}:`, error);
|
||||||
if (tries > 3) {
|
if (tries > 3) {
|
||||||
setStatus(instance, false);
|
setStatus(instance, false);
|
||||||
} else {
|
} else {
|
||||||
|
@ -240,12 +243,13 @@ export async function observe(instances: Instance[]): Promise<void> {
|
||||||
uptimeObject[name] = obj;
|
uptimeObject[name] = obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (obj.at(-1)?.online !== status) {
|
const lastEntry = obj.at(-1);
|
||||||
|
if (!lastEntry || lastEntry.online !== status) {
|
||||||
obj.push({ time: Date.now(), online: status });
|
obj.push({ time: Date.now(), online: status });
|
||||||
saveUptimeObject();
|
saveUptimeObject();
|
||||||
}
|
|
||||||
|
|
||||||
if (typeof instance !== "string") {
|
if (typeof instance !== "string") {
|
||||||
calcStats(instance);
|
calcStats(instance);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue