cleanup incidents

This commit is contained in:
Alex Holliday committed 2026-02-17 19:39:24 +00:00
1 parent 0032765bd1
commit cddf8b32e6
6 files changed
+25 -1

No files matched your search

+1
View File
@@ -205,6 +205,7 @@ export const initializeServices = async ({
teamsRepository,
monitorStatsRepository,
checksRepository,
incidentsRepository,
});
const superSimpleQueue = await SuperSimpleQueue.create({
@@ -13,7 +13,7 @@ import type {
GotTimings,
MonitorType,
} from "@/types/index.js";
import { CheckModel, type CheckDocument } from "@/db/models/index.js";
import { CheckModel, MonitorModel, type CheckDocument } from "@/db/models/index.js";
import mongoose from "mongoose";
const SERVICE_NAME = "StatusService";
@@ -24,5 +24,6 @@ export interface IIncidentsRepository {
updateById(incidentId: string, teamId: string, updateData: Partial<Incident>): Promise<Incident>;
// delete
deleteByMonitorId(monitorId: string, teamId: string): Promise<number>;
deleteByMonitorIdsNotIn(monitorIds: string[]): Promise<number>;
// other
}
@@ -281,5 +281,11 @@ class MongoIncidentRepository implements IIncidentsRepository {
});
return result.deletedCount || 0;
};
deleteByMonitorIdsNotIn = async (monitorIds: string[]): Promise<number> => {
const objectIds = monitorIds.map((id) => new mongoose.Types.ObjectId(id));
const result = await IncidentModel.deleteMany({ monitorId: { $nin: objectIds } });
return result.deletedCount ?? 0;
};
}
export default MongoIncidentRepository;
@@ -97,6 +97,7 @@ class SuperSimpleQueue implements ISuperSimpleQueue {
this.scheduler.start();
this.scheduler.addTemplate("monitor-job", this.helper.getMonitorJob());
this.scheduler.addTemplate("cleanup-orphaned", this.helper.getCleanupOrphanedJob());
const monitors = await this.monitorsRepository.findAll();
if (!monitors) {
return true;
@@ -108,6 +109,8 @@ class SuperSimpleQueue implements ISuperSimpleQueue {
}, randomOffset);
}
this.scheduler.addJob({ id: "cleanup-orphaned", template: "cleanup-orphaned", active: true });
return true;
} catch (error) {
this.logger.error({
@@ -10,6 +10,7 @@ import {
ITeamsRepository,
IMonitorStatsRepository,
IChecksRepository,
IIncidentsRepository,
} from "@/repositories/index.js";
export interface MonitorActionDecision {
@@ -41,6 +42,7 @@ class SuperSimpleQueueHelper {
private teamsRepository: ITeamsRepository;
private monitorStatsRepository: IMonitorStatsRepository;
private checksRepository: IChecksRepository;
private incidentsRepository: IIncidentsRepository;
constructor({
logger,
@@ -55,6 +57,7 @@ class SuperSimpleQueueHelper {
teamsRepository,
monitorStatsRepository,
checksRepository,
incidentsRepository,
}: {
logger: any;
networkService: INetworkService;
@@ -68,6 +71,7 @@ class SuperSimpleQueueHelper {
teamsRepository: ITeamsRepository;
monitorStatsRepository: IMonitorStatsRepository;
checksRepository: IChecksRepository;
incidentsRepository: IIncidentsRepository;
}) {
this.logger = logger;
this.networkService = networkService;
@@ -81,6 +85,7 @@ class SuperSimpleQueueHelper {
this.teamsRepository = teamsRepository;
this.monitorStatsRepository = monitorStatsRepository;
this.checksRepository = checksRepository;
this.incidentsRepository = incidentsRepository;
}
get serviceName() {
@@ -226,6 +231,14 @@ class SuperSimpleQueueHelper {
}
// Remove orphaned incidents
const deletedIncidentsCount = await this.incidentsRepository.deleteByMonitorIdsNotIn(allMonitorIds);
if (deletedIncidentsCount > 0) {
this.logger.info({
message: `Deleted ${deletedIncidentsCount} orphaned incidents`,
service: SERVICE_NAME,
method: "getCleanupOrphanedJob",
});
}
this.logger.info({
message: "Cleanup of orphaned data completed",