This commit is contained in:
Lee
2024-01-03 07:25:17 +00:00
parent 4ed5d2af84
commit e603f65fdc
18 changed files with 1350 additions and 639 deletions

View File

@ -1,7 +1,6 @@
import cron from "node-cron";
import { database, serverManager, websocketServer } from "..";
import Server, { ServerStatus } from "../server/server";
import { serverManager } from "..";
import Config from "../../data/config.json";
import { logger } from "../utils/logger";
@ -22,43 +21,9 @@ export default class Scanner {
// ping all servers in parallel
await Promise.all(
serverManager.getServers().map((server) => this.scanServer(server))
serverManager.getServers().map((server) => server.pingServer())
);
logger.info("Finished scanning servers");
}
/**
* Scans a server and inserts the ping into the database.
*
* @param server the server to scan
* @returns a promise that resolves when the server has been scanned
*/
async scanServer(server: Server): Promise<void> {
//logger.info(`Scanning server ${server.getIP()} - ${server.getType()}`);
let response;
let online = false;
try {
response = await server.pingServer();
if (response == undefined) {
return; // Server is offline
}
online = true;
} catch (err) {
logger.info(`Failed to ping ${server.getIP()}`, err);
websocketServer.sendServerError(server, ServerStatus.OFFLINE);
return;
}
if (!online || !response) {
return; // Server is offline
}
database.insertPing(server, response);
const isNewRecord = database.insertRecord(server, response);
// todo: send all server pings at once
websocketServer.sendNewPing(server, response, isNewRecord);
}
}