Files
blitzkit/packages/bot/src/core/blitzkit/processes.ts
T

37 lines
875 B
TypeScript
Raw Normal View History

2026-02-18 16:36:12 -06:00
import { usersDatabase } from "../db/users";
import { client } from "../discord/client";
2023-10-10 20:33:24 -05:00
2026-02-18 16:36:12 -06:00
const EXIT_EVENTS = ["SIGINT", "SIGTERM", "SIGUSR1", "SIGUSR2"];
2023-10-10 20:33:24 -05:00
2026-02-18 16:36:12 -06:00
let cleaningUp = false;
2023-10-10 20:33:24 -05:00
2026-02-18 16:36:12 -06:00
async function cleanup(signal: string) {
if (cleaningUp) return;
cleaningUp = true;
2023-10-10 20:33:24 -05:00
2026-02-18 16:36:12 -06:00
console.log(`Cleaning up... (${signal})`);
try {
await Promise.all([usersDatabase.$disconnect(), client.destroy()]);
} catch (err) {
console.error("Cleanup error:", err);
}
console.log("Gracefully exiting...");
2023-10-10 20:33:24 -05:00
process.exit(0);
}
export function registerProcesses() {
2026-02-18 16:36:12 -06:00
process.on("uncaughtException", (err) => {
console.error("uncaughtException:", err);
});
2023-10-10 20:33:24 -05:00
2026-02-18 16:36:12 -06:00
process.on("unhandledRejection", (err) => {
console.error("unhandledRejection:", err);
});
client.on("error", console.error);
EXIT_EVENTS.forEach((event) => process.on(event, () => cleanup(event)));
2023-10-10 20:33:24 -05:00
}