add cleanup behaviour

This commit is contained in:
1e99 2025-07-21 21:23:06 +02:00
parent 022d624315
commit 82104bfd20

View file

@ -42,6 +42,7 @@ try {
const db = new sqlite.DatabaseSync(config["database"])
migrateDB(db)
const reasonProcessExiting = "__processExiting__"
const reconnectDelay = 10_000
const dateFormat = new Intl.DateTimeFormat("en-US", {
@ -50,19 +51,25 @@ const dateFormat = new Intl.DateTimeFormat("en-US", {
timeZone: "UTC",
})
let client = null
newClient()
function newClient() {
console.log("Client connecting")
// Client
const client = mcProtocol.createClient(config["minecraft"])
client = mcProtocol.createClient(config["minecraft"])
client.addListener("connect", () => {
console.log("Client connected")
})
client.addListener("end", reason => {
if (reason === reasonProcessExiting) {
console.log("Client disconnected")
return
}
console.log(`Client disconnected, reconnecting in ${reconnectDelay}ms: ${reason}`)
setTimeout(() => newClient(), reconnectDelay)
})
@ -103,3 +110,14 @@ function newClient() {
commands.register(new TPSCommand(chat, tps))
commands.register(new WorstPingCommand(chat, players))
}
function end() {
console.log("Detected end, quitting")
client.end(reasonProcessExiting)
db.close()
}
process.on("SIGINT", end)
process.on("SIGUSR1", end)
process.on("SIGUSR2", end)
process.on("uncaughtException", end)