discord-optimizer
Version:
Reduce Discord.js bot RAM usage with automatic optimization and restart capabilities
107 lines • 4.19 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MemoryGuard = void 0;
const events_1 = require("events");
const memoryUtils_1 = require("./utils/memoryUtils");
const optimizations_1 = require("./utils/optimizations");
const webhook_1 = require("./utils/webhook");
class MemoryGuard extends events_1.EventEmitter {
constructor(client, options = {}) {
super();
this.isRestarting = false;
this.client = client;
this.options = {
maxMemory: options.maxMemory || 512,
autoRestart: options.autoRestart !== false,
optimizations: {
clearCache: options.optimizations?.clearCache !== false,
garbageCollect: options.optimizations?.garbageCollect !== false,
},
monitoring: {
interval: options.monitoring?.interval || 30000,
logging: options.monitoring?.logging !== false,
webhook: options.monitoring?.webhook || '',
},
};
this.setupMonitoring();
this.attachMethods();
}
static wrap(client, options) {
if (client._memoryGuard) {
console.log('Reusing existing MemoryGuard instance');
return client;
}
const guard = new MemoryGuard(client, options);
client._memoryGuard = guard;
return client;
}
setupMonitoring() {
this.monitoringInterval = setInterval(() => {
this.checkMemoryUsage();
}, this.options.monitoring.interval);
}
async checkMemoryUsage() {
const usage = (0, memoryUtils_1.getMemoryUsage)();
if (this.options.monitoring.logging) {
console.log(`[Discord Optimizer] Memory Usage: ${usage.heapUsed}MB / ${this.options.maxMemory}MB`);
}
if ((0, memoryUtils_1.isMemoryLimitExceeded)(usage.heapUsed, this.options.maxMemory)) {
this.emit('memoryLimitExceeded', usage);
if (this.options.monitoring.webhook) {
await (0, webhook_1.sendWebhookAlert)(this.options.monitoring.webhook, {
type: 'error',
message: 'Memory limit exceeded!',
usage,
maxMemory: this.options.maxMemory,
action: this.options.autoRestart ? 'Restarting bot' : 'Performing cleanup'
});
}
if (this.options.autoRestart && !this.isRestarting) {
this.handleRestart();
}
else {
this.performCleanup();
}
}
else if (usage.heapUsed > this.options.maxMemory * 0.8) {
if (this.options.monitoring.webhook) {
await (0, webhook_1.sendWebhookAlert)(this.options.monitoring.webhook, {
type: 'warning',
message: 'High memory usage detected',
usage,
maxMemory: this.options.maxMemory,
action: 'Monitoring'
});
}
}
}
performCleanup() {
console.log('[Discord Optimizer] Performing memory cleanup...');
(0, optimizations_1.performOptimizations)(this.client, this.options.optimizations);
this.emit('cleanup');
}
async handleRestart() {
if (this.isRestarting)
return;
this.isRestarting = true;
console.log('[Discord Optimizer] Memory limit exceeded, requesting process restart...');
console.log('[Discord Optimizer] Note: Requires process manager (PM2, Docker, etc.) for auto-restart');
this.emit('restart');
await this.client.destroy();
process.exit(1);
}
attachMethods() {
this.client.memoryGuard = {
getMemoryUsage: () => (0, memoryUtils_1.getMemoryUsage)(),
forceCleanup: () => this.performCleanup(),
restart: () => this.handleRestart(),
};
}
destroy() {
if (this.monitoringInterval) {
clearInterval(this.monitoringInterval);
}
}
}
exports.MemoryGuard = MemoryGuard;
//# sourceMappingURL=MemoryGuard.js.map