Files
Checkmate/server/src/service/v1/infrastructure/bufferService.js
T

166 lines
3.9 KiB
JavaScript
Raw Normal View History

2025-04-20 11:29:53 -07:00
const SERVICE_NAME = "BufferService";
class BufferService {
2025-07-25 15:15:14 -07:00
static SERVICE_NAME = SERVICE_NAME;
2025-11-18 16:01:47 -05:00
constructor({ db, logger, envSettings, incidentService }) {
this.BUFFER_TIMEOUT = envSettings.nodeEnv === "development" ? 1000 : 1000 * 60 * 1; // 1 minute
2025-04-20 11:29:53 -07:00
this.db = db;
this.logger = logger;
2025-11-18 16:01:47 -05:00
this.incidentService = incidentService;
2025-04-20 11:29:53 -07:00
this.SERVICE_NAME = SERVICE_NAME;
2025-08-18 10:55:12 -07:00
this.buffer = [];
2025-11-18 16:01:47 -05:00
this.incidentBuffer = [];
2025-04-20 11:29:53 -07:00
this.scheduleNextFlush();
this.logger.info({
2025-07-28 14:37:18 -07:00
message: `Buffer service initialized, flushing every ${this.BUFFER_TIMEOUT / 1000}s`,
2025-04-20 11:29:53 -07:00
service: this.SERVICE_NAME,
method: "constructor",
});
}
2025-07-25 15:15:14 -07:00
get serviceName() {
return BufferService.SERVICE_NAME;
}
2025-04-20 11:29:53 -07:00
2025-08-18 10:55:12 -07:00
addToBuffer({ check }) {
2025-04-20 11:29:53 -07:00
try {
2025-08-18 10:55:12 -07:00
this.buffer.push(check);
2025-04-20 11:29:53 -07:00
} catch (error) {
this.logger.error({
message: error.message,
service: this.SERVICE_NAME,
method: "addToBuffer",
stack: error.stack,
});
}
}
2025-11-18 16:01:47 -05:00
addIncidentToBuffer({ monitor, check, action = "create" }) {
try {
if (!monitor || !check) {
this.logger.warn({
message: "Skipping incident buffer item: missing monitor or check",
service: this.SERVICE_NAME,
method: "addIncidentToBuffer",
});
return;
}
this.incidentBuffer.push({ monitor, check, action });
} catch (error) {
this.logger.error({
message: error.message,
service: this.SERVICE_NAME,
method: "addIncidentToBuffer",
stack: error.stack,
});
}
}
removeCheckFromBuffer(checkToRemove) {
try {
if (!checkToRemove) {
return false;
}
const index = this.buffer.findIndex((check) => {
if (checkToRemove._id && check._id) {
return check._id.toString() === checkToRemove._id.toString();
}
return (
check.monitorId?.toString() === checkToRemove.monitorId?.toString() &&
check.teamId?.toString() === checkToRemove.teamId?.toString() &&
check.type === checkToRemove.type &&
check.status === checkToRemove.status &&
check.statusCode === checkToRemove.statusCode &&
check.responseTime === checkToRemove.responseTime &&
check.message === checkToRemove.message
);
});
if (index !== -1) {
this.buffer.splice(index, 1);
return true;
}
return false;
} catch (error) {
this.logger.error({
message: error.message,
service: this.SERVICE_NAME,
method: "removeCheckFromBuffer",
stack: error.stack,
});
return false;
}
}
2025-04-20 11:29:53 -07:00
scheduleNextFlush() {
this.bufferTimer = setTimeout(async () => {
try {
2025-08-18 10:55:12 -07:00
await this.flushBuffer();
2025-04-20 11:29:53 -07:00
} catch (error) {
this.logger.error({
message: `Error in flush cycle: ${error.message}`,
service: this.SERVICE_NAME,
method: "scheduleNextFlush",
stack: error.stack,
});
} finally {
// Schedule the next flush only after the current one completes
this.scheduleNextFlush();
}
2025-07-28 14:37:18 -07:00
}, this.BUFFER_TIMEOUT);
2025-04-20 11:29:53 -07:00
}
2025-08-18 10:55:12 -07:00
async flushBuffer() {
2025-11-18 16:01:47 -05:00
try {
if (this.buffer.length > 0) {
await this.db.checkModule.createChecks(this.buffer);
}
} catch (error) {
this.logger.error({
message: error.message,
service: this.SERVICE_NAME,
method: "flushBuffer",
stack: error.stack,
});
}
2025-08-18 10:55:12 -07:00
try {
2025-11-18 16:01:47 -05:00
if (this.incidentBuffer.length > 0 && this.incidentService) {
await this.flushIncidentBuffer();
}
2025-08-18 10:55:12 -07:00
} catch (error) {
this.logger.error({
message: error.message,
service: this.SERVICE_NAME,
method: "flushBuffer",
stack: error.stack,
});
2025-04-20 11:29:53 -07:00
}
2025-11-18 16:01:47 -05:00
2025-08-18 10:55:12 -07:00
this.buffer = [];
2025-11-18 16:01:47 -05:00
this.incidentBuffer = [];
}
async flushIncidentBuffer() {
if (!this.incidentService || this.incidentBuffer.length === 0) {
return;
}
try {
const itemsToProcess = [...this.incidentBuffer];
await this.incidentService.processIncidentsFromBuffer(itemsToProcess);
} catch (error) {
this.logger.error({
message: `Error flushing incident buffer: ${error.message}`,
service: this.SERVICE_NAME,
method: "flushIncidentBuffer",
stack: error.stack,
});
throw error;
}
2025-04-20 11:29:53 -07:00
}
}
export default BufferService;