feat: implement a dedicated update service to centralize bot update logic, dependency checks, and post-restart handling.

This commit is contained in:
syntaxbullet
2025-12-24 13:38:45 +01:00
parent fcc82292f2
commit fc7afd7d22
4 changed files with 248 additions and 96 deletions

View File

@@ -9,54 +9,9 @@ const event: Event<Events.ClientReady> = {
console.log(`Ready! Logged in as ${c.user.tag}`);
schedulerService.start();
// Check for restart context
const { readFile, unlink } = await import("fs/promises");
const { createSuccessEmbed } = await import("@lib/embeds");
try {
const contextData = await readFile(".restart_context.json", "utf-8");
const context = JSON.parse(contextData);
// Validate context freshness (e.g., ignore if older than 5 minutes)
if (Date.now() - context.timestamp < 5 * 60 * 1000) {
const channel = await c.channels.fetch(context.channelId);
if (channel && channel.isSendable()) {
let migrationOutput = "";
let success = true;
if (context.runMigrations) {
try {
const { exec } = await import("child_process");
const { promisify } = await import("util");
const execAsync = promisify(exec);
// Send intermediate update if possible, though ready event should be fast.
const { stdout: dbOut } = await execAsync("bun run db:push:local");
migrationOutput = dbOut;
} catch (dbErr: any) {
success = false;
migrationOutput = `Migration Failed: ${dbErr.message}`;
console.error("Migration Error:", dbErr);
}
}
if (context.runMigrations) {
await channel.send({
embeds: [createSuccessEmbed(`Bot is back online!\n\n**DB Migration Output:**\n\`\`\`\n${migrationOutput}\n\`\`\``, success ? "Update Successful" : "Update Completed with/Errors")]
});
} else {
await channel.send({
embeds: [createSuccessEmbed("Bot is back online! Redeploy successful.", "System Online")]
});
}
}
}
await unlink(".restart_context.json");
} catch (error) {
// Ignore errors (file not found, etc.)
}
// Handle post-update tasks
const { UpdateService } = await import("@/modules/admin/update.service");
await UpdateService.handlePostRestart(c);
},
};