2025-12-05 22:46:46 +00:00
|
|
|
import { IMonitor, ICheck } from "@/db/models/index.js";
|
2025-11-13 13:47:26 -08:00
|
|
|
import { INetworkService } from "./NetworkService.js";
|
|
|
|
|
import { ICheckService } from "../business/CheckService.js";
|
|
|
|
|
import { IMonitorStatsService } from "../business/MonitorStatsService.js";
|
|
|
|
|
import { IStatusService } from "./StatusService.js";
|
|
|
|
|
import { INotificationService } from "./NotificationService.js";
|
|
|
|
|
import { IMaintenanceService } from "../business/MaintenanceService.js";
|
|
|
|
|
import { IIncidentService } from "../business/IncidentService.js";
|
|
|
|
|
import ApiError from "@/utils/ApiError.js";
|
|
|
|
|
import { getChildLogger } from "@/logger/Logger.js";
|
|
|
|
|
|
|
|
|
|
const SERVICE_NAME = "JobGenerator";
|
|
|
|
|
const logger = getChildLogger(SERVICE_NAME);
|
|
|
|
|
export interface IJobGenerator {
|
|
|
|
|
generateJob: () => (Monitor: IMonitor) => Promise<void>;
|
|
|
|
|
generateCleanupJob: () => () => Promise<void>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class JobGenerator implements IJobGenerator {
|
|
|
|
|
public SERVICE_NAME: string;
|
|
|
|
|
private networkService: INetworkService;
|
|
|
|
|
private checkService: ICheckService;
|
|
|
|
|
private monitorStatsService: IMonitorStatsService;
|
|
|
|
|
private statusService: IStatusService;
|
|
|
|
|
private notificationService: INotificationService;
|
|
|
|
|
private maintenanceService: IMaintenanceService;
|
|
|
|
|
private incidentService: IIncidentService;
|
|
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
|
networkService: INetworkService,
|
|
|
|
|
checkService: ICheckService,
|
|
|
|
|
monitorStatsService: IMonitorStatsService,
|
|
|
|
|
statusService: IStatusService,
|
|
|
|
|
notificationService: INotificationService,
|
|
|
|
|
incidentService: IIncidentService,
|
|
|
|
|
maintenanceService: IMaintenanceService
|
|
|
|
|
) {
|
|
|
|
|
this.SERVICE_NAME = SERVICE_NAME;
|
|
|
|
|
this.networkService = networkService;
|
|
|
|
|
this.checkService = checkService;
|
|
|
|
|
this.monitorStatsService = monitorStatsService;
|
|
|
|
|
this.statusService = statusService;
|
|
|
|
|
this.notificationService = notificationService;
|
|
|
|
|
this.incidentService = incidentService;
|
|
|
|
|
this.maintenanceService = maintenanceService;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
generateJob = () => {
|
|
|
|
|
return async (monitor: IMonitor) => {
|
|
|
|
|
try {
|
|
|
|
|
const monitorId = monitor._id.toString();
|
|
|
|
|
if (!monitorId) {
|
|
|
|
|
throw new ApiError("No monitorID for creating job", 400);
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-05 22:46:46 +00:00
|
|
|
// Step 1. Check for active maintenance window, if found, skip the check
|
2025-11-13 13:47:26 -08:00
|
|
|
const isInMaintenance = await this.maintenanceService.isInMaintenance(
|
|
|
|
|
monitorId
|
|
|
|
|
);
|
|
|
|
|
if (isInMaintenance) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-05 22:46:46 +00:00
|
|
|
// Setp 2. Request monitor status
|
2025-11-13 13:47:26 -08:00
|
|
|
const status = await this.networkService.requestStatus(monitor);
|
2025-12-05 22:46:46 +00:00
|
|
|
|
|
|
|
|
// Step 3. Build check
|
2025-11-13 13:47:26 -08:00
|
|
|
const check = await this.checkService.buildCheck(status, monitor.type);
|
|
|
|
|
await check.save();
|
2025-12-05 22:46:46 +00:00
|
|
|
|
|
|
|
|
// Step 4. Update monitor's status
|
2025-11-13 13:47:26 -08:00
|
|
|
const [updatedMonitor, statusChanged] =
|
|
|
|
|
await this.statusService.updateMonitorStatus(monitor, status);
|
|
|
|
|
|
2025-12-05 22:46:46 +00:00
|
|
|
// Step 5. Handle status change
|
2025-11-13 13:47:26 -08:00
|
|
|
if (statusChanged) {
|
|
|
|
|
const incident = await this.incidentService.handleStatusChange(
|
|
|
|
|
updatedMonitor,
|
|
|
|
|
check
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Best effort, don't wait, don't fail
|
|
|
|
|
if (incident) {
|
|
|
|
|
this.notificationService
|
|
|
|
|
.handleNotifications(updatedMonitor, incident)
|
|
|
|
|
.catch((error) => {
|
|
|
|
|
logger.warn(error);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-12-05 22:46:46 +00:00
|
|
|
|
|
|
|
|
// Step 6. Update aggregate monitor stats
|
2025-11-13 13:47:26 -08:00
|
|
|
await this.statusService.updateMonitorStats(
|
|
|
|
|
updatedMonitor,
|
|
|
|
|
status,
|
|
|
|
|
statusChanged
|
|
|
|
|
);
|
2025-12-05 01:13:23 +00:00
|
|
|
|
2025-12-05 22:46:46 +00:00
|
|
|
// Step 7. Evaluate thresholds for Infrastrucutre monitors
|
|
|
|
|
if (updatedMonitor.type === "infrastructure") {
|
|
|
|
|
const evalResult = await this.statusService.evaluateThresholds(
|
|
|
|
|
updatedMonitor,
|
|
|
|
|
check
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const incident = await this.incidentService.handleThresholdBreach(
|
|
|
|
|
updatedMonitor,
|
|
|
|
|
check,
|
|
|
|
|
evalResult
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (incident) {
|
|
|
|
|
this.notificationService
|
|
|
|
|
.handleNotifications(updatedMonitor, incident)
|
|
|
|
|
.catch((error) => {
|
|
|
|
|
logger.warn(error);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-11-13 13:47:26 -08:00
|
|
|
} catch (error) {
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
generateCleanupJob = () => {
|
|
|
|
|
return async () => {
|
|
|
|
|
try {
|
|
|
|
|
await this.checkService.cleanupOrphanedChecks();
|
|
|
|
|
await this.monitorStatsService.cleanupOrphanedMonitorStats();
|
2025-11-21 20:18:34 +00:00
|
|
|
await this.incidentService.cleanupOrphanedIncidents();
|
2025-11-13 13:47:26 -08:00
|
|
|
} catch (error) {
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default JobGenerator;
|