Merge pull request #3294 from bluewave-labs/fix/remove-incidents-on-delete-monitor
fix: remove incidents on delete
This commit is contained in:
5 files changed
+49
-1
No files matched your search
@@ -245,6 +245,7 @@ export const initializeServices = async ({
|
|||||||
checksRepository,
|
checksRepository,
|
||||||
monitorStatsRepository,
|
monitorStatsRepository,
|
||||||
statusPagesRepository,
|
statusPagesRepository,
|
||||||
|
incidentsRepository,
|
||||||
});
|
});
|
||||||
|
|
||||||
const statusPageService = new StatusPageService(statusPagesRepository);
|
const statusPageService = new StatusPageService(statusPagesRepository);
|
||||||
|
|||||||
@@ -23,5 +23,6 @@ export interface IIncidentsRepository {
|
|||||||
// update
|
// update
|
||||||
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>;
|
||||||
// other
|
// other
|
||||||
}
|
}
|
||||||
@@ -273,5 +273,13 @@ class MongoIncidentRepository implements IIncidentsRepository {
|
|||||||
})),
|
})),
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
deleteByMonitorId = async (monitorId: string, teamId: string) => {
|
||||||
|
const result = await IncidentModel.deleteMany({
|
||||||
|
monitorId: new mongoose.Types.ObjectId(monitorId),
|
||||||
|
teamId: new mongoose.Types.ObjectId(teamId),
|
||||||
|
});
|
||||||
|
return result.deletedCount || 0;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
export default MongoIncidentRepository;
|
export default MongoIncidentRepository;
|
||||||
@@ -8,7 +8,13 @@ import type {
|
|||||||
PageSpeedDetailsResult,
|
PageSpeedDetailsResult,
|
||||||
GamesMap,
|
GamesMap,
|
||||||
} from "@/types/monitor.js";
|
} from "@/types/monitor.js";
|
||||||
import type { IChecksRepository, IMonitorsRepository, IMonitorStatsRepository, IStatusPagesRepository } from "@/repositories/index.js";
|
import type {
|
||||||
|
IChecksRepository,
|
||||||
|
IIncidentsRepository,
|
||||||
|
IMonitorsRepository,
|
||||||
|
IMonitorStatsRepository,
|
||||||
|
IStatusPagesRepository,
|
||||||
|
} from "@/repositories/index.js";
|
||||||
import fs from "fs";
|
import fs from "fs";
|
||||||
import { fileURLToPath } from "url";
|
import { fileURLToPath } from "url";
|
||||||
import path from "path";
|
import path from "path";
|
||||||
@@ -79,6 +85,7 @@ export class MonitorService implements IMonitorService {
|
|||||||
private checksRepository: IChecksRepository;
|
private checksRepository: IChecksRepository;
|
||||||
private monitorStatsRepository: IMonitorStatsRepository;
|
private monitorStatsRepository: IMonitorStatsRepository;
|
||||||
private statusPagesRepository: IStatusPagesRepository;
|
private statusPagesRepository: IStatusPagesRepository;
|
||||||
|
private incidentsRepository: IIncidentsRepository;
|
||||||
|
|
||||||
constructor({
|
constructor({
|
||||||
jobQueue,
|
jobQueue,
|
||||||
@@ -89,6 +96,7 @@ export class MonitorService implements IMonitorService {
|
|||||||
checksRepository,
|
checksRepository,
|
||||||
monitorStatsRepository,
|
monitorStatsRepository,
|
||||||
statusPagesRepository,
|
statusPagesRepository,
|
||||||
|
incidentsRepository,
|
||||||
}: {
|
}: {
|
||||||
jobQueue: ISuperSimpleQueue;
|
jobQueue: ISuperSimpleQueue;
|
||||||
emailService: any;
|
emailService: any;
|
||||||
@@ -98,6 +106,7 @@ export class MonitorService implements IMonitorService {
|
|||||||
checksRepository: IChecksRepository;
|
checksRepository: IChecksRepository;
|
||||||
monitorStatsRepository: IMonitorStatsRepository;
|
monitorStatsRepository: IMonitorStatsRepository;
|
||||||
statusPagesRepository: IStatusPagesRepository;
|
statusPagesRepository: IStatusPagesRepository;
|
||||||
|
incidentsRepository: IIncidentsRepository;
|
||||||
}) {
|
}) {
|
||||||
this.jobQueue = jobQueue;
|
this.jobQueue = jobQueue;
|
||||||
this.emailService = emailService;
|
this.emailService = emailService;
|
||||||
@@ -107,6 +116,7 @@ export class MonitorService implements IMonitorService {
|
|||||||
this.checksRepository = checksRepository;
|
this.checksRepository = checksRepository;
|
||||||
this.monitorStatsRepository = monitorStatsRepository;
|
this.monitorStatsRepository = monitorStatsRepository;
|
||||||
this.statusPagesRepository = statusPagesRepository;
|
this.statusPagesRepository = statusPagesRepository;
|
||||||
|
this.incidentsRepository = incidentsRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
get serviceName(): string {
|
get serviceName(): string {
|
||||||
@@ -418,6 +428,15 @@ export class MonitorService implements IMonitorService {
|
|||||||
stack: err.stack,
|
stack: err.stack,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
await this.incidentsRepository.deleteByMonitorId(monitor.id, teamId).catch((err: any) => {
|
||||||
|
this.logger.warn({
|
||||||
|
message: `Error deleting incidents for monitor ${monitor.id} with name ${monitor.name}`,
|
||||||
|
service: SERVICE_NAME,
|
||||||
|
stack: err.stack,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
await this.jobQueue.deleteJob(monitor);
|
await this.jobQueue.deleteJob(monitor);
|
||||||
return monitor;
|
return monitor;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -153,6 +153,25 @@ class SuperSimpleQueueHelper {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
getCleanupOrphanedJob = () => {
|
||||||
|
return async () => {
|
||||||
|
try {
|
||||||
|
// Remove orphaned monitors
|
||||||
|
// remove orphaned monitorStats
|
||||||
|
// Remove orphaned checks
|
||||||
|
// Remove orphaned incidents
|
||||||
|
} catch (error: any) {
|
||||||
|
this.logger.warn({
|
||||||
|
message: error.message,
|
||||||
|
service: SERVICE_NAME,
|
||||||
|
method: "getCleanupOrphanedJob",
|
||||||
|
stack: error.stack,
|
||||||
|
});
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
async isInMaintenanceWindow(monitorId: string, teamId: string) {
|
async isInMaintenanceWindow(monitorId: string, teamId: string) {
|
||||||
const maintenanceWindows = await this.maintenanceWindowsRepository.findByMonitorId(monitorId, teamId);
|
const maintenanceWindows = await this.maintenanceWindowsRepository.findByMonitorId(monitorId, teamId);
|
||||||
// Check for active maintenance window:
|
// Check for active maintenance window:
|
||||||
|
|||||||
Reference in new issue
Block a user