fix: implement debounce for config saves + fix duplicate embed issues

This commit is contained in:
Shrev Dev
2025-10-21 11:30:24 -05:00
parent 4ee1442589
commit 35d86f8ec3
3 changed files with 26 additions and 1 deletions
+21
View File
@@ -25,6 +25,7 @@ export class ConfigStorage {
private configPath: string;
private config: MultiGuildConfig;
private logger: Logger;
private saveTimeout: NodeJS.Timeout | null = null;
constructor() {
this.logger = new Logger('ConfigStorage');
@@ -73,12 +74,32 @@ export class ConfigStorage {
}
private save(): void {
// Debounce saves to prevent excessive writes
if (this.saveTimeout) {
clearTimeout(this.saveTimeout);
}
this.saveTimeout = setTimeout(() => {
this.forceSave();
}, 100); // 100ms debounce
}
private forceSave(): void {
try {
writeFileSync(this.configPath, JSON.stringify(this.config, null, 2), 'utf-8');
this.logger.info('Saved configuration to storage');
} catch (error: any) {
this.logger.error(`Failed to save config: ${error.message}`);
}
this.saveTimeout = null;
}
public flush(): void {
// Force immediate save (useful for shutdown)
if (this.saveTimeout) {
clearTimeout(this.saveTimeout);
this.forceSave();
}
}
private getDefaultConfig(): GuildConfig {
+3
View File
@@ -188,6 +188,9 @@ class UptimeKumaDiscordBot {
this.uptimeKuma.disconnect();
this.discord.disconnect();
// Flush any pending config saves
configStorage.flush();
this.logger.info('Shutdown complete');
process.exit(0);
} catch (error: any) {
+2 -1
View File
@@ -362,8 +362,9 @@ export class DiscordService {
}
} catch (error: any) {
this.logger.error(`Failed to update message for guild ${guildId}: ${error.message}`);
// Don't create new messages here - let the main flow handle it
// Just clear the message IDs so next update will create new ones
configStorage.setMessageIds(guildId, []);
await this.createNewMessages(guildId, channel, embeds);
return;
}
}