eris/tasks/pingWorkers.ts

33 lines
1.0 KiB
TypeScript
Raw Normal View History

2023-09-10 18:56:17 +00:00
import { Async, Log } from "../deps.ts";
import { getGlobalSession } from "../bot/session.ts";
2023-09-13 09:50:22 +00:00
import { sdGetConfig } from "../common/sdApi.ts";
2023-09-10 18:56:17 +00:00
const logger = () => Log.getLogger();
export const runningWorkers = new Set<string>();
/**
* Periodically ping the workers to see if they are alive.
*/
export async function pingWorkers(): Promise<never> {
while (true) {
try {
const config = await getGlobalSession();
for (const worker of config.workers) {
const status = await sdGetConfig(worker.api).catch(() => null);
2023-09-12 22:53:50 +00:00
const wasRunning = runningWorkers.has(worker.id);
2023-09-10 18:56:17 +00:00
if (status) {
2023-09-12 22:53:50 +00:00
runningWorkers.add(worker.id);
if (!wasRunning) logger().info(`Worker ${worker.id} is online`);
2023-09-10 18:56:17 +00:00
} else {
2023-09-12 22:53:50 +00:00
runningWorkers.delete(worker.id);
if (wasRunning) logger().warning(`Worker ${worker.id} went offline`);
2023-09-10 18:56:17 +00:00
}
}
await Async.delay(60 * 1000);
} catch (err) {
logger().warning(`Pinging workers failed: ${err}`);
}
}
}