improve shutdown procedure

This commit is contained in:
Holly Stubbs 2024-10-26 14:38:32 +01:00
parent 78c894a94d
commit c521455ec4
Signed by: tgpholly
GPG key ID: B8583C4B7D18119E

View file

@ -77,31 +77,33 @@ export default class MinecraftServer {
// Disconnect all players // Disconnect all players
const kickPacket = new PacketDisconnectKick("Server shutting down.").writeData(); const kickPacket = new PacketDisconnectKick("Server shutting down.").writeData();
this.sendToAllClients(kickPacket); this.sendToAllClients(kickPacket);
// Shut down the tcp server
this.server.close();
// Save chunks // Save chunks
Console.printInfo("Saving worlds..."); Console.printInfo("Saving worlds...");
// There's a race condition here. oops.
const keepRunningInterval = setInterval(() => {}, 1000);
let savedWorldCount = 0; let savedWorldCount = 0;
let savedChunkCount = 0; let savedChunkCount = 0;
const worldsSaveStartTime = Date.now();
await this.worlds.forEach(async (world) => { await this.worlds.forEach(async (world) => {
const worldSaveStartTime = Date.now();
if (world.chunks.length !== 0) { if (world.chunks.length !== 0) {
await world.chunks.forEach(async (chunk) => { await world.chunks.forEach(async (chunk) => {
await world.unloadChunk(Chunk.CreateCoordPair(chunk.x, chunk.z)); await world.unloadChunk(Chunk.CreateCoordPair(chunk.x, chunk.z));
savedChunkCount++; savedChunkCount++;
}); });
} }
Console.printInfo(`Saved DIM${world.dimension} to disk. Took ${Date.now() - worldSaveStartTime}ms`);
savedWorldCount++; savedWorldCount++;
}); });
Console.printInfo(`Saved ${savedChunkCount} chunks from ${savedWorldCount} world(s).`); Console.printInfo(`Saved ${savedChunkCount} chunks from ${savedWorldCount} world(s). Took ${Date.now() - worldsSaveStartTime}ms`);
// Flush final console log to disk and close all writers // Flush final console log to disk and close all writers
Console.cleanup(); Console.cleanup();
// hsconsole is gone now so we have to use built in. // hsconsole is gone now so we have to use built in.
console.log("Goodbye"); console.log("Goodbye");
clearInterval(keepRunningInterval);
// Shut down the tcp server
this.server.close();
}); });
if (this.config.saveCompression === SaveCompressionType.NONE) { if (this.config.saveCompression === SaveCompressionType.NONE) {