cleanup incidents
This commit is contained in:
1 parent
0032765bd1
commit
cddf8b32e6
6 files changed
+25
-1
No files matched your search
@@ -205,6 +205,7 @@ export const initializeServices = async ({
|
|||||||
teamsRepository,
|
teamsRepository,
|
||||||
monitorStatsRepository,
|
monitorStatsRepository,
|
||||||
checksRepository,
|
checksRepository,
|
||||||
|
incidentsRepository,
|
||||||
});
|
});
|
||||||
|
|
||||||
const superSimpleQueue = await SuperSimpleQueue.create({
|
const superSimpleQueue = await SuperSimpleQueue.create({
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ import type {
|
|||||||
GotTimings,
|
GotTimings,
|
||||||
MonitorType,
|
MonitorType,
|
||||||
} from "@/types/index.js";
|
} 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";
|
import mongoose from "mongoose";
|
||||||
|
|
||||||
const SERVICE_NAME = "StatusService";
|
const SERVICE_NAME = "StatusService";
|
||||||
|
|||||||
@@ -24,5 +24,6 @@ export interface IIncidentsRepository {
|
|||||||
updateById(incidentId: string, teamId: string, updateData: Partial<Incident>): Promise<Incident>;
|
updateById(incidentId: string, teamId: string, updateData: Partial<Incident>): Promise<Incident>;
|
||||||
// delete
|
// delete
|
||||||
deleteByMonitorId(monitorId: string, teamId: string): Promise<number>;
|
deleteByMonitorId(monitorId: string, teamId: string): Promise<number>;
|
||||||
|
deleteByMonitorIdsNotIn(monitorIds: string[]): Promise<number>;
|
||||||
// other
|
// other
|
||||||
}
|
}
|
||||||
@@ -281,5 +281,11 @@ class MongoIncidentRepository implements IIncidentsRepository {
|
|||||||
});
|
});
|
||||||
return result.deletedCount || 0;
|
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;
|
export default MongoIncidentRepository;
|
||||||
@@ -97,6 +97,7 @@ class SuperSimpleQueue implements ISuperSimpleQueue {
|
|||||||
this.scheduler.start();
|
this.scheduler.start();
|
||||||
|
|
||||||
this.scheduler.addTemplate("monitor-job", this.helper.getMonitorJob());
|
this.scheduler.addTemplate("monitor-job", this.helper.getMonitorJob());
|
||||||
|
this.scheduler.addTemplate("cleanup-orphaned", this.helper.getCleanupOrphanedJob());
|
||||||
const monitors = await this.monitorsRepository.findAll();
|
const monitors = await this.monitorsRepository.findAll();
|
||||||
if (!monitors) {
|
if (!monitors) {
|
||||||
return true;
|
return true;
|
||||||
@@ -108,6 +109,8 @@ class SuperSimpleQueue implements ISuperSimpleQueue {
|
|||||||
}, randomOffset);
|
}, randomOffset);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.scheduler.addJob({ id: "cleanup-orphaned", template: "cleanup-orphaned", active: true });
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.logger.error({
|
this.logger.error({
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import {
|
|||||||
ITeamsRepository,
|
ITeamsRepository,
|
||||||
IMonitorStatsRepository,
|
IMonitorStatsRepository,
|
||||||
IChecksRepository,
|
IChecksRepository,
|
||||||
|
IIncidentsRepository,
|
||||||
} from "@/repositories/index.js";
|
} from "@/repositories/index.js";
|
||||||
|
|
||||||
export interface MonitorActionDecision {
|
export interface MonitorActionDecision {
|
||||||
@@ -41,6 +42,7 @@ class SuperSimpleQueueHelper {
|
|||||||
private teamsRepository: ITeamsRepository;
|
private teamsRepository: ITeamsRepository;
|
||||||
private monitorStatsRepository: IMonitorStatsRepository;
|
private monitorStatsRepository: IMonitorStatsRepository;
|
||||||
private checksRepository: IChecksRepository;
|
private checksRepository: IChecksRepository;
|
||||||
|
private incidentsRepository: IIncidentsRepository;
|
||||||
|
|
||||||
constructor({
|
constructor({
|
||||||
logger,
|
logger,
|
||||||
@@ -55,6 +57,7 @@ class SuperSimpleQueueHelper {
|
|||||||
teamsRepository,
|
teamsRepository,
|
||||||
monitorStatsRepository,
|
monitorStatsRepository,
|
||||||
checksRepository,
|
checksRepository,
|
||||||
|
incidentsRepository,
|
||||||
}: {
|
}: {
|
||||||
logger: any;
|
logger: any;
|
||||||
networkService: INetworkService;
|
networkService: INetworkService;
|
||||||
@@ -68,6 +71,7 @@ class SuperSimpleQueueHelper {
|
|||||||
teamsRepository: ITeamsRepository;
|
teamsRepository: ITeamsRepository;
|
||||||
monitorStatsRepository: IMonitorStatsRepository;
|
monitorStatsRepository: IMonitorStatsRepository;
|
||||||
checksRepository: IChecksRepository;
|
checksRepository: IChecksRepository;
|
||||||
|
incidentsRepository: IIncidentsRepository;
|
||||||
}) {
|
}) {
|
||||||
this.logger = logger;
|
this.logger = logger;
|
||||||
this.networkService = networkService;
|
this.networkService = networkService;
|
||||||
@@ -81,6 +85,7 @@ class SuperSimpleQueueHelper {
|
|||||||
this.teamsRepository = teamsRepository;
|
this.teamsRepository = teamsRepository;
|
||||||
this.monitorStatsRepository = monitorStatsRepository;
|
this.monitorStatsRepository = monitorStatsRepository;
|
||||||
this.checksRepository = checksRepository;
|
this.checksRepository = checksRepository;
|
||||||
|
this.incidentsRepository = incidentsRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
get serviceName() {
|
get serviceName() {
|
||||||
@@ -226,6 +231,14 @@ class SuperSimpleQueueHelper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Remove orphaned incidents
|
// 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({
|
this.logger.info({
|
||||||
message: "Cleanup of orphaned data completed",
|
message: "Cleanup of orphaned data completed",
|
||||||
|
|||||||
Reference in new issue
Block a user