fix: cleanup
All checks were successful
Publish Docker Image / docker (push) Successful in 52s

This commit is contained in:
Lee
2024-01-03 08:48:16 +00:00
parent 7369ad4d7b
commit 31176e4025
5 changed files with 448 additions and 68 deletions

View File

@ -1,20 +1,9 @@
import Influx from "./influx/influx";
import Scanner from "./scanner/scanner";
import ServerManager from "./server/serverManager";
/**
* The server manager instance.
*/
export const serverManager = new ServerManager();
/**
* The influx database instance.
*/
export const influx = new Influx();
(async () => {
await serverManager.init();
// The scanner is responsible for scanning all servers
new Scanner();
})();
new ServerManager();

View File

@ -1,27 +0,0 @@
import cron from "node-cron";
import { serverManager } from "..";
import Config from "../../data/config.json";
import { logger } from "../utils/logger";
export default class Scanner {
constructor() {
cron.schedule(Config.scanner.updateCron, () => {
this.scanServers();
});
}
/**
* Start a server scan to ping all servers.
*/
private async scanServers(): Promise<void> {
logger.info(`Scanning servers ${serverManager.getServers().length}`);
// ping all servers in parallel
await Promise.all(
serverManager.getServers().map((server) => server.pingServer())
);
logger.info("Finished scanning servers");
}
}

View File

@ -15,10 +15,6 @@ import { logger } from "../utils/logger";
*/
export type ServerType = "PC" | "PE";
export enum ServerStatus {
OFFLINE = "Unable to reach host",
}
type ServerOptions = {
name: string;
ip: string;
@ -52,11 +48,6 @@ export default class Server {
*/
private type: ServerType;
/**
* The favicon of the server.
*/
private favicon: string | undefined;
/**
* The resolved server information from
* DNS records for a PC server.
@ -148,12 +139,11 @@ export default class Server {
const serverPing = new javaPing.MinecraftServer(ip, port);
return new Promise((resolve, reject) => {
serverPing.ping(Config.scanner.timeout, 765, (err, res) => {
serverPing.ping(Config.pinger.timeout, 765, (err, res) => {
if (err || res == undefined) {
return reject(err);
}
this.favicon = res.favicon; // Set the favicon
resolve({
timestamp: Date.now(),
ip: ip,
@ -224,13 +214,4 @@ export default class Server {
public getType(): ServerType {
return this.type;
}
/**
* Returns the favicon of the server.
*
* @returns the favicon
*/
public getFavicon(): string | undefined {
return this.favicon;
}
}

View File

@ -1,18 +1,15 @@
import cron from "node-cron";
import { logger } from "../utils/logger";
import Server, { ServerType } from "./server";
import Config from "../../data/config.json";
import Servers from "../../data/servers.json";
import { logger } from "../utils/logger";
export default class ServerManager {
private servers: Server[] = [];
constructor() {}
/**
* Loads the servers from the config file.
*/
async init() {
logger.info("Loading servers");
constructor() {
logger.info("Loading servers...");
for (const configServer of Servers) {
const server = new Server({
ip: configServer.ip,
@ -21,7 +18,23 @@ export default class ServerManager {
});
this.servers.push(server);
}
logger.info(`Loaded ${this.servers.length} servers`);
logger.info(`Loaded ${this.servers.length} servers!`);
cron.schedule(Config.pinger.pingCron, () => {
this.pingServers();
});
}
/**
* Ping all servers to update their status.
*/
private async pingServers(): Promise<void> {
logger.info(`Pinging servers ${this.servers.length}`);
// ping all servers in parallel
await Promise.all(this.servers.map((server) => server.pingServer()));
logger.info("Finished pinging servers");
}
/**