incident cleanup

This commit is contained in:
Alex Holliday committed 2025-11-21 20:18:34 +00:00
1 parent 1683c2980e
commit 9bce91c20d
4 files changed
+29 -5

No files matched your search

@@ -1,4 +1,10 @@
import { IIncident, Incident, IMonitor, ICheck } from "@/db/models/index.js";
import {
IIncident,
Incident,
IMonitor,
Monitor,
ICheck,
} from "@/db/models/index.js";
import type { ResolutionType } from "@/db/models/index.js";
import mongoose from "mongoose";
import { getChildLogger } from "@/logger/Logger.js";
@@ -39,10 +45,11 @@ export interface IIncidentService {
teamId: mongoose.Types.ObjectId,
incidentId: mongoose.Types.ObjectId
) => Promise<boolean>;
cleanupOrphanedIncidents: () => Promise<boolean>;
}
const SERVICE_NAME = "IncidentService";
const logger = getChildLogger(SERVICE_NAME);
class IncidentService implements IIncidentService {
public SERVICE_NAME: string;
constructor() {
@@ -199,6 +206,20 @@ class IncidentService implements IIncidentService {
});
return result.deletedCount === 1;
};
async cleanupOrphanedIncidents() {
try {
const monitorIds = await Monitor.find().distinct("_id");
const result = await Incident.deleteMany({
monitorId: { $nin: monitorIds },
});
logger.info(`Deleted ${result.deletedCount} orphaned Incidents.`);
return true;
} catch (error) {
logger.error("Error cleaning up orphaned Incidents:", error);
return false;
}
}
}
export default IncidentService;
@@ -98,6 +98,7 @@ class JobGenerator implements IJobGenerator {
try {
await this.checkService.cleanupOrphanedChecks();
await this.monitorStatsService.cleanupOrphanedMonitorStats();
await this.incidentService.cleanupOrphanedIncidents();
} catch (error) {
throw error;
}