@coretext-ai/qa-discord-77f3255a-cccf-4fab-b131-c7d49ae1be7c
Version:
MCP server with discord integration
81 lines • 2.41 kB
JavaScript
/**
* LogBatcher - Implements the exact batching logic as specified in requirements
* Batches logs and sends them to the centralized logging endpoint
*/
export class LogBatcher {
constructor(logShipper, maxBatchSize = 500, flushInterval = 5000) {
this.logs = [];
this.flushTimer = null;
this.logShipper = logShipper;
this.maxBatchSize = maxBatchSize;
this.flushInterval = flushInterval;
this.sessionId = this.generateSessionId();
// Start the flush timer
this.flushTimer = setInterval(() => this.flush(), this.flushInterval);
}
/**
* Add a structured log entry to the batch
*/
addStructuredLog(logEntry) {
this.logs.push(logEntry);
// Flush immediately if batch size reached
if (this.logs.length >= this.maxBatchSize) {
this.flush();
}
}
/**
* Flush all batched logs
*/
async flush() {
if (this.logs.length === 0)
return;
const batch = this.logs.splice(0, this.maxBatchSize);
try {
// Send each structured log through the LogShipper
for (const logEntry of batch) {
this.logShipper.addLog(logEntry);
}
// Trigger immediate flush in LogShipper
await this.logShipper.flush();
}
catch (error) {
console.error('Failed to send logs:', error);
// Could implement retry logic here
}
}
/**
* Get the session ID for this batcher instance
*/
getSessionId() {
return this.sessionId;
}
/**
* Generate a unique session ID
*/
generateSessionId() {
return `discord-mcp-server-${Date.now()}-${Math.random().toString(36).substring(2, 9)}`;
}
/**
* Get current batch status
*/
getBatchStatus() {
return {
queueSize: this.logs.length,
maxBatchSize: this.maxBatchSize,
flushInterval: this.flushInterval,
sessionId: this.sessionId
};
}
/**
* Shutdown the batcher and flush remaining logs
*/
async shutdown() {
if (this.flushTimer) {
clearInterval(this.flushTimer);
this.flushTimer = null;
}
// Flush any remaining logs
await this.flush();
}
}
//# sourceMappingURL=log-batcher.js.map