mirror of
https://github.com/BrenBroZAYT/uptime-kuma-discord-bot.git
synced 2026-06-13 16:40:03 +00:00
fix: implement debounce for config saves + fix duplicate embed issues
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user