Merge branch 'develop' into feat/status-page-custom-domain
This commit is contained in:
commit
33f8641f3a
41 files changed
+1265
-1062
No files matched your search
@@ -1,4 +1,4 @@
|
|||||||
import { IJobQueue } from "@/service/job-queues/job-queue.interface.js";
|
import { IWorker } from "@/worker/worker.interface.js";
|
||||||
import { getQueueJobsQueryValidation } from "@/api/validation/queueValidation.js";
|
import { getQueueJobsQueryValidation } from "@/api/validation/queueValidation.js";
|
||||||
import { Request, Response, NextFunction } from "express";
|
import { Request, Response, NextFunction } from "express";
|
||||||
|
|
||||||
@@ -13,9 +13,9 @@ export interface IJobQueueController {
|
|||||||
|
|
||||||
class JobQueueController implements IJobQueueController {
|
class JobQueueController implements IJobQueueController {
|
||||||
static SERVICE_NAME = SERVICE_NAME;
|
static SERVICE_NAME = SERVICE_NAME;
|
||||||
private jobQueue: IJobQueue;
|
private worker: IWorker;
|
||||||
constructor(jobQueue: IJobQueue) {
|
constructor(worker: IWorker) {
|
||||||
this.jobQueue = jobQueue;
|
this.worker = worker;
|
||||||
}
|
}
|
||||||
|
|
||||||
get serviceName() {
|
get serviceName() {
|
||||||
@@ -24,7 +24,7 @@ class JobQueueController implements IJobQueueController {
|
|||||||
|
|
||||||
getMetrics = async (req: Request, res: Response, next: NextFunction) => {
|
getMetrics = async (req: Request, res: Response, next: NextFunction) => {
|
||||||
try {
|
try {
|
||||||
const metrics = await this.jobQueue.getMetrics();
|
const metrics = await this.worker.getMetrics();
|
||||||
res.status(200).json({
|
res.status(200).json({
|
||||||
success: true,
|
success: true,
|
||||||
msg: "Queue metrics fetched successfully",
|
msg: "Queue metrics fetched successfully",
|
||||||
@@ -38,7 +38,7 @@ class JobQueueController implements IJobQueueController {
|
|||||||
getJobs = async (req: Request, res: Response, next: NextFunction) => {
|
getJobs = async (req: Request, res: Response, next: NextFunction) => {
|
||||||
try {
|
try {
|
||||||
const pagination = getQueueJobsQueryValidation.parse(req.query);
|
const pagination = getQueueJobsQueryValidation.parse(req.query);
|
||||||
const { jobs, count } = await this.jobQueue.getJobs(pagination);
|
const { jobs, count } = await this.worker.getJobs(pagination);
|
||||||
return res.status(200).json({
|
return res.status(200).json({
|
||||||
success: true,
|
success: true,
|
||||||
msg: "Queue jobs fetched successfully",
|
msg: "Queue jobs fetched successfully",
|
||||||
@@ -52,8 +52,8 @@ class JobQueueController implements IJobQueueController {
|
|||||||
getAllMetrics = async (req: Request, res: Response, next: NextFunction) => {
|
getAllMetrics = async (req: Request, res: Response, next: NextFunction) => {
|
||||||
try {
|
try {
|
||||||
const pagination = getQueueJobsQueryValidation.parse(req.query);
|
const pagination = getQueueJobsQueryValidation.parse(req.query);
|
||||||
const { jobs, count } = await this.jobQueue.getJobs(pagination);
|
const { jobs, count } = await this.worker.getJobs(pagination);
|
||||||
const metrics = await this.jobQueue.getMetrics();
|
const metrics = await this.worker.getMetrics();
|
||||||
return res.status(200).json({
|
return res.status(200).json({
|
||||||
success: true,
|
success: true,
|
||||||
msg: "Queue metrics fetched successfully",
|
msg: "Queue metrics fetched successfully",
|
||||||
@@ -66,7 +66,7 @@ class JobQueueController implements IJobQueueController {
|
|||||||
|
|
||||||
flushQueue = async (req: Request, res: Response, next: NextFunction) => {
|
flushQueue = async (req: Request, res: Response, next: NextFunction) => {
|
||||||
try {
|
try {
|
||||||
const result = await this.jobQueue.flushQueues();
|
const result = await this.worker.flushQueues();
|
||||||
return res.status(200).json({
|
return res.status(200).json({
|
||||||
success: true,
|
success: true,
|
||||||
msg: "Queue flushed successfully",
|
msg: "Queue flushed successfully",
|
||||||
|
|||||||
@@ -1,11 +1,16 @@
|
|||||||
import type { NextFunction, Request, Response } from "express";
|
import type { NextFunction, Request, Response } from "express";
|
||||||
|
import { MulterError } from "multer";
|
||||||
import { logger } from "@/utils/logger.js";
|
import { logger } from "@/utils/logger.js";
|
||||||
import { AppError } from "@/utils/AppError.js";
|
import { AppError } from "@/utils/AppError.js";
|
||||||
|
|
||||||
|
// Multer throws MulterError, send 413 Payload Too Large / 400
|
||||||
|
const multerErrorStatus = (error: MulterError): number => (error.code === "LIMIT_FILE_SIZE" ? 413 : 400);
|
||||||
|
|
||||||
const handleErrors = (error: unknown, req: Request, res: Response, _next: NextFunction) => {
|
const handleErrors = (error: unknown, req: Request, res: Response, _next: NextFunction) => {
|
||||||
const status = error instanceof AppError ? error.status || 500 : 500;
|
const status =
|
||||||
|
error instanceof MulterError ? multerErrorStatus(error) : error instanceof AppError ? error.status || 500 : 500;
|
||||||
const message = error instanceof AppError ? error.message : error instanceof Error ? error.message : "Server error";
|
const message = error instanceof AppError ? error.message : error instanceof Error ? error.message : "Server error";
|
||||||
const service = error instanceof AppError ? error.service : "unknownService";
|
const service = error instanceof AppError ? error.service : error instanceof MulterError ? "uploadMiddleware" : "unknownService";
|
||||||
const method = error instanceof AppError ? error.method : "unknownMethod";
|
const method = error instanceof AppError ? error.method : "unknownMethod";
|
||||||
logger.error({
|
logger.error({
|
||||||
message: message,
|
message: message,
|
||||||
|
|||||||
@@ -0,0 +1,27 @@
|
|||||||
|
import multer from "multer";
|
||||||
|
import { AppError } from "@/utils/AppError.js";
|
||||||
|
import { ImageMimeTypes, MAX_IMAGE_SIZE_BYTES } from "@/types/upload.js";
|
||||||
|
|
||||||
|
// Reusable multer instance for handling image uploads
|
||||||
|
const imageUpload = multer({
|
||||||
|
limits: {
|
||||||
|
fileSize: MAX_IMAGE_SIZE_BYTES,
|
||||||
|
files: 1,
|
||||||
|
},
|
||||||
|
fileFilter: (_req, file, cb) => {
|
||||||
|
if (!(ImageMimeTypes as readonly string[]).includes(file.mimetype)) {
|
||||||
|
cb(
|
||||||
|
new AppError({
|
||||||
|
status: 415,
|
||||||
|
message: "File must be a valid image (jpeg, jpg, or png)",
|
||||||
|
service: "uploadMiddleware",
|
||||||
|
method: "fileFilter",
|
||||||
|
})
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
cb(null, true);
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
export { imageUpload };
|
||||||
@@ -1,10 +1,8 @@
|
|||||||
import { Router, RequestHandler } from "express";
|
import { Router, RequestHandler } from "express";
|
||||||
import { isAllowed } from "../middleware/isAllowed.js";
|
import { isAllowed } from "@/api/middleware/isAllowed.js";
|
||||||
import multer from "multer";
|
import { imageUpload } from "@/api/middleware/upload.js";
|
||||||
import { IAuthController } from "@/api/controllers/authController.js";
|
import { IAuthController } from "@/api/controllers/authController.js";
|
||||||
|
|
||||||
const upload = multer();
|
|
||||||
|
|
||||||
class AuthRoutes {
|
class AuthRoutes {
|
||||||
private router: Router;
|
private router: Router;
|
||||||
private authController: IAuthController;
|
private authController: IAuthController;
|
||||||
@@ -16,7 +14,7 @@ class AuthRoutes {
|
|||||||
}
|
}
|
||||||
|
|
||||||
initRoutes(verifyJWT: RequestHandler) {
|
initRoutes(verifyJWT: RequestHandler) {
|
||||||
this.router.post("/register", upload.single("profileImage"), this.authController.registerUser);
|
this.router.post("/register", imageUpload.single("profileImage"), this.authController.registerUser);
|
||||||
this.router.post("/login", this.authController.loginUser);
|
this.router.post("/login", this.authController.loginUser);
|
||||||
|
|
||||||
this.router.post("/recovery/request", this.authController.requestRecovery);
|
this.router.post("/recovery/request", this.authController.requestRecovery);
|
||||||
@@ -26,13 +24,13 @@ class AuthRoutes {
|
|||||||
this.router.get("/users/superadmin", this.authController.checkSuperadminExists);
|
this.router.get("/users/superadmin", this.authController.checkSuperadminExists);
|
||||||
|
|
||||||
this.router.get("/users", verifyJWT, isAllowed(["admin", "superadmin"]), this.authController.getAllUsers);
|
this.router.get("/users", verifyJWT, isAllowed(["admin", "superadmin"]), this.authController.getAllUsers);
|
||||||
this.router.post("/users", verifyJWT, isAllowed(["superadmin"]), upload.single("profileImage"), this.authController.createUser);
|
this.router.post("/users", verifyJWT, isAllowed(["superadmin"]), imageUpload.single("profileImage"), this.authController.createUser);
|
||||||
this.router.get("/users/:userId", verifyJWT, isAllowed(["admin", "superadmin"]), this.authController.getUserById);
|
this.router.get("/users/:userId", verifyJWT, isAllowed(["admin", "superadmin"]), this.authController.getUserById);
|
||||||
this.router.patch("/users/:userId", verifyJWT, isAllowed(["superadmin"]), this.authController.editUserById);
|
this.router.patch("/users/:userId", verifyJWT, isAllowed(["superadmin"]), this.authController.editUserById);
|
||||||
this.router.patch("/users/:userId/password", verifyJWT, isAllowed(["superadmin"]), this.authController.editUserPasswordById);
|
this.router.patch("/users/:userId/password", verifyJWT, isAllowed(["superadmin"]), this.authController.editUserPasswordById);
|
||||||
this.router.delete("/users/:userId", verifyJWT, isAllowed(["admin", "superadmin"]), this.authController.deleteUserById);
|
this.router.delete("/users/:userId", verifyJWT, isAllowed(["admin", "superadmin"]), this.authController.deleteUserById);
|
||||||
|
|
||||||
this.router.patch("/user", verifyJWT, upload.single("profileImage"), isAllowed(["admin", "superadmin", "user"]), this.authController.editUser);
|
this.router.patch("/user", verifyJWT, imageUpload.single("profileImage"), isAllowed(["admin", "superadmin", "user"]), this.authController.editUser);
|
||||||
this.router.delete("/user", verifyJWT, isAllowed(["admin", "superadmin", "user"]), this.authController.deleteUser);
|
this.router.delete("/user", verifyJWT, isAllowed(["admin", "superadmin", "user"]), this.authController.deleteUser);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
import { IStatusPageController } from "@/api/controllers/statusPageController.js";
|
import { IStatusPageController } from "@/api/controllers/statusPageController.js";
|
||||||
import { RequestHandler, Router } from "express";
|
import { RequestHandler, Router } from "express";
|
||||||
import multer from "multer";
|
import { imageUpload } from "@/api/middleware/upload.js";
|
||||||
const upload = multer();
|
|
||||||
|
|
||||||
class StatusPageRoutes {
|
class StatusPageRoutes {
|
||||||
private router: Router;
|
private router: Router;
|
||||||
@@ -16,8 +15,8 @@ class StatusPageRoutes {
|
|||||||
initRoutes(verifyJWT: RequestHandler, verifyStatusPageAccess: RequestHandler) {
|
initRoutes(verifyJWT: RequestHandler, verifyStatusPageAccess: RequestHandler) {
|
||||||
this.router.get("/team", verifyJWT, this.statusPageController.getStatusPagesByTeamId);
|
this.router.get("/team", verifyJWT, this.statusPageController.getStatusPagesByTeamId);
|
||||||
|
|
||||||
this.router.post("/", upload.single("logo"), verifyJWT, this.statusPageController.createStatusPage);
|
this.router.post("/", imageUpload.single("logo"), verifyJWT, this.statusPageController.createStatusPage);
|
||||||
this.router.put("/:id", upload.single("logo"), verifyJWT, this.statusPageController.updateStatusPage);
|
this.router.put("/:id", imageUpload.single("logo"), verifyJWT, this.statusPageController.updateStatusPage);
|
||||||
|
|
||||||
this.router.get("/resolve", this.statusPageController.resolveStatusPageByDomain);
|
this.router.get("/resolve", this.statusPageController.resolveStatusPageByDomain);
|
||||||
this.router.get("/:url", verifyStatusPageAccess, this.statusPageController.getStatusPageByUrl);
|
this.router.get("/:url", verifyStatusPageAccess, this.statusPageController.getStatusPageByUrl);
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ export const initializeControllers = (services: InitializedServices): Initialize
|
|||||||
geoCheckController: new GeoCheckController(services.geoChecksService),
|
geoCheckController: new GeoCheckController(services.geoChecksService),
|
||||||
inviteController: new InviteController(services.inviteService),
|
inviteController: new InviteController(services.inviteService),
|
||||||
maintenanceWindowController: new MaintenanceWindowController(services.maintenanceWindowService),
|
maintenanceWindowController: new MaintenanceWindowController(services.maintenanceWindowService),
|
||||||
queueController: new QueueController(services.jobQueue),
|
queueController: new QueueController(services.worker),
|
||||||
logController: new LogController(services.logger),
|
logController: new LogController(services.logger),
|
||||||
statusPageController: new StatusPageController(services.statusPageService, services.monitorsRepository, services.settingsService),
|
statusPageController: new StatusPageController(services.statusPageService, services.monitorsRepository, services.settingsService),
|
||||||
notificationController: new NotificationController(services.notificationsService, services.monitorsRepository),
|
notificationController: new NotificationController(services.notificationsService, services.monitorsRepository),
|
||||||
|
|||||||
@@ -28,12 +28,13 @@ import { IUserService, UserService } from "@/domain/users/user.service.js";
|
|||||||
import { BufferService, IBufferService } from "@/service/bufferService.js";
|
import { BufferService, IBufferService } from "@/service/bufferService.js";
|
||||||
import { EmailService, IEmailService } from "@/service/emailService.js";
|
import { EmailService, IEmailService } from "@/service/emailService.js";
|
||||||
import { GlobalPingService } from "@/service/globalPingService.js";
|
import { GlobalPingService } from "@/service/globalPingService.js";
|
||||||
import { QueueHelper } from "@/service/job-queues/job-queue.helper.js";
|
import { WorkerHelper } from "@/worker/worker.helper.js";
|
||||||
import { IJobQueue } from "@/service/job-queues/job-queue.interface.js";
|
import { IWorker } from "@/worker/worker.interface.js";
|
||||||
import { LessSimpleQueue } from "@/service/job-queues/job-queue.less-simple.js";
|
import { LessSimpleWorker } from "@/worker/worker.less-simple.js";
|
||||||
import { SuperSimpleQueue } from "@/service/job-queues/job-queue.sumper-simple.js";
|
import { SuperSimpleQueue } from "@/worker/worker.super-simple.js";
|
||||||
import { INetworkService, NetworkService } from "@/service/networkService.js";
|
import { INetworkService, NetworkService } from "@/service/networkService.js";
|
||||||
import { IStatusService, StatusService } from "@/service/statusService.js";
|
import { IStatusService, StatusService } from "@/service/statusService.js";
|
||||||
|
import { MonitorStatusPolicy } from "@/worker/worker.monitor-status-policy.js";
|
||||||
|
|
||||||
// Network providers
|
// Network providers
|
||||||
import { PingProvider } from "@/service/network/PingProvider.js";
|
import { PingProvider } from "@/service/network/PingProvider.js";
|
||||||
@@ -102,6 +103,10 @@ import MongoUsersRepository from "@/domain/users/user.repository.mongo.js";
|
|||||||
import { ILogger } from "@/utils/logger.js";
|
import { ILogger } from "@/utils/logger.js";
|
||||||
import { AppError } from "@/utils/AppError.js";
|
import { AppError } from "@/utils/AppError.js";
|
||||||
import { type QueueMode } from "@/domain/app-settings/app-settings.type.js";
|
import { type QueueMode } from "@/domain/app-settings/app-settings.type.js";
|
||||||
|
import { NotificationReactor } from "@/worker/reactors/reactor.notification.js";
|
||||||
|
import { ReactorDispatcher } from "@/worker/reactors/reactor.dispatcher.js";
|
||||||
|
import { IncidentReactor } from "@/worker/reactors/reactor.incident.js";
|
||||||
|
import { CheckPipeline, GeoChecksPipeline } from "@/worker/worker.check-pipeline.js";
|
||||||
|
|
||||||
export type InitializedServices = {
|
export type InitializedServices = {
|
||||||
settingsService: ISettingsService;
|
settingsService: ISettingsService;
|
||||||
@@ -110,7 +115,7 @@ export type InitializedServices = {
|
|||||||
emailService: IEmailService;
|
emailService: IEmailService;
|
||||||
bufferService: IBufferService;
|
bufferService: IBufferService;
|
||||||
statusService: IStatusService;
|
statusService: IStatusService;
|
||||||
jobQueue: IJobQueue;
|
worker: IWorker;
|
||||||
userService: IUserService;
|
userService: IUserService;
|
||||||
checkService: ICheckService;
|
checkService: ICheckService;
|
||||||
geoChecksService: IGeoChecksService;
|
geoChecksService: IGeoChecksService;
|
||||||
@@ -309,23 +314,39 @@ export const initializeServices = async ({
|
|||||||
|
|
||||||
const tagsService = new TagsService(tagsRepository, monitorsRepository);
|
const tagsService = new TagsService(tagsRepository, monitorsRepository);
|
||||||
|
|
||||||
const queueHelper = new QueueHelper(
|
const monitorStatusPolicy = new MonitorStatusPolicy();
|
||||||
logger,
|
// reactors and dispatcher
|
||||||
|
const notificationReactor = new NotificationReactor(notificationsService);
|
||||||
|
const incidentReactor = new IncidentReactor(incidentService);
|
||||||
|
const reactorDispatcher = new ReactorDispatcher(logger, [notificationReactor, incidentReactor]);
|
||||||
|
|
||||||
|
// pipelines
|
||||||
|
const checkPipeline = new CheckPipeline(
|
||||||
|
monitorsRepository,
|
||||||
|
maintenanceWindowsRepository,
|
||||||
|
checkService,
|
||||||
networkService,
|
networkService,
|
||||||
|
bufferService,
|
||||||
|
monitorStatusPolicy,
|
||||||
statusService,
|
statusService,
|
||||||
notificationsService,
|
logger
|
||||||
|
);
|
||||||
|
|
||||||
|
const geoCheckPipeline = new GeoChecksPipeline(maintenanceWindowsRepository, geoChecksService, bufferService, logger);
|
||||||
|
|
||||||
|
const workerHelper = new WorkerHelper(
|
||||||
|
logger,
|
||||||
checkService,
|
checkService,
|
||||||
settingsService,
|
settingsService,
|
||||||
bufferService,
|
|
||||||
incidentService,
|
|
||||||
maintenanceWindowsRepository,
|
|
||||||
monitorsRepository,
|
monitorsRepository,
|
||||||
teamsRepository,
|
teamsRepository,
|
||||||
monitorStatsRepository,
|
monitorStatsRepository,
|
||||||
checksRepository,
|
checksRepository,
|
||||||
incidentsRepository,
|
incidentsRepository,
|
||||||
geoChecksService,
|
geoChecksRepository,
|
||||||
geoChecksRepository
|
reactorDispatcher,
|
||||||
|
checkPipeline,
|
||||||
|
geoCheckPipeline
|
||||||
);
|
);
|
||||||
|
|
||||||
if (queueMode === "worker" && envSettings.queueType !== "lessSimpleQueue") {
|
if (queueMode === "worker" && envSettings.queueType !== "lessSimpleQueue") {
|
||||||
@@ -337,13 +358,13 @@ export const initializeServices = async ({
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
let jobQueue: IJobQueue;
|
let worker: IWorker;
|
||||||
switch (envSettings.queueType) {
|
switch (envSettings.queueType) {
|
||||||
case "lessSimpleQueue":
|
case "lessSimpleQueue":
|
||||||
jobQueue = await LessSimpleQueue.create(logger, queueHelper, monitorsRepository, queueWorkersRepository, envSettings, queueMode);
|
worker = await LessSimpleWorker.create(logger, workerHelper, monitorsRepository, queueWorkersRepository, envSettings, queueMode);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
jobQueue = await SuperSimpleQueue.create(logger, queueHelper, monitorsRepository);
|
worker = await SuperSimpleQueue.create(logger, workerHelper, monitorsRepository);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Business services
|
// Business services
|
||||||
@@ -353,7 +374,7 @@ export const initializeServices = async ({
|
|||||||
settingsService,
|
settingsService,
|
||||||
logger,
|
logger,
|
||||||
jwt,
|
jwt,
|
||||||
jobQueue,
|
worker,
|
||||||
monitorsRepository,
|
monitorsRepository,
|
||||||
usersRepository,
|
usersRepository,
|
||||||
invitesRepository,
|
invitesRepository,
|
||||||
@@ -373,7 +394,7 @@ export const initializeServices = async ({
|
|||||||
maintenanceWindowsRepository,
|
maintenanceWindowsRepository,
|
||||||
});
|
});
|
||||||
const monitorService = new MonitorService({
|
const monitorService = new MonitorService({
|
||||||
jobQueue,
|
worker,
|
||||||
logger,
|
logger,
|
||||||
games,
|
games,
|
||||||
monitorsRepository,
|
monitorsRepository,
|
||||||
@@ -393,7 +414,7 @@ export const initializeServices = async ({
|
|||||||
emailService,
|
emailService,
|
||||||
bufferService,
|
bufferService,
|
||||||
statusService,
|
statusService,
|
||||||
jobQueue,
|
worker,
|
||||||
userService,
|
userService,
|
||||||
checkService,
|
checkService,
|
||||||
geoChecksService,
|
geoChecksService,
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import type { IMonitorsRepository } from "@/domain/monitors/monitor.repository.i
|
|||||||
import type { IUsersRepository } from "@/domain/users/user.repository.interface.js";
|
import type { IUsersRepository } from "@/domain/users/user.repository.interface.js";
|
||||||
import type { Incident, IncidentSummary } from "@/domain/incidents/incident.type.js";
|
import type { Incident, IncidentSummary } from "@/domain/incidents/incident.type.js";
|
||||||
import type { User } from "@/domain/users/user.type.js";
|
import type { User } from "@/domain/users/user.type.js";
|
||||||
import type { MonitorActionDecision } from "@/service/job-queues/job-queue.helper.js";
|
import type { MonitorActionDecision } from "@/worker/worker.helper.js";
|
||||||
import type { INotificationMessageBuilder } from "@/domain/notifications/notification.message-builder.js";
|
import type { INotificationMessageBuilder } from "@/domain/notifications/notification.message-builder.js";
|
||||||
import type { ILogger } from "@/utils/logger.js";
|
import type { ILogger } from "@/utils/logger.js";
|
||||||
|
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ import type { IStatusPagesRepository } from "@/domain/status-pages/status-page-r
|
|||||||
import demoMonitorsData from "@/utils/demoMonitors.json" with { type: "json" };
|
import demoMonitorsData from "@/utils/demoMonitors.json" with { type: "json" };
|
||||||
import { AppError } from "@/utils/AppError.js";
|
import { AppError } from "@/utils/AppError.js";
|
||||||
import type { ImportedMonitor } from "@/api/validation/monitorValidation.js";
|
import type { ImportedMonitor } from "@/api/validation/monitorValidation.js";
|
||||||
import { IJobQueue } from "@/service/job-queues/job-queue.interface.js";
|
import { IWorker } from "@/worker/worker.interface.js";
|
||||||
import { ILogger } from "@/utils/logger.js";
|
import { ILogger } from "@/utils/logger.js";
|
||||||
|
|
||||||
const SERVICE_NAME = "MonitorService";
|
const SERVICE_NAME = "MonitorService";
|
||||||
@@ -94,7 +94,7 @@ export interface IMonitorService {
|
|||||||
export class MonitorService implements IMonitorService {
|
export class MonitorService implements IMonitorService {
|
||||||
static SERVICE_NAME = SERVICE_NAME;
|
static SERVICE_NAME = SERVICE_NAME;
|
||||||
|
|
||||||
private jobQueue: IJobQueue;
|
private worker: IWorker;
|
||||||
private logger: ILogger;
|
private logger: ILogger;
|
||||||
private games: GamesMap;
|
private games: GamesMap;
|
||||||
private monitorsRepository: IMonitorsRepository;
|
private monitorsRepository: IMonitorsRepository;
|
||||||
@@ -105,7 +105,7 @@ export class MonitorService implements IMonitorService {
|
|||||||
private incidentsRepository: IIncidentsRepository;
|
private incidentsRepository: IIncidentsRepository;
|
||||||
|
|
||||||
constructor({
|
constructor({
|
||||||
jobQueue,
|
worker,
|
||||||
logger,
|
logger,
|
||||||
games,
|
games,
|
||||||
monitorsRepository,
|
monitorsRepository,
|
||||||
@@ -115,7 +115,7 @@ export class MonitorService implements IMonitorService {
|
|||||||
statusPagesRepository,
|
statusPagesRepository,
|
||||||
incidentsRepository,
|
incidentsRepository,
|
||||||
}: {
|
}: {
|
||||||
jobQueue: IJobQueue;
|
worker: IWorker;
|
||||||
logger: ILogger;
|
logger: ILogger;
|
||||||
games: GamesMap;
|
games: GamesMap;
|
||||||
monitorsRepository: IMonitorsRepository;
|
monitorsRepository: IMonitorsRepository;
|
||||||
@@ -125,7 +125,7 @@ export class MonitorService implements IMonitorService {
|
|||||||
statusPagesRepository: IStatusPagesRepository;
|
statusPagesRepository: IStatusPagesRepository;
|
||||||
incidentsRepository: IIncidentsRepository;
|
incidentsRepository: IIncidentsRepository;
|
||||||
}) {
|
}) {
|
||||||
this.jobQueue = jobQueue;
|
this.worker = worker;
|
||||||
this.logger = logger;
|
this.logger = logger;
|
||||||
this.games = games;
|
this.games = games;
|
||||||
this.monitorsRepository = monitorsRepository;
|
this.monitorsRepository = monitorsRepository;
|
||||||
@@ -171,7 +171,7 @@ export class MonitorService implements IMonitorService {
|
|||||||
throw new AppError({ message: "Failed to create monitor", status: 500, service: SERVICE_NAME, method: "createMonitor" });
|
throw new AppError({ message: "Failed to create monitor", status: 500, service: SERVICE_NAME, method: "createMonitor" });
|
||||||
}
|
}
|
||||||
|
|
||||||
this.jobQueue.addJob(monitor.id, monitor);
|
this.worker.addJob(monitor.id, monitor);
|
||||||
};
|
};
|
||||||
|
|
||||||
createMonitors = async (monitors: Array<Monitor>): Promise<Monitor[] | null> => {
|
createMonitors = async (monitors: Array<Monitor>): Promise<Monitor[] | null> => {
|
||||||
@@ -180,7 +180,7 @@ export class MonitorService implements IMonitorService {
|
|||||||
throw new AppError({ message: "Failed to create monitors", status: 500, service: SERVICE_NAME, method: "createMonitors" });
|
throw new AppError({ message: "Failed to create monitors", status: 500, service: SERVICE_NAME, method: "createMonitors" });
|
||||||
}
|
}
|
||||||
|
|
||||||
await Promise.all(createdMonitors.map((monitor) => this.jobQueue.addJob(monitor.id, monitor)));
|
await Promise.all(createdMonitors.map((monitor) => this.worker.addJob(monitor.id, monitor)));
|
||||||
return createdMonitors;
|
return createdMonitors;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -196,7 +196,7 @@ export class MonitorService implements IMonitorService {
|
|||||||
}));
|
}));
|
||||||
const demoMonitors = await this.monitorsRepository.createMonitors(monitors as unknown as Monitor[]);
|
const demoMonitors = await this.monitorsRepository.createMonitors(monitors as unknown as Monitor[]);
|
||||||
|
|
||||||
await Promise.all(demoMonitors.map((monitor) => this.jobQueue.addJob(monitor.id, monitor)));
|
await Promise.all(demoMonitors.map((monitor) => this.worker.addJob(monitor.id, monitor)));
|
||||||
return demoMonitors;
|
return demoMonitors;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -428,7 +428,7 @@ export class MonitorService implements IMonitorService {
|
|||||||
|
|
||||||
editMonitor = async ({ teamId, monitorId, body }: { teamId: string; monitorId: string; body: Partial<Monitor> }) => {
|
editMonitor = async ({ teamId, monitorId, body }: { teamId: string; monitorId: string; body: Partial<Monitor> }) => {
|
||||||
const editedMonitor = await this.monitorsRepository.updateById(monitorId, teamId, body);
|
const editedMonitor = await this.monitorsRepository.updateById(monitorId, teamId, body);
|
||||||
await this.jobQueue.updateJob(editedMonitor);
|
await this.worker.updateJob(editedMonitor);
|
||||||
return editedMonitor;
|
return editedMonitor;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -448,7 +448,7 @@ export class MonitorService implements IMonitorService {
|
|||||||
// If notifications were updated, we should update the jobs in the queue
|
// If notifications were updated, we should update the jobs in the queue
|
||||||
if (modifiedCount > 0) {
|
if (modifiedCount > 0) {
|
||||||
const monitors = await this.monitorsRepository.findByIds(monitorIds);
|
const monitors = await this.monitorsRepository.findByIds(monitorIds);
|
||||||
await Promise.all(monitors.map((monitor) => this.jobQueue.updateJob(monitor)));
|
await Promise.all(monitors.map((monitor) => this.worker.updateJob(monitor)));
|
||||||
}
|
}
|
||||||
|
|
||||||
return modifiedCount;
|
return modifiedCount;
|
||||||
@@ -457,9 +457,9 @@ export class MonitorService implements IMonitorService {
|
|||||||
pauseMonitor = async ({ teamId, monitorId }: { teamId: string; monitorId: string }): Promise<Monitor> => {
|
pauseMonitor = async ({ teamId, monitorId }: { teamId: string; monitorId: string }): Promise<Monitor> => {
|
||||||
const monitor = await this.monitorsRepository.togglePauseById(monitorId, teamId);
|
const monitor = await this.monitorsRepository.togglePauseById(monitorId, teamId);
|
||||||
if (monitor.isActive) {
|
if (monitor.isActive) {
|
||||||
await this.jobQueue.resumeJob(monitor);
|
await this.worker.resumeJob(monitor);
|
||||||
} else {
|
} else {
|
||||||
await this.jobQueue.pauseJob(monitor);
|
await this.worker.pauseJob(monitor);
|
||||||
}
|
}
|
||||||
return monitor;
|
return monitor;
|
||||||
};
|
};
|
||||||
@@ -478,9 +478,9 @@ export class MonitorService implements IMonitorService {
|
|||||||
const results = await Promise.allSettled(
|
const results = await Promise.allSettled(
|
||||||
monitors.map(async (monitor) => {
|
monitors.map(async (monitor) => {
|
||||||
if (monitor.isActive) {
|
if (monitor.isActive) {
|
||||||
await this.jobQueue.resumeJob(monitor);
|
await this.worker.resumeJob(monitor);
|
||||||
} else {
|
} else {
|
||||||
await this.jobQueue.pauseJob(monitor);
|
await this.worker.pauseJob(monitor);
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
@@ -541,7 +541,7 @@ export class MonitorService implements IMonitorService {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
await this.jobQueue.deleteJob(monitor);
|
await this.worker.deleteJob(monitor);
|
||||||
return monitor;
|
return monitor;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -550,7 +550,7 @@ export class MonitorService implements IMonitorService {
|
|||||||
await Promise.all(
|
await Promise.all(
|
||||||
monitors.map(async (monitor) => {
|
monitors.map(async (monitor) => {
|
||||||
try {
|
try {
|
||||||
await this.jobQueue.deleteJob(monitor);
|
await this.worker.deleteJob(monitor);
|
||||||
await this.checksRepository.deleteByMonitorId(monitor.id);
|
await this.checksRepository.deleteByMonitorId(monitor.id);
|
||||||
await this.geoChecksRepository.deleteByMonitorId(monitor.id);
|
await this.geoChecksRepository.deleteByMonitorId(monitor.id);
|
||||||
await this.statusPagesRepository.removeMonitorFromStatusPages(monitor.id);
|
await this.statusPagesRepository.removeMonitorFromStatusPages(monitor.id);
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import type { Monitor } from "@/domain/monitors/monitor.types.js";
|
import type { Monitor } from "@/domain/monitors/monitor.types.js";
|
||||||
import type { HardwareStatusPayload, MonitorStatusResponse } from "@/types/network.js";
|
import type { HardwareStatusPayload, MonitorStatusResponse } from "@/types/network.js";
|
||||||
import type { MonitorActionDecision } from "@/service/job-queues/job-queue.helper.js";
|
import type { MonitorActionDecision } from "@/worker/worker.helper.js";
|
||||||
import type {
|
import type {
|
||||||
NotificationMessage,
|
NotificationMessage,
|
||||||
NotificationType,
|
NotificationType,
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import type { NotificationMessage } from "@/domain/notifications/notification.ty
|
|||||||
import { IMonitorsRepository } from "@/domain/monitors/monitor.repository.interface.js";
|
import { IMonitorsRepository } from "@/domain/monitors/monitor.repository.interface.js";
|
||||||
import { INotificationsRepository } from "@/domain/notifications/notification.repository.interface.js";
|
import { INotificationsRepository } from "@/domain/notifications/notification.repository.interface.js";
|
||||||
import { INotificationProvider } from "@/domain/notifications/providers/INotificationProvider.js";
|
import { INotificationProvider } from "@/domain/notifications/providers/INotificationProvider.js";
|
||||||
import type { MonitorActionDecision } from "@/service/job-queues/job-queue.helper.js";
|
import type { MonitorActionDecision } from "@/worker/worker.helper.js";
|
||||||
import type { ISettingsService } from "@/domain/app-settings/app-settings.service.js";
|
import type { ISettingsService } from "@/domain/app-settings/app-settings.service.js";
|
||||||
import { ILogger } from "@/utils/logger.js";
|
import { ILogger } from "@/utils/logger.js";
|
||||||
import type { INotificationMessageBuilder } from "@/domain/notifications/notification.message-builder.js";
|
import type { INotificationMessageBuilder } from "@/domain/notifications/notification.message-builder.js";
|
||||||
|
|||||||
@@ -8,12 +8,12 @@ import type { User } from "@/domain/users/user.type.js";
|
|||||||
import { canManageRole, type UserRole } from "@/domain/users/user.type.js";
|
import { canManageRole, type UserRole } from "@/domain/users/user.type.js";
|
||||||
import bcrypt from "bcryptjs";
|
import bcrypt from "bcryptjs";
|
||||||
import { AppError } from "@/utils/AppError.js";
|
import { AppError } from "@/utils/AppError.js";
|
||||||
import { IJobQueue } from "@/service/job-queues/job-queue.interface.js";
|
|
||||||
import { IEmailService } from "@/service/emailService.js";
|
import { IEmailService } from "@/service/emailService.js";
|
||||||
import { EnvConfig, ISettingsService } from "@/domain/app-settings/app-settings.service.js";
|
import { EnvConfig, ISettingsService } from "@/domain/app-settings/app-settings.service.js";
|
||||||
import { ILogger } from "@/utils/logger.js";
|
import { ILogger } from "@/utils/logger.js";
|
||||||
import jwt from "jsonwebtoken";
|
import jwt from "jsonwebtoken";
|
||||||
import crypto from "crypto";
|
import crypto from "crypto";
|
||||||
|
import { IWorker } from "@/worker/worker.interface.js";
|
||||||
type CryptoType = typeof crypto;
|
type CryptoType = typeof crypto;
|
||||||
type JwtType = typeof jwt;
|
type JwtType = typeof jwt;
|
||||||
const SERVICE_NAME = "userService";
|
const SERVICE_NAME = "userService";
|
||||||
@@ -53,7 +53,7 @@ export class UserService implements IUserService {
|
|||||||
private settingsService: ISettingsService;
|
private settingsService: ISettingsService;
|
||||||
private logger: ILogger;
|
private logger: ILogger;
|
||||||
private jwt: JwtType;
|
private jwt: JwtType;
|
||||||
private jobQueue: IJobQueue;
|
private worker: IWorker;
|
||||||
private crypto: CryptoType;
|
private crypto: CryptoType;
|
||||||
private monitorsRepository: IMonitorsRepository;
|
private monitorsRepository: IMonitorsRepository;
|
||||||
private usersRepository: IUsersRepository;
|
private usersRepository: IUsersRepository;
|
||||||
@@ -68,7 +68,7 @@ export class UserService implements IUserService {
|
|||||||
settingsService,
|
settingsService,
|
||||||
logger,
|
logger,
|
||||||
jwt,
|
jwt,
|
||||||
jobQueue,
|
worker,
|
||||||
monitorsRepository,
|
monitorsRepository,
|
||||||
usersRepository,
|
usersRepository,
|
||||||
invitesRepository,
|
invitesRepository,
|
||||||
@@ -81,7 +81,7 @@ export class UserService implements IUserService {
|
|||||||
settingsService: ISettingsService;
|
settingsService: ISettingsService;
|
||||||
logger: ILogger;
|
logger: ILogger;
|
||||||
jwt: JwtType;
|
jwt: JwtType;
|
||||||
jobQueue: IJobQueue;
|
worker: IWorker;
|
||||||
monitorsRepository: IMonitorsRepository;
|
monitorsRepository: IMonitorsRepository;
|
||||||
usersRepository: IUsersRepository;
|
usersRepository: IUsersRepository;
|
||||||
invitesRepository: IInvitesRepository;
|
invitesRepository: IInvitesRepository;
|
||||||
@@ -93,7 +93,7 @@ export class UserService implements IUserService {
|
|||||||
this.settingsService = settingsService;
|
this.settingsService = settingsService;
|
||||||
this.logger = logger;
|
this.logger = logger;
|
||||||
this.jwt = jwt;
|
this.jwt = jwt;
|
||||||
this.jobQueue = jobQueue;
|
this.worker = worker;
|
||||||
this.crypto = crypto;
|
this.crypto = crypto;
|
||||||
this.monitorsRepository = monitorsRepository;
|
this.monitorsRepository = monitorsRepository;
|
||||||
this.usersRepository = usersRepository;
|
this.usersRepository = usersRepository;
|
||||||
@@ -330,7 +330,7 @@ export class UserService implements IUserService {
|
|||||||
if (roles.includes("superadmin")) {
|
if (roles.includes("superadmin")) {
|
||||||
const monitors = await this.monitorsRepository.findByTeamId(teamId, {});
|
const monitors = await this.monitorsRepository.findByTeamId(teamId, {});
|
||||||
if (monitors) {
|
if (monitors) {
|
||||||
await Promise.all(monitors.map((monitor) => this.jobQueue.deleteJob(monitor)));
|
await Promise.all(monitors.map((monitor) => this.worker.deleteJob(monitor)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// 6. Delete the user by id
|
// 6. Delete the user by id
|
||||||
|
|||||||
@@ -63,7 +63,7 @@ export class EmailService implements IEmailService {
|
|||||||
init = () => {
|
init = () => {
|
||||||
const loadTemplate = (templateName: string) => {
|
const loadTemplate = (templateName: string) => {
|
||||||
try {
|
try {
|
||||||
const templatePath = this.path.join(__dirname, `../../templates/${templateName}.mjml`);
|
const templatePath = this.path.join(__dirname, `../templates/${templateName}.mjml`);
|
||||||
const templateContent = this.fs.readFileSync(templatePath, "utf8");
|
const templateContent = this.fs.readFileSync(templatePath, "utf8");
|
||||||
return this.compile(templateContent);
|
return this.compile(templateContent);
|
||||||
} catch (error: unknown) {
|
} catch (error: unknown) {
|
||||||
|
|||||||
@@ -1,434 +0,0 @@
|
|||||||
const SERVICE_NAME = "JobQueueHelper";
|
|
||||||
import type { Monitor } from "@/domain/monitors/monitor.types.js";
|
|
||||||
import { supportsGeoCheck } from "@/domain/monitors/monitor.types.js";
|
|
||||||
import { AppError } from "@/utils/AppError.js";
|
|
||||||
import { ISettingsService } from "@/domain/app-settings/app-settings.service.js";
|
|
||||||
import { ICheckService } from "@/domain/checks/check.service.js";
|
|
||||||
import { type IGeoChecksService } from "@/domain/geo-checks/geo-check.service.js";
|
|
||||||
import { IncidentService } from "@/domain/incidents/incident.service.js";
|
|
||||||
import { INotificationsService } from "@/domain/notifications/notification.service.js";
|
|
||||||
import { INetworkService } from "@/service/networkService.js";
|
|
||||||
import { IStatusService } from "@/service/statusService.js";
|
|
||||||
import { CHECK_TTL_SENTINEL } from "@/domain/checks/check.type.js";
|
|
||||||
import { type StatusChangeResult } from "@/types/network.js";
|
|
||||||
import { isWindowActive } from "@/utils/maintenanceWindow.js";
|
|
||||||
import { IChecksRepository } from "@/domain/checks/check.repository.interface.js";
|
|
||||||
import { IGeoChecksRepository } from "@/domain/geo-checks/geo-check.repository.interface.js";
|
|
||||||
import { IIncidentsRepository } from "@/domain/incidents/incident.repository.interface.js";
|
|
||||||
import { IMaintenanceWindowsRepository } from "@/domain/maintenance-windows/maintenance-window.repository.interface.js";
|
|
||||||
import { IMonitorStatsRepository } from "@/domain/monitor-stats/monitor-stats.repository.interface.js";
|
|
||||||
import { IMonitorsRepository } from "@/domain/monitors/monitor.repository.interface.js";
|
|
||||||
import { ITeamsRepository } from "@/domain/teams/team.repository.interface.js";
|
|
||||||
import { ILogger } from "@/utils/logger.js";
|
|
||||||
import { IBufferService } from "@/service/bufferService.js";
|
|
||||||
|
|
||||||
export interface IQueueHelper {
|
|
||||||
readonly serviceName: string;
|
|
||||||
getHeartbeatJob(): (monitor: Monitor) => Promise<void>;
|
|
||||||
getHeartbeatGeoJob(): (monitor: Monitor) => Promise<void>;
|
|
||||||
getCleanupOrphanedJob(): () => Promise<void>;
|
|
||||||
getCleanupRetentionJob(): () => Promise<void>;
|
|
||||||
isInMaintenanceWindow(monitorId: string, teamId: string): Promise<boolean>;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface MonitorActionDecision {
|
|
||||||
shouldCreateIncident: boolean;
|
|
||||||
shouldResolveIncident: boolean;
|
|
||||||
shouldSendNotification: boolean;
|
|
||||||
incidentReason: "status_down" | "threshold_breach" | null;
|
|
||||||
notificationReason: "status_change" | "threshold_breach" | null;
|
|
||||||
thresholdBreaches?: {
|
|
||||||
cpu?: boolean;
|
|
||||||
memory?: boolean;
|
|
||||||
disk?: boolean;
|
|
||||||
temp?: boolean;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
export class QueueHelper implements IQueueHelper {
|
|
||||||
static SERVICE_NAME = SERVICE_NAME;
|
|
||||||
|
|
||||||
private logger: ILogger;
|
|
||||||
private networkService: INetworkService;
|
|
||||||
private statusService: IStatusService;
|
|
||||||
private notificationsService: INotificationsService;
|
|
||||||
private checkService: ICheckService;
|
|
||||||
private settingsService: ISettingsService;
|
|
||||||
private buffer: IBufferService;
|
|
||||||
private incidentService: IncidentService;
|
|
||||||
private maintenanceWindowsRepository: IMaintenanceWindowsRepository;
|
|
||||||
private monitorsRepository: IMonitorsRepository;
|
|
||||||
private teamsRepository: ITeamsRepository;
|
|
||||||
private monitorStatsRepository: IMonitorStatsRepository;
|
|
||||||
private checksRepository: IChecksRepository;
|
|
||||||
private incidentsRepository: IIncidentsRepository;
|
|
||||||
private geoChecksService: IGeoChecksService;
|
|
||||||
private geoChecksRepository: IGeoChecksRepository;
|
|
||||||
|
|
||||||
constructor(
|
|
||||||
logger: ILogger,
|
|
||||||
networkService: INetworkService,
|
|
||||||
statusService: IStatusService,
|
|
||||||
notificationsService: INotificationsService,
|
|
||||||
checkService: ICheckService,
|
|
||||||
settingsService: ISettingsService,
|
|
||||||
buffer: IBufferService,
|
|
||||||
incidentService: IncidentService,
|
|
||||||
maintenanceWindowsRepository: IMaintenanceWindowsRepository,
|
|
||||||
monitorsRepository: IMonitorsRepository,
|
|
||||||
teamsRepository: ITeamsRepository,
|
|
||||||
monitorStatsRepository: IMonitorStatsRepository,
|
|
||||||
checksRepository: IChecksRepository,
|
|
||||||
incidentsRepository: IIncidentsRepository,
|
|
||||||
geoChecksService: IGeoChecksService,
|
|
||||||
geoChecksRepository: IGeoChecksRepository
|
|
||||||
) {
|
|
||||||
this.logger = logger;
|
|
||||||
this.networkService = networkService;
|
|
||||||
this.statusService = statusService;
|
|
||||||
this.checkService = checkService;
|
|
||||||
this.settingsService = settingsService;
|
|
||||||
this.buffer = buffer;
|
|
||||||
this.notificationsService = notificationsService;
|
|
||||||
this.incidentService = incidentService;
|
|
||||||
this.maintenanceWindowsRepository = maintenanceWindowsRepository;
|
|
||||||
this.monitorsRepository = monitorsRepository;
|
|
||||||
this.teamsRepository = teamsRepository;
|
|
||||||
this.monitorStatsRepository = monitorStatsRepository;
|
|
||||||
this.checksRepository = checksRepository;
|
|
||||||
this.incidentsRepository = incidentsRepository;
|
|
||||||
this.geoChecksService = geoChecksService;
|
|
||||||
this.geoChecksRepository = geoChecksRepository;
|
|
||||||
}
|
|
||||||
|
|
||||||
get serviceName() {
|
|
||||||
return QueueHelper.SERVICE_NAME;
|
|
||||||
}
|
|
||||||
|
|
||||||
getHeartbeatJob = () => {
|
|
||||||
return async (monitor: Monitor) => {
|
|
||||||
try {
|
|
||||||
const monitorId = monitor.id;
|
|
||||||
const teamId = monitor.teamId;
|
|
||||||
if (!monitorId) {
|
|
||||||
throw new AppError({ message: "No monitor id", service: SERVICE_NAME, method: "getMonitorJob" });
|
|
||||||
}
|
|
||||||
|
|
||||||
// Step 1. Check for maintenance window, if found, skip the check
|
|
||||||
|
|
||||||
const maintenanceWindowActive = await this.isInMaintenanceWindow(monitorId, teamId);
|
|
||||||
if (maintenanceWindowActive) {
|
|
||||||
this.logger.debug({
|
|
||||||
message: `Monitor ${monitorId} is in maintenance window`,
|
|
||||||
service: SERVICE_NAME,
|
|
||||||
method: "getMonitorJob",
|
|
||||||
});
|
|
||||||
if (monitor.status !== "maintenance") {
|
|
||||||
await this.monitorsRepository.updateById(monitorId, teamId, { status: "maintenance" });
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Step 2. Request monitor status
|
|
||||||
const status = await this.networkService.requestStatus(monitor);
|
|
||||||
if (!status) {
|
|
||||||
throw new Error("No network response");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Step 3. Build check
|
|
||||||
const check = this.checkService.buildCheck(status);
|
|
||||||
if (!check) {
|
|
||||||
this.logger.warn({
|
|
||||||
message: `No check could be built for monitor ${monitorId}`,
|
|
||||||
service: SERVICE_NAME,
|
|
||||||
method: "getMonitorJob",
|
|
||||||
details: { code: status.code, message: status.message },
|
|
||||||
});
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
// Step 4 Add check to buffer
|
|
||||||
this.buffer.addToBuffer(check);
|
|
||||||
// Step 4. Update monitor status
|
|
||||||
const statusChangeResult = await this.statusService.updateMonitorStatus(status, check);
|
|
||||||
|
|
||||||
// Step 5. Get decisions
|
|
||||||
const decision = this.evaluateMonitorAction(statusChangeResult);
|
|
||||||
|
|
||||||
// Step 6. Handle notifications (best effort, continue even in event of failure, don't wait)
|
|
||||||
if (decision.shouldSendNotification) {
|
|
||||||
this.notificationsService.handleNotifications(statusChangeResult.monitor, status, decision).catch((error: unknown) => {
|
|
||||||
this.logger.error({
|
|
||||||
message: `Error sending notifications for job ${statusChangeResult.monitor.id}: ${error instanceof Error ? error.message : "Unknown error"}`,
|
|
||||||
service: SERVICE_NAME,
|
|
||||||
method: "getMonitorJob",
|
|
||||||
stack: error instanceof Error ? error.stack : undefined,
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Step 7. Handle incidents
|
|
||||||
try {
|
|
||||||
await this.incidentService.handleIncident(statusChangeResult.monitor, statusChangeResult.code, decision, status);
|
|
||||||
} catch (error: unknown) {
|
|
||||||
this.logger.warn({
|
|
||||||
message: `Error handling incident for job ${monitor.id}: ${error instanceof Error ? error.message : "Unknown error"}`,
|
|
||||||
service: SERVICE_NAME,
|
|
||||||
method: "getMonitorJob",
|
|
||||||
stack: error instanceof Error ? error.stack : undefined,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
} catch (error: unknown) {
|
|
||||||
this.logger.warn({
|
|
||||||
message: error instanceof Error ? error.message : "Unknown error",
|
|
||||||
service: SERVICE_NAME,
|
|
||||||
method: "getMonitorJob",
|
|
||||||
stack: error instanceof Error ? error.stack : undefined,
|
|
||||||
});
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
getCleanupOrphanedJob = () => {
|
|
||||||
return async () => {
|
|
||||||
try {
|
|
||||||
this.logger.info({
|
|
||||||
message: "Starting cleanup of orphaned data",
|
|
||||||
service: SERVICE_NAME,
|
|
||||||
method: "getCleanupOrphanedJob",
|
|
||||||
});
|
|
||||||
|
|
||||||
// Get all valid team IDs
|
|
||||||
const validTeamIds = await this.teamsRepository.findAllTeamIds();
|
|
||||||
this.logger.debug({
|
|
||||||
message: `Found ${validTeamIds.length} valid teams`,
|
|
||||||
service: SERVICE_NAME,
|
|
||||||
method: "getCleanupOrphanedJob",
|
|
||||||
});
|
|
||||||
|
|
||||||
// Remove orphaned monitors (monitors without a valid team)
|
|
||||||
const deletedMonitorCount = await this.monitorsRepository.deleteByTeamIdsNotIn(validTeamIds);
|
|
||||||
if (deletedMonitorCount > 0) {
|
|
||||||
this.logger.info({
|
|
||||||
message: `Deleted ${deletedMonitorCount} orphaned monitors`,
|
|
||||||
service: SERVICE_NAME,
|
|
||||||
method: "getCleanupOrphanedJob",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Remove orphaned monitorStats (stats without a valid monitor)
|
|
||||||
const allMonitorIds = await this.monitorsRepository.findAllMonitorIds();
|
|
||||||
this.logger.debug({
|
|
||||||
message: `Found ${allMonitorIds.length} valid monitors`,
|
|
||||||
service: SERVICE_NAME,
|
|
||||||
method: "getCleanupOrphanedJob",
|
|
||||||
});
|
|
||||||
|
|
||||||
const deletedStatsCount = await this.monitorStatsRepository.deleteByMonitorIdsNotIn(allMonitorIds);
|
|
||||||
if (deletedStatsCount > 0) {
|
|
||||||
this.logger.info({
|
|
||||||
message: `Deleted ${deletedStatsCount} orphaned monitor stats`,
|
|
||||||
service: SERVICE_NAME,
|
|
||||||
method: "getCleanupOrphanedJob",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Remove orphaned checks
|
|
||||||
const deletedChecksCount = await this.checksRepository.deleteByMonitorIdsNotIn(allMonitorIds);
|
|
||||||
if (deletedChecksCount > 0) {
|
|
||||||
this.logger.info({
|
|
||||||
message: `Deleted ${deletedChecksCount} orphaned checks`,
|
|
||||||
service: SERVICE_NAME,
|
|
||||||
method: "getCleanupOrphanedJob",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Remove orphaned geo checks
|
|
||||||
const deletedGeoChecksCount = await this.geoChecksRepository.deleteByMonitorIdsNotIn(allMonitorIds);
|
|
||||||
if (deletedGeoChecksCount > 0) {
|
|
||||||
this.logger.info({
|
|
||||||
message: `Deleted ${deletedGeoChecksCount} orphaned geo checks`,
|
|
||||||
service: SERVICE_NAME,
|
|
||||||
method: "getCleanupOrphanedJob",
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
this.logger.info({
|
|
||||||
message: "Cleanup of orphaned data completed",
|
|
||||||
service: SERVICE_NAME,
|
|
||||||
method: "getCleanupOrphanedJob",
|
|
||||||
});
|
|
||||||
} catch (error: unknown) {
|
|
||||||
this.logger.warn({
|
|
||||||
message: error instanceof Error ? error.message : "Unknown error",
|
|
||||||
service: SERVICE_NAME,
|
|
||||||
method: "getCleanupOrphanedJob",
|
|
||||||
stack: error instanceof Error ? error.stack : undefined,
|
|
||||||
});
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
getHeartbeatGeoJob = () => {
|
|
||||||
return async (monitor: Monitor) => {
|
|
||||||
try {
|
|
||||||
const monitorId = monitor.id;
|
|
||||||
const teamId = monitor.teamId;
|
|
||||||
|
|
||||||
// Step 1: Validate monitor eligibility
|
|
||||||
if (!monitorId) {
|
|
||||||
throw new AppError({ message: "No monitor id", service: SERVICE_NAME, method: "getHeartbeatGeoJob" });
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!monitor.geoCheckEnabled) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (!supportsGeoCheck(monitor.type)) {
|
|
||||||
this.logger.debug({
|
|
||||||
message: `Monitor ${monitorId} type does not support geo checks, skipping`,
|
|
||||||
service: SERVICE_NAME,
|
|
||||||
method: "getHeartbeatGeoJob",
|
|
||||||
});
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!monitor.geoCheckLocations || monitor.geoCheckLocations.length === 0) {
|
|
||||||
this.logger.warn({
|
|
||||||
message: `No geo check locations configured for monitor ${monitorId}`,
|
|
||||||
service: SERVICE_NAME,
|
|
||||||
method: "getHeartbeatGeoJob",
|
|
||||||
});
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Step 2: Check for maintenance window
|
|
||||||
const maintenanceWindowActive = await this.isInMaintenanceWindow(monitorId, teamId);
|
|
||||||
if (maintenanceWindowActive) {
|
|
||||||
this.logger.debug({
|
|
||||||
message: `Monitor ${monitorId} is in maintenance window, skipping geo check`,
|
|
||||||
service: SERVICE_NAME,
|
|
||||||
method: "getHeartbeatGeoJob",
|
|
||||||
});
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Step 3: Build geo check (handles API calls and polling)
|
|
||||||
const geoCheck = await this.geoChecksService.buildGeoCheck(monitor);
|
|
||||||
if (!geoCheck) {
|
|
||||||
this.logger.warn({
|
|
||||||
message: `No geo check could be built for monitor ${monitorId}`,
|
|
||||||
service: SERVICE_NAME,
|
|
||||||
method: "getHeartbeatGeoJob",
|
|
||||||
});
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Step 4: Add geo check to buffer
|
|
||||||
this.buffer.addGeoCheckToBuffer(geoCheck);
|
|
||||||
|
|
||||||
this.logger.debug({
|
|
||||||
message: `Geo check job executed for monitor ${monitorId}`,
|
|
||||||
service: SERVICE_NAME,
|
|
||||||
method: "getHeartbeatGeoJob",
|
|
||||||
});
|
|
||||||
} catch (error: unknown) {
|
|
||||||
this.logger.error({
|
|
||||||
message: error instanceof Error ? error.message : "Unknown error",
|
|
||||||
service: SERVICE_NAME,
|
|
||||||
method: "getHeartbeatGeoJob",
|
|
||||||
stack: error instanceof Error ? error.stack : undefined,
|
|
||||||
});
|
|
||||||
// Don't throw - geo check failures shouldn't crash the job scheduler
|
|
||||||
}
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
async isInMaintenanceWindow(monitorId: string, teamId: string) {
|
|
||||||
const maintenanceWindows = await this.maintenanceWindowsRepository.findByMonitorId(monitorId, teamId);
|
|
||||||
const now = new Date();
|
|
||||||
return maintenanceWindows.some((window) => isWindowActive(window, now));
|
|
||||||
}
|
|
||||||
|
|
||||||
getCleanupRetentionJob = () => {
|
|
||||||
return async () => {
|
|
||||||
try {
|
|
||||||
const settings = await this.settingsService.getDBSettings();
|
|
||||||
|
|
||||||
const checkTTL = settings.checkTTL; // Check TTL is in DAYS, not MS
|
|
||||||
|
|
||||||
if (checkTTL === CHECK_TTL_SENTINEL) {
|
|
||||||
this.logger.info({
|
|
||||||
message: `Check TTL is set to unlimited, skipping cleanup`,
|
|
||||||
service: SERVICE_NAME,
|
|
||||||
method: "getCleanupRetentionJob",
|
|
||||||
});
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const checkTTLInMs = checkTTL * 24 * 60 * 60 * 1000;
|
|
||||||
const cutoffDate = new Date(Date.now() - checkTTLInMs);
|
|
||||||
const deleteCount = await this.checkService.deleteOlderThan(cutoffDate);
|
|
||||||
this.logger.info({
|
|
||||||
message: `Deleted ${deleteCount} checks older than ${cutoffDate.toISOString()}`,
|
|
||||||
service: SERVICE_NAME,
|
|
||||||
method: "getCleanupRetentionJob",
|
|
||||||
});
|
|
||||||
} catch (error: unknown) {
|
|
||||||
this.logger.error({
|
|
||||||
message: error instanceof Error ? error.message : "Unknown error",
|
|
||||||
service: SERVICE_NAME,
|
|
||||||
method: "getCleanupRetentionJob",
|
|
||||||
stack: error instanceof Error ? error.stack : undefined,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
private evaluateMonitorAction(statusChangeResult: StatusChangeResult): MonitorActionDecision {
|
|
||||||
const { monitor, statusChanged, prevStatus } = statusChangeResult;
|
|
||||||
|
|
||||||
// Initialize result
|
|
||||||
const decision: MonitorActionDecision = {
|
|
||||||
shouldCreateIncident: false,
|
|
||||||
shouldResolveIncident: false,
|
|
||||||
shouldSendNotification: false,
|
|
||||||
incidentReason: null,
|
|
||||||
notificationReason: null,
|
|
||||||
};
|
|
||||||
|
|
||||||
if (!statusChanged) {
|
|
||||||
return decision;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (monitor.status === "down") {
|
|
||||||
// Monitor went down (unreachable)
|
|
||||||
decision.shouldCreateIncident = true;
|
|
||||||
decision.shouldSendNotification = true;
|
|
||||||
decision.incidentReason = "status_down";
|
|
||||||
decision.notificationReason = "status_change";
|
|
||||||
} else if (monitor.status === "breached") {
|
|
||||||
// Hardware monitor exceeded thresholds
|
|
||||||
decision.shouldCreateIncident = true;
|
|
||||||
decision.shouldSendNotification = true;
|
|
||||||
decision.incidentReason = "threshold_breach";
|
|
||||||
decision.notificationReason = "threshold_breach";
|
|
||||||
} else if (monitor.status === "up" && (prevStatus === "down" || prevStatus === "breached")) {
|
|
||||||
// Monitor recovered from down or breached state
|
|
||||||
decision.shouldResolveIncident = true;
|
|
||||||
decision.shouldSendNotification = true;
|
|
||||||
decision.notificationReason = "status_change";
|
|
||||||
}
|
|
||||||
|
|
||||||
return decision;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -16,7 +16,7 @@ export const initShutdownListener = (server: Server | null, services: Initialize
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
server?.close();
|
server?.close();
|
||||||
await services.jobQueue.shutdown();
|
await services.worker.shutdown();
|
||||||
await services.db.disconnect();
|
await services.db.disconnect();
|
||||||
logger.info({ message: "Graceful shutdown complete" });
|
logger.info({ message: "Graceful shutdown complete" });
|
||||||
process.exit(0);
|
process.exit(0);
|
||||||
|
|||||||
@@ -0,0 +1,5 @@
|
|||||||
|
// Shared constraints for user supplied image uploads
|
||||||
|
export const ImageMimeTypes = ["image/jpeg", "image/png", "image/jpg"] as const;
|
||||||
|
export type ImageMimeType = (typeof ImageMimeTypes)[number];
|
||||||
|
|
||||||
|
export const MAX_IMAGE_SIZE_BYTES = 3 * 1024 * 1024; // 3MB
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
import { ILogger } from "@/utils/logger.js";
|
||||||
|
import { MonitorEvaluation } from "@/worker/worker.interface.js";
|
||||||
|
import { IMonitorReactor } from "@/worker/reactors/reactor.interface.js";
|
||||||
|
|
||||||
|
const SERVICE_NAME = "ReactorDispatcher";
|
||||||
|
|
||||||
|
export interface IReactorDispatcher {
|
||||||
|
dispatch(evaluation: MonitorEvaluation): Promise<void>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class ReactorDispatcher implements IReactorDispatcher {
|
||||||
|
constructor(
|
||||||
|
private logger: ILogger,
|
||||||
|
private reactors: IMonitorReactor[]
|
||||||
|
) {}
|
||||||
|
|
||||||
|
private logFailure(reactor: IMonitorReactor, evaluation: MonitorEvaluation, error: unknown) {
|
||||||
|
this.logger.error({
|
||||||
|
service: SERVICE_NAME,
|
||||||
|
method: "dispatch",
|
||||||
|
message: `Reactor ${reactor.name} failed for monitor ${evaluation.monitor.id}: ${error instanceof Error ? error.message : String(error)}`,
|
||||||
|
stack: error instanceof Error ? error.stack : undefined,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
dispatch = async (evaluation: MonitorEvaluation) => {
|
||||||
|
for (const reactor of this.reactors) {
|
||||||
|
if (reactor.blocking) {
|
||||||
|
try {
|
||||||
|
await reactor.react(evaluation);
|
||||||
|
} catch (error: unknown) {
|
||||||
|
this.logFailure(reactor, evaluation, error);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
reactor.react(evaluation).catch((error: unknown) => this.logFailure(reactor, evaluation, error));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
import { IMonitorReactor } from "@/worker/reactors/reactor.interface.js";
|
||||||
|
import { MonitorEvaluation } from "@/worker/worker.interface.js";
|
||||||
|
import { IIncidentService } from "@/domain/incidents/incident.service.js";
|
||||||
|
|
||||||
|
export class IncidentReactor implements IMonitorReactor {
|
||||||
|
readonly name = "incident";
|
||||||
|
readonly blocking = true;
|
||||||
|
constructor(private incidentService: IIncidentService) {}
|
||||||
|
|
||||||
|
react = async (evaluation: MonitorEvaluation) => {
|
||||||
|
await this.incidentService.handleIncident(evaluation.monitor, evaluation.statusChange.code, evaluation.decision, evaluation.status);
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
import { MonitorEvaluation } from "@/worker/worker.interface.js";
|
||||||
|
|
||||||
|
export interface IMonitorReactor {
|
||||||
|
readonly name: string;
|
||||||
|
readonly blocking: boolean;
|
||||||
|
react(evaluation: MonitorEvaluation): Promise<void>;
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
import { INotificationsService } from "@/domain/notifications/notification.service.js";
|
||||||
|
import { IMonitorReactor } from "@/worker/reactors/reactor.interface.js";
|
||||||
|
import { MonitorEvaluation } from "@/worker/worker.interface.js";
|
||||||
|
export class NotificationReactor implements IMonitorReactor {
|
||||||
|
readonly name = "notification";
|
||||||
|
readonly blocking = false;
|
||||||
|
constructor(private notificationService: INotificationsService) {}
|
||||||
|
|
||||||
|
react = async (evaluation: MonitorEvaluation) => {
|
||||||
|
if (!evaluation.decision.shouldSendNotification) return;
|
||||||
|
await this.notificationService.handleNotifications(evaluation.monitor, evaluation.status, evaluation.decision);
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -0,0 +1,187 @@
|
|||||||
|
import { Monitor } from "@/domain/monitors/monitor.types.js";
|
||||||
|
import { MonitorEvaluation } from "@/worker/worker.interface.js";
|
||||||
|
import { IMaintenanceWindowsRepository } from "@/domain/maintenance-windows/maintenance-window.repository.interface.js";
|
||||||
|
import { IMonitorsRepository } from "@/domain/monitors/monitor.repository.interface.js";
|
||||||
|
import { isWindowActive } from "@/utils/maintenanceWindow.js";
|
||||||
|
import { ILogger } from "@/utils/logger.js";
|
||||||
|
import { INetworkService } from "@/service/networkService.js";
|
||||||
|
import { ICheckService } from "@/domain/checks/check.service.js";
|
||||||
|
import { IBufferService } from "@/service/bufferService.js";
|
||||||
|
import { IMonitorStatusPolicy } from "@/worker/worker.monitor-status-policy.js";
|
||||||
|
import { IStatusService } from "@/service/statusService.js";
|
||||||
|
import { AppError } from "@/utils/AppError.js";
|
||||||
|
import { supportsGeoCheck } from "@/domain/monitors/monitor.types.js";
|
||||||
|
import { IGeoChecksService } from "@/domain/geo-checks/geo-check.service.js";
|
||||||
|
|
||||||
|
const SERVICE_NAME = "CheckPipeline";
|
||||||
|
|
||||||
|
export interface ICheckPipeline {
|
||||||
|
run(monitor: Monitor): Promise<MonitorEvaluation | null>; // null = skipped
|
||||||
|
}
|
||||||
|
|
||||||
|
export class CheckPipeline implements ICheckPipeline {
|
||||||
|
constructor(
|
||||||
|
private monitorsRepository: IMonitorsRepository,
|
||||||
|
private maintenanceWindowsRepository: IMaintenanceWindowsRepository,
|
||||||
|
private checkService: ICheckService,
|
||||||
|
private networkService: INetworkService,
|
||||||
|
private bufferService: IBufferService,
|
||||||
|
private monitorStatusPolicy: IMonitorStatusPolicy,
|
||||||
|
private statusService: IStatusService,
|
||||||
|
private logger: ILogger
|
||||||
|
) {}
|
||||||
|
|
||||||
|
private async isInMaintenanceWindow(monitorId: string, teamId: string) {
|
||||||
|
const maintenanceWindows = await this.maintenanceWindowsRepository.findByMonitorId(monitorId, teamId);
|
||||||
|
const now = new Date();
|
||||||
|
return maintenanceWindows.some((window) => isWindowActive(window, now));
|
||||||
|
}
|
||||||
|
|
||||||
|
run = async (monitor: Monitor): Promise<MonitorEvaluation | null> => {
|
||||||
|
const monitorId = monitor.id;
|
||||||
|
if (!monitorId) {
|
||||||
|
throw new AppError({ message: "No monitor id", service: SERVICE_NAME, method: "getMonitorJob" });
|
||||||
|
}
|
||||||
|
// ****************************
|
||||||
|
// Step 1: Acquire
|
||||||
|
// ****************************
|
||||||
|
|
||||||
|
// Step 1a: Maintenance window gate - skip if in maintenance
|
||||||
|
const maintenanceWindowActive = await this.isInMaintenanceWindow(monitor.id, monitor.teamId);
|
||||||
|
if (maintenanceWindowActive) {
|
||||||
|
this.logger.debug({
|
||||||
|
message: `Monitor ${monitor.id} is in maintenance window`,
|
||||||
|
service: SERVICE_NAME,
|
||||||
|
method: "runCheckPipeline",
|
||||||
|
});
|
||||||
|
if (monitor.status !== "maintenance") {
|
||||||
|
await this.monitorsRepository.updateById(monitor.id, monitor.teamId, { status: "maintenance" });
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Step 1b: Acquire status
|
||||||
|
const status = await this.networkService.requestStatus(monitor);
|
||||||
|
if (!status) {
|
||||||
|
throw new Error("No network response");
|
||||||
|
}
|
||||||
|
|
||||||
|
// ****************************
|
||||||
|
// Step 2: Record
|
||||||
|
// ****************************
|
||||||
|
|
||||||
|
// Step 2a: Create & record a check, return null if fail
|
||||||
|
const check = this.checkService.buildCheck(status);
|
||||||
|
if (!check) {
|
||||||
|
this.logger.warn({
|
||||||
|
message: `No check could be built for monitor ${monitor.id}`,
|
||||||
|
service: SERVICE_NAME,
|
||||||
|
method: "runCHeckPipeline",
|
||||||
|
details: { code: status.code, message: status.message },
|
||||||
|
});
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
// Step 2b: Add to buffer
|
||||||
|
this.bufferService.addToBuffer(check);
|
||||||
|
|
||||||
|
// ****************************
|
||||||
|
// Step 3: Evaluate and return result to reactors
|
||||||
|
// ****************************
|
||||||
|
const statusChangeResult = await this.statusService.updateMonitorStatus(status, check);
|
||||||
|
|
||||||
|
// Step 5. Get decisions and create an evaluation obj
|
||||||
|
const decision = this.monitorStatusPolicy.evaluate(statusChangeResult);
|
||||||
|
const evaluation: MonitorEvaluation = {
|
||||||
|
monitor: statusChangeResult.monitor,
|
||||||
|
status,
|
||||||
|
check,
|
||||||
|
statusChange: statusChangeResult,
|
||||||
|
decision,
|
||||||
|
};
|
||||||
|
return evaluation;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export class GeoChecksPipeline implements ICheckPipeline {
|
||||||
|
constructor(
|
||||||
|
private maintenanceWindowsRepository: IMaintenanceWindowsRepository,
|
||||||
|
private geoChecksService: IGeoChecksService,
|
||||||
|
private bufferService: IBufferService,
|
||||||
|
private logger: ILogger
|
||||||
|
) {}
|
||||||
|
|
||||||
|
private async isInMaintenanceWindow(monitorId: string, teamId: string) {
|
||||||
|
const maintenanceWindows = await this.maintenanceWindowsRepository.findByMonitorId(monitorId, teamId);
|
||||||
|
const now = new Date();
|
||||||
|
return maintenanceWindows.some((window) => isWindowActive(window, now));
|
||||||
|
}
|
||||||
|
|
||||||
|
run = async (monitor: Monitor): Promise<MonitorEvaluation | null> => {
|
||||||
|
// ****************************
|
||||||
|
// Step 1: Acquire
|
||||||
|
// ****************************
|
||||||
|
|
||||||
|
// Step 1a: Guards - skip if unsupported or not enabled
|
||||||
|
|
||||||
|
if (!monitor.id) {
|
||||||
|
throw new AppError({ message: "No monitor id", service: SERVICE_NAME, method: "getHeartbeatGeoJob" });
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!monitor.geoCheckEnabled) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
if (!supportsGeoCheck(monitor.type)) {
|
||||||
|
this.logger.debug({
|
||||||
|
message: `Monitor ${monitor.id} type does not support geo checks, skipping`,
|
||||||
|
service: SERVICE_NAME,
|
||||||
|
method: "runGeoChecksPipeline",
|
||||||
|
});
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!monitor.geoCheckLocations || monitor.geoCheckLocations.length === 0) {
|
||||||
|
this.logger.warn({
|
||||||
|
message: `No geo check locations configured for monitor ${monitor.id}`,
|
||||||
|
service: SERVICE_NAME,
|
||||||
|
method: "runGeoChecksPipeline",
|
||||||
|
});
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Step 1b: Maintenance window check
|
||||||
|
const maintenanceWindowActive = await this.isInMaintenanceWindow(monitor.id, monitor.teamId);
|
||||||
|
if (maintenanceWindowActive) {
|
||||||
|
this.logger.debug({
|
||||||
|
message: `Monitor ${monitor.id} is in maintenance window, skipping geo check`,
|
||||||
|
service: SERVICE_NAME,
|
||||||
|
method: "runGeoChecksPipeline",
|
||||||
|
});
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ****************************
|
||||||
|
// Step 2: Record
|
||||||
|
// ****************************
|
||||||
|
|
||||||
|
// Step 2a: Build geo check, return null if fail
|
||||||
|
const geoCheck = await this.geoChecksService.buildGeoCheck(monitor);
|
||||||
|
if (!geoCheck) {
|
||||||
|
this.logger.warn({
|
||||||
|
message: `No geo check could be built for monitor ${monitor.id}`,
|
||||||
|
service: SERVICE_NAME,
|
||||||
|
method: "runGeoChecksPipeline",
|
||||||
|
});
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Step 2b: Add to buffer
|
||||||
|
this.bufferService.addGeoCheckToBuffer(geoCheck);
|
||||||
|
|
||||||
|
this.logger.debug({
|
||||||
|
message: `Geo check job executed for monitor ${monitor.id}`,
|
||||||
|
service: SERVICE_NAME,
|
||||||
|
method: "runGeoChecksPipeline",
|
||||||
|
});
|
||||||
|
return null;
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -0,0 +1,243 @@
|
|||||||
|
const SERVICE_NAME = "JobQueueHelper";
|
||||||
|
import type { Monitor } from "@/domain/monitors/monitor.types.js";
|
||||||
|
import { ISettingsService } from "@/domain/app-settings/app-settings.service.js";
|
||||||
|
import { ICheckService } from "@/domain/checks/check.service.js";
|
||||||
|
import { CHECK_TTL_SENTINEL } from "@/domain/checks/check.type.js";
|
||||||
|
import { IChecksRepository } from "@/domain/checks/check.repository.interface.js";
|
||||||
|
import { IGeoChecksRepository } from "@/domain/geo-checks/geo-check.repository.interface.js";
|
||||||
|
import { IIncidentsRepository } from "@/domain/incidents/incident.repository.interface.js";
|
||||||
|
import { IMonitorStatsRepository } from "@/domain/monitor-stats/monitor-stats.repository.interface.js";
|
||||||
|
import { IMonitorsRepository } from "@/domain/monitors/monitor.repository.interface.js";
|
||||||
|
import { ITeamsRepository } from "@/domain/teams/team.repository.interface.js";
|
||||||
|
import { ILogger } from "@/utils/logger.js";
|
||||||
|
import { IReactorDispatcher } from "@/worker/reactors/reactor.dispatcher.js";
|
||||||
|
import { ICheckPipeline } from "@/worker/worker.check-pipeline.js";
|
||||||
|
|
||||||
|
export interface IWorkerHelper {
|
||||||
|
readonly serviceName: string;
|
||||||
|
getHeartbeatJob(): (monitor: Monitor) => Promise<void>;
|
||||||
|
getHeartbeatGeoJob(): (monitor: Monitor) => Promise<void>;
|
||||||
|
getCleanupOrphanedJob(): () => Promise<void>;
|
||||||
|
getCleanupRetentionJob(): () => Promise<void>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface MonitorActionDecision {
|
||||||
|
shouldCreateIncident: boolean;
|
||||||
|
shouldResolveIncident: boolean;
|
||||||
|
shouldSendNotification: boolean;
|
||||||
|
incidentReason: "status_down" | "threshold_breach" | null;
|
||||||
|
notificationReason: "status_change" | "threshold_breach" | null;
|
||||||
|
thresholdBreaches?: {
|
||||||
|
cpu?: boolean;
|
||||||
|
memory?: boolean;
|
||||||
|
disk?: boolean;
|
||||||
|
temp?: boolean;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export class WorkerHelper implements IWorkerHelper {
|
||||||
|
static SERVICE_NAME = SERVICE_NAME;
|
||||||
|
|
||||||
|
private logger: ILogger;
|
||||||
|
private checkService: ICheckService;
|
||||||
|
private settingsService: ISettingsService;
|
||||||
|
private monitorsRepository: IMonitorsRepository;
|
||||||
|
private teamsRepository: ITeamsRepository;
|
||||||
|
private monitorStatsRepository: IMonitorStatsRepository;
|
||||||
|
private checksRepository: IChecksRepository;
|
||||||
|
private incidentsRepository: IIncidentsRepository;
|
||||||
|
private geoChecksRepository: IGeoChecksRepository;
|
||||||
|
private reactorDispatcher: IReactorDispatcher;
|
||||||
|
private checkPipeline: ICheckPipeline;
|
||||||
|
private geoCheckPipeline: ICheckPipeline;
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
logger: ILogger,
|
||||||
|
checkService: ICheckService,
|
||||||
|
settingsService: ISettingsService,
|
||||||
|
monitorsRepository: IMonitorsRepository,
|
||||||
|
teamsRepository: ITeamsRepository,
|
||||||
|
monitorStatsRepository: IMonitorStatsRepository,
|
||||||
|
checksRepository: IChecksRepository,
|
||||||
|
incidentsRepository: IIncidentsRepository,
|
||||||
|
geoChecksRepository: IGeoChecksRepository,
|
||||||
|
reactorDispatcher: IReactorDispatcher,
|
||||||
|
checkPipeline: ICheckPipeline,
|
||||||
|
geoCheckPipeline: ICheckPipeline
|
||||||
|
) {
|
||||||
|
this.logger = logger;
|
||||||
|
this.checkService = checkService;
|
||||||
|
this.settingsService = settingsService;
|
||||||
|
this.monitorsRepository = monitorsRepository;
|
||||||
|
this.teamsRepository = teamsRepository;
|
||||||
|
this.monitorStatsRepository = monitorStatsRepository;
|
||||||
|
this.checksRepository = checksRepository;
|
||||||
|
this.incidentsRepository = incidentsRepository;
|
||||||
|
this.geoChecksRepository = geoChecksRepository;
|
||||||
|
this.reactorDispatcher = reactorDispatcher;
|
||||||
|
this.checkPipeline = checkPipeline;
|
||||||
|
this.geoCheckPipeline = geoCheckPipeline;
|
||||||
|
}
|
||||||
|
|
||||||
|
get serviceName() {
|
||||||
|
return WorkerHelper.SERVICE_NAME;
|
||||||
|
}
|
||||||
|
|
||||||
|
getHeartbeatJob = () => {
|
||||||
|
return async (monitor: Monitor) => {
|
||||||
|
try {
|
||||||
|
const evaluation = await this.checkPipeline.run(monitor);
|
||||||
|
if (evaluation) await this.reactorDispatcher.dispatch(evaluation);
|
||||||
|
} catch (error: unknown) {
|
||||||
|
this.logger.warn({
|
||||||
|
message: error instanceof Error ? error.message : "Unknown error",
|
||||||
|
service: SERVICE_NAME,
|
||||||
|
method: "getMonitorJob",
|
||||||
|
stack: error instanceof Error ? error.stack : undefined,
|
||||||
|
});
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
getHeartbeatGeoJob = () => {
|
||||||
|
return async (monitor: Monitor) => {
|
||||||
|
try {
|
||||||
|
await this.geoCheckPipeline.run(monitor);
|
||||||
|
} catch (error: unknown) {
|
||||||
|
this.logger.error({
|
||||||
|
message: error instanceof Error ? error.message : "Unknown error",
|
||||||
|
service: SERVICE_NAME,
|
||||||
|
method: "getHeartbeatGeoJob",
|
||||||
|
stack: error instanceof Error ? error.stack : undefined,
|
||||||
|
});
|
||||||
|
// Don't throw - geo check failures shouldn't crash the job scheduler
|
||||||
|
}
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
getCleanupOrphanedJob = () => {
|
||||||
|
return async () => {
|
||||||
|
try {
|
||||||
|
this.logger.info({
|
||||||
|
message: "Starting cleanup of orphaned data",
|
||||||
|
service: SERVICE_NAME,
|
||||||
|
method: "getCleanupOrphanedJob",
|
||||||
|
});
|
||||||
|
|
||||||
|
// Get all valid team IDs
|
||||||
|
const validTeamIds = await this.teamsRepository.findAllTeamIds();
|
||||||
|
this.logger.debug({
|
||||||
|
message: `Found ${validTeamIds.length} valid teams`,
|
||||||
|
service: SERVICE_NAME,
|
||||||
|
method: "getCleanupOrphanedJob",
|
||||||
|
});
|
||||||
|
|
||||||
|
// Remove orphaned monitors (monitors without a valid team)
|
||||||
|
const deletedMonitorCount = await this.monitorsRepository.deleteByTeamIdsNotIn(validTeamIds);
|
||||||
|
if (deletedMonitorCount > 0) {
|
||||||
|
this.logger.info({
|
||||||
|
message: `Deleted ${deletedMonitorCount} orphaned monitors`,
|
||||||
|
service: SERVICE_NAME,
|
||||||
|
method: "getCleanupOrphanedJob",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove orphaned monitorStats (stats without a valid monitor)
|
||||||
|
const allMonitorIds = await this.monitorsRepository.findAllMonitorIds();
|
||||||
|
this.logger.debug({
|
||||||
|
message: `Found ${allMonitorIds.length} valid monitors`,
|
||||||
|
service: SERVICE_NAME,
|
||||||
|
method: "getCleanupOrphanedJob",
|
||||||
|
});
|
||||||
|
|
||||||
|
const deletedStatsCount = await this.monitorStatsRepository.deleteByMonitorIdsNotIn(allMonitorIds);
|
||||||
|
if (deletedStatsCount > 0) {
|
||||||
|
this.logger.info({
|
||||||
|
message: `Deleted ${deletedStatsCount} orphaned monitor stats`,
|
||||||
|
service: SERVICE_NAME,
|
||||||
|
method: "getCleanupOrphanedJob",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove orphaned checks
|
||||||
|
const deletedChecksCount = await this.checksRepository.deleteByMonitorIdsNotIn(allMonitorIds);
|
||||||
|
if (deletedChecksCount > 0) {
|
||||||
|
this.logger.info({
|
||||||
|
message: `Deleted ${deletedChecksCount} orphaned checks`,
|
||||||
|
service: SERVICE_NAME,
|
||||||
|
method: "getCleanupOrphanedJob",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove orphaned geo checks
|
||||||
|
const deletedGeoChecksCount = await this.geoChecksRepository.deleteByMonitorIdsNotIn(allMonitorIds);
|
||||||
|
if (deletedGeoChecksCount > 0) {
|
||||||
|
this.logger.info({
|
||||||
|
message: `Deleted ${deletedGeoChecksCount} orphaned geo checks`,
|
||||||
|
service: SERVICE_NAME,
|
||||||
|
method: "getCleanupOrphanedJob",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
this.logger.info({
|
||||||
|
message: "Cleanup of orphaned data completed",
|
||||||
|
service: SERVICE_NAME,
|
||||||
|
method: "getCleanupOrphanedJob",
|
||||||
|
});
|
||||||
|
} catch (error: unknown) {
|
||||||
|
this.logger.warn({
|
||||||
|
message: error instanceof Error ? error.message : "Unknown error",
|
||||||
|
service: SERVICE_NAME,
|
||||||
|
method: "getCleanupOrphanedJob",
|
||||||
|
stack: error instanceof Error ? error.stack : undefined,
|
||||||
|
});
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
getCleanupRetentionJob = () => {
|
||||||
|
return async () => {
|
||||||
|
try {
|
||||||
|
const settings = await this.settingsService.getDBSettings();
|
||||||
|
|
||||||
|
const checkTTL = settings.checkTTL; // Check TTL is in DAYS, not MS
|
||||||
|
|
||||||
|
if (checkTTL === CHECK_TTL_SENTINEL) {
|
||||||
|
this.logger.info({
|
||||||
|
message: `Check TTL is set to unlimited, skipping cleanup`,
|
||||||
|
service: SERVICE_NAME,
|
||||||
|
method: "getCleanupRetentionJob",
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const checkTTLInMs = checkTTL * 24 * 60 * 60 * 1000;
|
||||||
|
const cutoffDate = new Date(Date.now() - checkTTLInMs);
|
||||||
|
const deleteCount = await this.checkService.deleteOlderThan(cutoffDate);
|
||||||
|
this.logger.info({
|
||||||
|
message: `Deleted ${deleteCount} checks older than ${cutoffDate.toISOString()}`,
|
||||||
|
service: SERVICE_NAME,
|
||||||
|
method: "getCleanupRetentionJob",
|
||||||
|
});
|
||||||
|
} catch (error: unknown) {
|
||||||
|
this.logger.error({
|
||||||
|
message: error instanceof Error ? error.message : "Unknown error",
|
||||||
|
service: SERVICE_NAME,
|
||||||
|
method: "getCleanupRetentionJob",
|
||||||
|
stack: error instanceof Error ? error.stack : undefined,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
};
|
||||||
|
}
|
||||||
+21
-10
@@ -1,7 +1,18 @@
|
|||||||
import { Monitor } from "@/domain/monitors/monitor.types.js";
|
import { Monitor } from "@/domain/monitors/monitor.types.js";
|
||||||
import { QueueWorker } from "@/domain/queue-workers/queue-worker.type.js";
|
import { QueueWorker } from "@/domain/queue-workers/queue-worker.type.js";
|
||||||
|
import { Check } from "@/domain/checks/check.type.js";
|
||||||
|
import { MonitorPayloadMap, MonitorStatusResponse, StatusChangeResult } from "@/types/network.js";
|
||||||
|
import { MonitorActionDecision } from "@/worker/worker.helper.js";
|
||||||
|
|
||||||
export type QueueJobFailure = {
|
export type MonitorEvaluation = {
|
||||||
|
monitor: Monitor;
|
||||||
|
status: MonitorStatusResponse<MonitorPayloadMap[keyof MonitorPayloadMap]>; // raw result from networkService.requestStatus
|
||||||
|
check: Check;
|
||||||
|
statusChange: StatusChangeResult; // from statusService.updateMonitorStatus
|
||||||
|
decision: MonitorActionDecision; // from MonitorStatusPolicy
|
||||||
|
};
|
||||||
|
|
||||||
|
export type WorkerJobFailure = {
|
||||||
monitorId: string | number;
|
monitorId: string | number;
|
||||||
monitorUrl: string | null;
|
monitorUrl: string | null;
|
||||||
monitorType: string | null;
|
monitorType: string | null;
|
||||||
@@ -10,17 +21,17 @@ export type QueueJobFailure = {
|
|||||||
failReason: string | null;
|
failReason: string | null;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type QueueMetrics = {
|
export type WorkerMetrics = {
|
||||||
jobs: number;
|
jobs: number;
|
||||||
activeJobs: number;
|
activeJobs: number;
|
||||||
failingJobs: number;
|
failingJobs: number;
|
||||||
jobsWithFailures: QueueJobFailure[];
|
jobsWithFailures: WorkerJobFailure[];
|
||||||
totalRuns: number;
|
totalRuns: number;
|
||||||
totalFailures: number;
|
totalFailures: number;
|
||||||
workers: QueueWorker[];
|
workers: QueueWorker[];
|
||||||
};
|
};
|
||||||
|
|
||||||
export type QueueJobSummary = {
|
export type WorkerJobSummary = {
|
||||||
monitorId: string | number;
|
monitorId: string | number;
|
||||||
monitorUrl: string | null;
|
monitorUrl: string | null;
|
||||||
monitorType: string | null;
|
monitorType: string | null;
|
||||||
@@ -41,17 +52,17 @@ export type QueueJobSummary = {
|
|||||||
repeat: number | null;
|
repeat: number | null;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type QueueJobsPagination = {
|
export type WorkerJobsPagination = {
|
||||||
page?: number;
|
page?: number;
|
||||||
rowsPerPage?: number;
|
rowsPerPage?: number;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type QueueJobsPage = {
|
export type WorkerJobsPage = {
|
||||||
jobs: QueueJobSummary[];
|
jobs: WorkerJobSummary[];
|
||||||
count: number;
|
count: number;
|
||||||
};
|
};
|
||||||
|
|
||||||
export interface IJobQueue {
|
export interface IWorker {
|
||||||
readonly serviceName: string;
|
readonly serviceName: string;
|
||||||
init(): Promise<boolean>;
|
init(): Promise<boolean>;
|
||||||
addJob(monitorId: string, monitor: Monitor): Promise<void>;
|
addJob(monitorId: string, monitor: Monitor): Promise<void>;
|
||||||
@@ -60,7 +71,7 @@ export interface IJobQueue {
|
|||||||
resumeJob(monitor: Monitor): Promise<void>;
|
resumeJob(monitor: Monitor): Promise<void>;
|
||||||
updateJob(monitor: Monitor): Promise<void>;
|
updateJob(monitor: Monitor): Promise<void>;
|
||||||
shutdown(): Promise<void>;
|
shutdown(): Promise<void>;
|
||||||
getMetrics(): Promise<QueueMetrics>;
|
getMetrics(): Promise<WorkerMetrics>;
|
||||||
getJobs(pagination: QueueJobsPagination): Promise<QueueJobsPage>;
|
getJobs(pagination: WorkerJobsPagination): Promise<WorkerJobsPage>;
|
||||||
flushQueues(): Promise<{ success: boolean }>;
|
flushQueues(): Promise<{ success: boolean }>;
|
||||||
}
|
}
|
||||||
+10
-10
@@ -2,9 +2,9 @@ import { IMonitorsRepository } from "@/domain/monitors/monitor.repository.interf
|
|||||||
import { IQueueWorkersRepository } from "@/domain/queue-workers/queue-worker.repository.interface.js";
|
import { IQueueWorkersRepository } from "@/domain/queue-workers/queue-worker.repository.interface.js";
|
||||||
import { ILogger } from "@/utils/logger.js";
|
import { ILogger } from "@/utils/logger.js";
|
||||||
import { MongoStore, Scheduler, IJob, AddJobInput } from "less-simple-scheduler";
|
import { MongoStore, Scheduler, IJob, AddJobInput } from "less-simple-scheduler";
|
||||||
import { IQueueHelper } from "@/service/job-queues/job-queue.helper.js";
|
|
||||||
import { Monitor, supportsGeoCheck } from "@/domain/monitors/monitor.types.js";
|
import { Monitor, supportsGeoCheck } from "@/domain/monitors/monitor.types.js";
|
||||||
import { type QueueMetrics, type QueueJobsPagination, type QueueJobsPage, IJobQueue } from "@/service/job-queues/job-queue.interface.js";
|
import { IWorkerHelper } from "@/worker/worker.helper.js";
|
||||||
|
import { type WorkerMetrics, type WorkerJobsPagination, type WorkerJobsPage, IWorker } from "@/worker/worker.interface.js";
|
||||||
import { type QueueMode } from "@/domain/app-settings/app-settings.type.js";
|
import { type QueueMode } from "@/domain/app-settings/app-settings.type.js";
|
||||||
import { EnvConfig } from "@/domain/app-settings/app-settings.service.js";
|
import { EnvConfig } from "@/domain/app-settings/app-settings.service.js";
|
||||||
const SERVICE_NAME = "JobQueue";
|
const SERVICE_NAME = "JobQueue";
|
||||||
@@ -17,11 +17,11 @@ type MonitorJob = Omit<IJob, "data"> & { data: Monitor };
|
|||||||
const MONITOR_TEMPLATES = new Set<string>(["monitor-job", "geo-check-job"]);
|
const MONITOR_TEMPLATES = new Set<string>(["monitor-job", "geo-check-job"]);
|
||||||
const isMonitorJob = (job: IJob): job is MonitorJob => MONITOR_TEMPLATES.has(job.template);
|
const isMonitorJob = (job: IJob): job is MonitorJob => MONITOR_TEMPLATES.has(job.template);
|
||||||
|
|
||||||
export class LessSimpleQueue implements IJobQueue {
|
export class LessSimpleWorker implements IWorker {
|
||||||
static SERVICE_NAME = SERVICE_NAME;
|
static SERVICE_NAME = SERVICE_NAME;
|
||||||
|
|
||||||
private logger: ILogger;
|
private logger: ILogger;
|
||||||
private helper: IQueueHelper;
|
private helper: IWorkerHelper;
|
||||||
private monitorsRepository: IMonitorsRepository;
|
private monitorsRepository: IMonitorsRepository;
|
||||||
private workersRepository: IQueueWorkersRepository;
|
private workersRepository: IQueueWorkersRepository;
|
||||||
private readonly scheduler: Scheduler;
|
private readonly scheduler: Scheduler;
|
||||||
@@ -30,7 +30,7 @@ export class LessSimpleQueue implements IJobQueue {
|
|||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
logger: ILogger,
|
logger: ILogger,
|
||||||
helper: IQueueHelper,
|
helper: IWorkerHelper,
|
||||||
monitorsRepository: IMonitorsRepository,
|
monitorsRepository: IMonitorsRepository,
|
||||||
workersRepository: IQueueWorkersRepository,
|
workersRepository: IQueueWorkersRepository,
|
||||||
scheduler: Scheduler,
|
scheduler: Scheduler,
|
||||||
@@ -46,7 +46,7 @@ export class LessSimpleQueue implements IJobQueue {
|
|||||||
}
|
}
|
||||||
|
|
||||||
get serviceName() {
|
get serviceName() {
|
||||||
return LessSimpleQueue.SERVICE_NAME;
|
return LessSimpleWorker.SERVICE_NAME;
|
||||||
}
|
}
|
||||||
|
|
||||||
private registerListeners = () => {
|
private registerListeners = () => {
|
||||||
@@ -126,7 +126,7 @@ export class LessSimpleQueue implements IJobQueue {
|
|||||||
|
|
||||||
static async create(
|
static async create(
|
||||||
logger: ILogger,
|
logger: ILogger,
|
||||||
helper: IQueueHelper,
|
helper: IWorkerHelper,
|
||||||
monitorsRepository: IMonitorsRepository,
|
monitorsRepository: IMonitorsRepository,
|
||||||
workersRepository: IQueueWorkersRepository,
|
workersRepository: IQueueWorkersRepository,
|
||||||
envSettings: EnvConfig,
|
envSettings: EnvConfig,
|
||||||
@@ -139,7 +139,7 @@ export class LessSimpleQueue implements IJobQueue {
|
|||||||
concurrency: envSettings.queueMode === "primary" ? 0 : 50,
|
concurrency: envSettings.queueMode === "primary" ? 0 : 50,
|
||||||
processEvery: 100,
|
processEvery: 100,
|
||||||
});
|
});
|
||||||
const instance = new LessSimpleQueue(logger, helper, monitorsRepository, workersRepository, scheduler, queueMode);
|
const instance = new LessSimpleWorker(logger, helper, monitorsRepository, workersRepository, scheduler, queueMode);
|
||||||
await instance.init(queueMode);
|
await instance.init(queueMode);
|
||||||
|
|
||||||
return instance;
|
return instance;
|
||||||
@@ -350,7 +350,7 @@ export class LessSimpleQueue implements IJobQueue {
|
|||||||
this.scheduler.stop();
|
this.scheduler.stop();
|
||||||
};
|
};
|
||||||
|
|
||||||
getMetrics = async (): Promise<QueueMetrics> => {
|
getMetrics = async (): Promise<WorkerMetrics> => {
|
||||||
const stats = await this.scheduler.getStats();
|
const stats = await this.scheduler.getStats();
|
||||||
|
|
||||||
const jobsWithFailures = stats.jobsWithFailures.map((failure) => {
|
const jobsWithFailures = stats.jobsWithFailures.map((failure) => {
|
||||||
@@ -379,7 +379,7 @@ export class LessSimpleQueue implements IJobQueue {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
getJobs = async ({ page = 0, rowsPerPage = 0 }: QueueJobsPagination): Promise<QueueJobsPage> => {
|
getJobs = async ({ page = 0, rowsPerPage = 0 }: WorkerJobsPagination): Promise<WorkerJobsPage> => {
|
||||||
const listOptions = rowsPerPage > 0 ? { skip: Math.max(page, 0) * rowsPerPage, limit: rowsPerPage } : undefined;
|
const listOptions = rowsPerPage > 0 ? { skip: Math.max(page, 0) * rowsPerPage, limit: rowsPerPage } : undefined;
|
||||||
const [jobs, count] = await Promise.all([this.scheduler.getJobs(listOptions), this.scheduler.countJobs()]);
|
const [jobs, count] = await Promise.all([this.scheduler.getJobs(listOptions), this.scheduler.countJobs()]);
|
||||||
const mapped = jobs.map((job) => {
|
const mapped = jobs.map((job) => {
|
||||||
@@ -0,0 +1,46 @@
|
|||||||
|
import { StatusChangeResult } from "@/types/network.js";
|
||||||
|
import { MonitorActionDecision } from "@/worker/worker.helper.js";
|
||||||
|
|
||||||
|
export interface IMonitorStatusPolicy {
|
||||||
|
evaluate(statusChange: StatusChangeResult): MonitorActionDecision;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class MonitorStatusPolicy implements IMonitorStatusPolicy {
|
||||||
|
evaluate = (statusChangeResult: StatusChangeResult): MonitorActionDecision => {
|
||||||
|
const { monitor, statusChanged, prevStatus } = statusChangeResult;
|
||||||
|
|
||||||
|
// Initialize result
|
||||||
|
const decision: MonitorActionDecision = {
|
||||||
|
shouldCreateIncident: false,
|
||||||
|
shouldResolveIncident: false,
|
||||||
|
shouldSendNotification: false,
|
||||||
|
incidentReason: null,
|
||||||
|
notificationReason: null,
|
||||||
|
};
|
||||||
|
|
||||||
|
if (!statusChanged) {
|
||||||
|
return decision;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (monitor.status === "down") {
|
||||||
|
// Monitor went down (unreachable)
|
||||||
|
decision.shouldCreateIncident = true;
|
||||||
|
decision.shouldSendNotification = true;
|
||||||
|
decision.incidentReason = "status_down";
|
||||||
|
decision.notificationReason = "status_change";
|
||||||
|
} else if (monitor.status === "breached") {
|
||||||
|
// Hardware monitor exceeded thresholds
|
||||||
|
decision.shouldCreateIncident = true;
|
||||||
|
decision.shouldSendNotification = true;
|
||||||
|
decision.incidentReason = "threshold_breach";
|
||||||
|
decision.notificationReason = "threshold_breach";
|
||||||
|
} else if (monitor.status === "up" && (prevStatus === "down" || prevStatus === "breached")) {
|
||||||
|
// Monitor recovered from down or breached state
|
||||||
|
decision.shouldResolveIncident = true;
|
||||||
|
decision.shouldSendNotification = true;
|
||||||
|
decision.notificationReason = "status_change";
|
||||||
|
}
|
||||||
|
|
||||||
|
return decision;
|
||||||
|
};
|
||||||
|
}
|
||||||
+7
-7
@@ -1,25 +1,25 @@
|
|||||||
import { IMonitorsRepository } from "@/domain/monitors/monitor.repository.interface.js";
|
import { IMonitorsRepository } from "@/domain/monitors/monitor.repository.interface.js";
|
||||||
import { ILogger } from "@/utils/logger.js";
|
import { ILogger } from "@/utils/logger.js";
|
||||||
import Scheduler from "super-simple-scheduler";
|
import Scheduler from "super-simple-scheduler";
|
||||||
import { IQueueHelper } from "@/service/job-queues/job-queue.helper.js";
|
|
||||||
import { Monitor, MonitorType, supportsGeoCheck } from "@/domain/monitors/monitor.types.js";
|
import { Monitor, MonitorType, supportsGeoCheck } from "@/domain/monitors/monitor.types.js";
|
||||||
import { hostname } from "node:os";
|
import { hostname } from "node:os";
|
||||||
import { randomUUID } from "node:crypto";
|
import { randomUUID } from "node:crypto";
|
||||||
import { QueueMode } from "@/domain/app-settings/app-settings.type.js";
|
import { QueueMode } from "@/domain/app-settings/app-settings.type.js";
|
||||||
import { IJobQueue, type QueueJobSummary } from "@/service/job-queues/job-queue.interface.js";
|
import { IWorker, WorkerJobSummary } from "@/worker/worker.interface.js";
|
||||||
|
import { IWorkerHelper } from "@/worker/worker.helper.js";
|
||||||
|
|
||||||
const SERVICE_NAME = "JobQueue";
|
const SERVICE_NAME = "JobQueue";
|
||||||
|
|
||||||
export class SuperSimpleQueue implements IJobQueue {
|
export class SuperSimpleQueue implements IWorker {
|
||||||
static SERVICE_NAME = SERVICE_NAME;
|
static SERVICE_NAME = SERVICE_NAME;
|
||||||
|
|
||||||
private logger: ILogger;
|
private logger: ILogger;
|
||||||
private helper: IQueueHelper;
|
private helper: IWorkerHelper;
|
||||||
private workerId: string;
|
private workerId: string;
|
||||||
private monitorsRepository: IMonitorsRepository;
|
private monitorsRepository: IMonitorsRepository;
|
||||||
private readonly scheduler: Scheduler;
|
private readonly scheduler: Scheduler;
|
||||||
|
|
||||||
constructor(logger: ILogger, helper: IQueueHelper, monitorsRepository: IMonitorsRepository, scheduler: Scheduler) {
|
constructor(logger: ILogger, helper: IWorkerHelper, monitorsRepository: IMonitorsRepository, scheduler: Scheduler) {
|
||||||
this.logger = logger;
|
this.logger = logger;
|
||||||
this.helper = helper;
|
this.helper = helper;
|
||||||
this.monitorsRepository = monitorsRepository;
|
this.monitorsRepository = monitorsRepository;
|
||||||
@@ -100,7 +100,7 @@ export class SuperSimpleQueue implements IJobQueue {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
static async create(logger: ILogger, helper: IQueueHelper, monitorsRepository: IMonitorsRepository) {
|
static async create(logger: ILogger, helper: IWorkerHelper, monitorsRepository: IMonitorsRepository) {
|
||||||
const scheduler = new Scheduler({
|
const scheduler = new Scheduler({
|
||||||
concurrency: 50,
|
concurrency: 50,
|
||||||
// storeType: "mongo",
|
// storeType: "mongo",
|
||||||
@@ -341,7 +341,7 @@ export class SuperSimpleQueue implements IJobQueue {
|
|||||||
return metrics;
|
return metrics;
|
||||||
};
|
};
|
||||||
|
|
||||||
getJobs = async ({ page = 0, rowsPerPage = 0 }: { page?: number; rowsPerPage?: number }): Promise<{ jobs: QueueJobSummary[]; count: number }> => {
|
getJobs = async ({ page = 0, rowsPerPage = 0 }: { page?: number; rowsPerPage?: number }): Promise<{ jobs: WorkerJobSummary[]; count: number }> => {
|
||||||
const jobs = await this.scheduler.getJobs();
|
const jobs = await this.scheduler.getJobs();
|
||||||
const mapped = jobs.map((job) => {
|
const mapped = jobs.map((job) => {
|
||||||
return {
|
return {
|
||||||
@@ -1,5 +1,10 @@
|
|||||||
import { jest } from "@jest/globals";
|
import { jest } from "@jest/globals";
|
||||||
import { QueueHelper } from "../../src/service/job-queues/job-queue.helper.ts";
|
import { WorkerHelper } from "../../src/worker/worker.helper.ts";
|
||||||
|
import { MonitorStatusPolicy } from "../../src/worker/worker.monitor-status-policy.ts";
|
||||||
|
import { CheckPipeline, GeoChecksPipeline } from "../../src/worker/worker.check-pipeline.ts";
|
||||||
|
import { NotificationReactor } from "../../src/worker/reactors/reactor.notification.ts";
|
||||||
|
import { IncidentReactor } from "../../src/worker/reactors/reactor.incident.ts";
|
||||||
|
import { ReactorDispatcher } from "../../src/worker/reactors/reactor.dispatcher.ts";
|
||||||
import { StatusService } from "../../src/service/statusService.ts";
|
import { StatusService } from "../../src/service/statusService.ts";
|
||||||
import { IncidentService } from "../../src/domain/incidents/incident.service.ts";
|
import { IncidentService } from "../../src/domain/incidents/incident.service.ts";
|
||||||
import { InMemoryMonitorsRepository } from "./InMemoryMonitorsRepository.ts";
|
import { InMemoryMonitorsRepository } from "./InMemoryMonitorsRepository.ts";
|
||||||
@@ -142,24 +147,37 @@ export function createHeartbeatTestHarness(): HeartbeatTestHarness {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const maintenanceWindowsRepo = { findByMonitorId: jest.fn().mockResolvedValue([]) };
|
const maintenanceWindowsRepo = { findByMonitorId: jest.fn().mockResolvedValue([]) };
|
||||||
|
const geoChecksService = { buildGeoCheck: jest.fn() };
|
||||||
|
|
||||||
const helper = new QueueHelper(
|
const checkPipeline = new CheckPipeline(
|
||||||
logger,
|
monitorsRepo as any,
|
||||||
|
maintenanceWindowsRepo as any,
|
||||||
|
checkService as any,
|
||||||
networkService as any,
|
networkService as any,
|
||||||
|
bufferStub as any,
|
||||||
|
new MonitorStatusPolicy(),
|
||||||
statusService as any,
|
statusService as any,
|
||||||
notificationsService as any,
|
logger
|
||||||
|
);
|
||||||
|
const geoCheckPipeline = new GeoChecksPipeline(maintenanceWindowsRepo as any, geoChecksService as any, bufferStub as any, logger);
|
||||||
|
|
||||||
|
const notificationReactor = new NotificationReactor(notificationsService as any);
|
||||||
|
const incidentReactor = new IncidentReactor(incidentService as any);
|
||||||
|
const reactorDispatcher = new ReactorDispatcher(logger, [notificationReactor, incidentReactor]);
|
||||||
|
|
||||||
|
const helper = new WorkerHelper(
|
||||||
|
logger,
|
||||||
checkService as any,
|
checkService as any,
|
||||||
{ getDBSettings: jest.fn() } as any,
|
{ getDBSettings: jest.fn() } as any,
|
||||||
bufferStub as any,
|
|
||||||
incidentService as any,
|
|
||||||
maintenanceWindowsRepo as any,
|
|
||||||
monitorsRepo as any,
|
monitorsRepo as any,
|
||||||
{ findAllTeamIds: jest.fn() } as any,
|
{ findAllTeamIds: jest.fn() } as any,
|
||||||
createStubMonitorStatsRepo() as any,
|
createStubMonitorStatsRepo() as any,
|
||||||
createStubChecksRepo() as any,
|
createStubChecksRepo() as any,
|
||||||
incidentsRepo as any,
|
incidentsRepo as any,
|
||||||
{ buildGeoCheck: jest.fn() } as any,
|
{ deleteByMonitorIdsNotIn: jest.fn() } as any,
|
||||||
{ deleteByMonitorIdsNotIn: jest.fn() } as any
|
reactorDispatcher,
|
||||||
|
checkPipeline,
|
||||||
|
geoCheckPipeline
|
||||||
);
|
);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import type { IMonitorsRepository } from "../../src/domain/monitors/monitor.repo
|
|||||||
import type { IUsersRepository } from "../../src/domain/users/user.repository.interface.ts";
|
import type { IUsersRepository } from "../../src/domain/users/user.repository.interface.ts";
|
||||||
import type { INotificationMessageBuilder } from "../../src/domain/notifications/notification.message-builder.ts";
|
import type { INotificationMessageBuilder } from "../../src/domain/notifications/notification.message-builder.ts";
|
||||||
import type { Monitor } from "../../src/domain/monitors/monitor.types.ts";
|
import type { Monitor } from "../../src/domain/monitors/monitor.types.ts";
|
||||||
import type { MonitorActionDecision } from "../../src/service/job-queues/job-queue.helper.ts";
|
import type { MonitorActionDecision } from "../../src/worker/worker.helper.ts";
|
||||||
|
|
||||||
// ── Helpers ──────────────────────────────────────────────────────────────────
|
// ── Helpers ──────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,38 @@
|
|||||||
|
import { describe, expect, it, jest } from "@jest/globals";
|
||||||
|
import type { NextFunction, Request, Response } from "express";
|
||||||
|
import { MulterError } from "multer";
|
||||||
|
import { handleErrors } from "../../../src/api/middleware/handleErrors.ts";
|
||||||
|
import { AppError } from "../../../src/utils/AppError.ts";
|
||||||
|
|
||||||
|
const makeRes = (): Response => {
|
||||||
|
const res = {} as Response;
|
||||||
|
res.status = jest.fn().mockReturnValue(res) as unknown as Response["status"];
|
||||||
|
res.json = jest.fn().mockReturnValue(res) as unknown as Response["json"];
|
||||||
|
return res;
|
||||||
|
};
|
||||||
|
|
||||||
|
describe("handleErrors", () => {
|
||||||
|
it("maps a multer file-size violation to 413 Payload Too Large", () => {
|
||||||
|
const res = makeRes();
|
||||||
|
handleErrors(new MulterError("LIMIT_FILE_SIZE", "profileImage"), {} as Request, res, (() => {}) as NextFunction);
|
||||||
|
expect(res.status).toHaveBeenCalledWith(413);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("maps other multer errors to 400", () => {
|
||||||
|
const res = makeRes();
|
||||||
|
handleErrors(new MulterError("LIMIT_UNEXPECTED_FILE", "profileImage"), {} as Request, res, (() => {}) as NextFunction);
|
||||||
|
expect(res.status).toHaveBeenCalledWith(400);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("preserves AppError status codes", () => {
|
||||||
|
const res = makeRes();
|
||||||
|
handleErrors(new AppError({ status: 415, message: "bad image" }), {} as Request, res, (() => {}) as NextFunction);
|
||||||
|
expect(res.status).toHaveBeenCalledWith(415);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("falls back to 500 for unknown errors", () => {
|
||||||
|
const res = makeRes();
|
||||||
|
handleErrors(new Error("boom"), {} as Request, res, (() => {}) as NextFunction);
|
||||||
|
expect(res.status).toHaveBeenCalledWith(500);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
import { describe, expect, it } from "@jest/globals";
|
||||||
|
import type { Request } from "express";
|
||||||
|
import { imageUpload } from "../../../src/api/middleware/upload.ts";
|
||||||
|
import { MAX_IMAGE_SIZE_BYTES } from "../../../src/types/upload.ts";
|
||||||
|
import { AppError } from "../../../src/utils/AppError.ts";
|
||||||
|
|
||||||
|
// Reaches into the multer instance config to exercise the fileFilter/limits in isolation.
|
||||||
|
// Guards against regressing GHSA-9xvg-x28f-m78m (pre-auth DoS via unbounded uploads).
|
||||||
|
const config = (imageUpload as unknown as { fileFilter: Function; limits: { fileSize: number; files: number } });
|
||||||
|
|
||||||
|
const makeFile = (mimetype: string): Express.Multer.File =>
|
||||||
|
({ fieldname: "profileImage", originalname: "x", mimetype }) as Express.Multer.File;
|
||||||
|
|
||||||
|
const runFilter = (mimetype: string): Promise<{ error: unknown; accepted: unknown }> =>
|
||||||
|
new Promise((resolve) => {
|
||||||
|
config.fileFilter({} as Request, makeFile(mimetype), (error: unknown, accepted: unknown) => resolve({ error, accepted }));
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("imageUpload middleware", () => {
|
||||||
|
it("enforces a file size limit so large uploads cannot exhaust memory", () => {
|
||||||
|
expect(config.limits.fileSize).toBe(MAX_IMAGE_SIZE_BYTES);
|
||||||
|
expect(config.limits.fileSize).toBeLessThanOrEqual(3 * 1024 * 1024);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("limits a request to a single file", () => {
|
||||||
|
expect(config.limits.files).toBe(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("accepts allowed image mime types", async () => {
|
||||||
|
for (const mimetype of ["image/jpeg", "image/png", "image/jpg"]) {
|
||||||
|
const { error, accepted } = await runFilter(mimetype);
|
||||||
|
expect(error).toBeNull();
|
||||||
|
expect(accepted).toBe(true);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
it("rejects non-image uploads with a 415 AppError", async () => {
|
||||||
|
const { error } = await runFilter("application/octet-stream");
|
||||||
|
expect(error).toBeInstanceOf(AppError);
|
||||||
|
expect((error as AppError).status).toBe(415);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,217 @@
|
|||||||
|
import { describe, expect, it, jest } from "@jest/globals";
|
||||||
|
import { CheckPipeline, GeoChecksPipeline } from "../../../src/worker/worker.check-pipeline.ts";
|
||||||
|
import { MonitorStatusPolicy } from "../../../src/worker/worker.monitor-status-policy.ts";
|
||||||
|
import type { Monitor } from "../../../src/domain/monitors/monitor.types.ts";
|
||||||
|
import { createMockLogger } from "../../helpers/createMockLogger.ts";
|
||||||
|
|
||||||
|
const makeMonitor = (overrides?: Partial<Monitor>): Monitor =>
|
||||||
|
({
|
||||||
|
id: "m1",
|
||||||
|
teamId: "team",
|
||||||
|
type: "http",
|
||||||
|
interval: 60000,
|
||||||
|
status: "up",
|
||||||
|
geoCheckEnabled: false,
|
||||||
|
geoCheckLocations: [],
|
||||||
|
...overrides,
|
||||||
|
}) as Monitor;
|
||||||
|
|
||||||
|
const activeWindow = () => {
|
||||||
|
const now = Date.now();
|
||||||
|
return { active: true, start: new Date(now - 1000).toISOString(), end: new Date(now + 1000).toISOString(), repeat: 0 };
|
||||||
|
};
|
||||||
|
|
||||||
|
// ── CheckPipeline ────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
const createCheckPipeline = (overrides?: Record<string, any>) => {
|
||||||
|
const defaults = {
|
||||||
|
logger: createMockLogger(),
|
||||||
|
networkService: { requestStatus: jest.fn().mockResolvedValue({ monitorId: "m1", status: true, code: 200, message: "OK" }) },
|
||||||
|
statusService: {
|
||||||
|
updateMonitorStatus: jest.fn().mockResolvedValue({ monitor: { id: "m1", status: "up" }, statusChanged: false, prevStatus: "up", code: 200 }),
|
||||||
|
},
|
||||||
|
checkService: { buildCheck: jest.fn().mockReturnValue({ id: "check-1" }) },
|
||||||
|
buffer: { addToBuffer: jest.fn() },
|
||||||
|
monitorsRepository: { updateById: jest.fn().mockResolvedValue({}) },
|
||||||
|
maintenanceWindowsRepository: { findByMonitorId: jest.fn().mockResolvedValue([]) },
|
||||||
|
monitorStatusPolicy: new MonitorStatusPolicy(),
|
||||||
|
...overrides,
|
||||||
|
};
|
||||||
|
const pipeline = new CheckPipeline(
|
||||||
|
defaults.monitorsRepository as any,
|
||||||
|
defaults.maintenanceWindowsRepository as any,
|
||||||
|
defaults.checkService as any,
|
||||||
|
defaults.networkService as any,
|
||||||
|
defaults.buffer as any,
|
||||||
|
defaults.monitorStatusPolicy as any,
|
||||||
|
defaults.statusService as any,
|
||||||
|
defaults.logger as any
|
||||||
|
);
|
||||||
|
return { pipeline, defaults };
|
||||||
|
};
|
||||||
|
|
||||||
|
describe("CheckPipeline", () => {
|
||||||
|
it("throws when monitor id is missing", async () => {
|
||||||
|
const { pipeline } = createCheckPipeline();
|
||||||
|
await expect(pipeline.run({} as Monitor)).rejects.toThrow("No monitor id");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("skips and sets maintenance status when in maintenance window", async () => {
|
||||||
|
const { pipeline, defaults } = createCheckPipeline({
|
||||||
|
maintenanceWindowsRepository: { findByMonitorId: jest.fn().mockResolvedValue([activeWindow()]) },
|
||||||
|
});
|
||||||
|
|
||||||
|
const result = await pipeline.run(makeMonitor({ status: "up" }));
|
||||||
|
|
||||||
|
expect(result).toBeNull();
|
||||||
|
expect(defaults.networkService.requestStatus).not.toHaveBeenCalled();
|
||||||
|
expect(defaults.monitorsRepository.updateById).toHaveBeenCalledWith("m1", "team", { status: "maintenance" });
|
||||||
|
});
|
||||||
|
|
||||||
|
it("does not re-write status when already in maintenance", async () => {
|
||||||
|
const { pipeline, defaults } = createCheckPipeline({
|
||||||
|
maintenanceWindowsRepository: { findByMonitorId: jest.fn().mockResolvedValue([activeWindow()]) },
|
||||||
|
});
|
||||||
|
|
||||||
|
await pipeline.run(makeMonitor({ status: "maintenance" }));
|
||||||
|
|
||||||
|
expect(defaults.monitorsRepository.updateById).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("throws when network response is null", async () => {
|
||||||
|
const { pipeline } = createCheckPipeline({
|
||||||
|
networkService: { requestStatus: jest.fn().mockResolvedValue(null) },
|
||||||
|
});
|
||||||
|
await expect(pipeline.run(makeMonitor())).rejects.toThrow("No network response");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("returns null and warns when buildCheck returns null", async () => {
|
||||||
|
const { pipeline, defaults } = createCheckPipeline({
|
||||||
|
checkService: { buildCheck: jest.fn().mockReturnValue(null) },
|
||||||
|
});
|
||||||
|
|
||||||
|
const result = await pipeline.run(makeMonitor());
|
||||||
|
|
||||||
|
expect(result).toBeNull();
|
||||||
|
expect(defaults.logger.warn).toHaveBeenCalledWith(expect.objectContaining({ message: expect.stringContaining("No check could be built") }));
|
||||||
|
expect(defaults.statusService.updateMonitorStatus).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("buffers the check, updates status, and returns an evaluation", async () => {
|
||||||
|
const status = { monitorId: "m1", status: false, code: 500, message: "Error" };
|
||||||
|
const statusChange = { monitor: { id: "m1", status: "down" }, statusChanged: true, prevStatus: "up", code: 500 };
|
||||||
|
const { pipeline, defaults } = createCheckPipeline({
|
||||||
|
networkService: { requestStatus: jest.fn().mockResolvedValue(status) },
|
||||||
|
statusService: { updateMonitorStatus: jest.fn().mockResolvedValue(statusChange) },
|
||||||
|
});
|
||||||
|
|
||||||
|
const result = await pipeline.run(makeMonitor());
|
||||||
|
|
||||||
|
expect(defaults.buffer.addToBuffer).toHaveBeenCalledWith({ id: "check-1" });
|
||||||
|
expect(defaults.statusService.updateMonitorStatus).toHaveBeenCalled();
|
||||||
|
expect(result).toMatchObject({
|
||||||
|
monitor: statusChange.monitor,
|
||||||
|
status,
|
||||||
|
check: { id: "check-1" },
|
||||||
|
statusChange,
|
||||||
|
decision: expect.objectContaining({ shouldCreateIncident: true, shouldSendNotification: true, incidentReason: "status_down" }),
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("proceeds with the check when no maintenance window is active", async () => {
|
||||||
|
const { pipeline, defaults } = createCheckPipeline({
|
||||||
|
maintenanceWindowsRepository: { findByMonitorId: jest.fn().mockResolvedValue([{ ...activeWindow(), active: false }]) },
|
||||||
|
});
|
||||||
|
|
||||||
|
await pipeline.run(makeMonitor());
|
||||||
|
|
||||||
|
expect(defaults.networkService.requestStatus).toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// ── GeoChecksPipeline ──────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
const createGeoPipeline = (overrides?: Record<string, any>) => {
|
||||||
|
const defaults = {
|
||||||
|
logger: createMockLogger(),
|
||||||
|
geoChecksService: { buildGeoCheck: jest.fn().mockResolvedValue(null) },
|
||||||
|
buffer: { addGeoCheckToBuffer: jest.fn() },
|
||||||
|
maintenanceWindowsRepository: { findByMonitorId: jest.fn().mockResolvedValue([]) },
|
||||||
|
...overrides,
|
||||||
|
};
|
||||||
|
const pipeline = new GeoChecksPipeline(
|
||||||
|
defaults.maintenanceWindowsRepository as any,
|
||||||
|
defaults.geoChecksService as any,
|
||||||
|
defaults.buffer as any,
|
||||||
|
defaults.logger as any
|
||||||
|
);
|
||||||
|
return { pipeline, defaults };
|
||||||
|
};
|
||||||
|
|
||||||
|
const geoMonitor = (overrides?: Partial<Monitor>) =>
|
||||||
|
makeMonitor({ geoCheckEnabled: true, type: "http", geoCheckLocations: ["us-east"], ...overrides });
|
||||||
|
|
||||||
|
describe("GeoChecksPipeline", () => {
|
||||||
|
it("throws when monitor id is missing", async () => {
|
||||||
|
const { pipeline } = createGeoPipeline();
|
||||||
|
await expect(pipeline.run({} as Monitor)).rejects.toThrow("No monitor id");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("returns null when geoCheckEnabled is false", async () => {
|
||||||
|
const { pipeline, defaults } = createGeoPipeline();
|
||||||
|
const result = await pipeline.run(makeMonitor({ geoCheckEnabled: false }));
|
||||||
|
expect(result).toBeNull();
|
||||||
|
expect(defaults.geoChecksService.buildGeoCheck).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("returns null when monitor type does not support geo checks", async () => {
|
||||||
|
const { pipeline, defaults } = createGeoPipeline();
|
||||||
|
await pipeline.run(geoMonitor({ type: "hardware" }));
|
||||||
|
expect(defaults.logger.debug).toHaveBeenCalledWith(expect.objectContaining({ message: expect.stringContaining("does not support geo checks") }));
|
||||||
|
expect(defaults.geoChecksService.buildGeoCheck).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("warns when geoCheckLocations is empty", async () => {
|
||||||
|
const { pipeline, defaults } = createGeoPipeline();
|
||||||
|
await pipeline.run(geoMonitor({ geoCheckLocations: [] }));
|
||||||
|
expect(defaults.logger.warn).toHaveBeenCalledWith(expect.objectContaining({ message: expect.stringContaining("No geo check locations") }));
|
||||||
|
});
|
||||||
|
|
||||||
|
it("warns when geoCheckLocations is undefined", async () => {
|
||||||
|
const { pipeline, defaults } = createGeoPipeline();
|
||||||
|
await pipeline.run(geoMonitor({ geoCheckLocations: undefined as any }));
|
||||||
|
expect(defaults.logger.warn).toHaveBeenCalledWith(expect.objectContaining({ message: expect.stringContaining("No geo check locations") }));
|
||||||
|
});
|
||||||
|
|
||||||
|
it("skips when in maintenance window", async () => {
|
||||||
|
const { pipeline, defaults } = createGeoPipeline({
|
||||||
|
maintenanceWindowsRepository: { findByMonitorId: jest.fn().mockResolvedValue([activeWindow()]) },
|
||||||
|
});
|
||||||
|
await pipeline.run(geoMonitor());
|
||||||
|
expect(defaults.logger.debug).toHaveBeenCalledWith(expect.objectContaining({ message: expect.stringContaining("maintenance window") }));
|
||||||
|
expect(defaults.geoChecksService.buildGeoCheck).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("warns when buildGeoCheck returns null", async () => {
|
||||||
|
const { pipeline, defaults } = createGeoPipeline();
|
||||||
|
await pipeline.run(geoMonitor());
|
||||||
|
expect(defaults.logger.warn).toHaveBeenCalledWith(expect.objectContaining({ message: expect.stringContaining("No geo check could be built") }));
|
||||||
|
});
|
||||||
|
|
||||||
|
it("adds geo check to buffer on success", async () => {
|
||||||
|
const geoCheck = { id: "gc-1", monitorId: "m1" };
|
||||||
|
const { pipeline, defaults } = createGeoPipeline({
|
||||||
|
geoChecksService: { buildGeoCheck: jest.fn().mockResolvedValue(geoCheck) },
|
||||||
|
});
|
||||||
|
await pipeline.run(geoMonitor());
|
||||||
|
expect(defaults.buffer.addGeoCheckToBuffer).toHaveBeenCalledWith(geoCheck);
|
||||||
|
expect(defaults.logger.debug).toHaveBeenCalledWith(expect.objectContaining({ message: expect.stringContaining("Geo check job executed") }));
|
||||||
|
});
|
||||||
|
|
||||||
|
it("propagates errors (the job glue handles isolation)", async () => {
|
||||||
|
const { pipeline } = createGeoPipeline({
|
||||||
|
geoChecksService: { buildGeoCheck: jest.fn().mockRejectedValue(new Error("api timeout")) },
|
||||||
|
});
|
||||||
|
await expect(pipeline.run(geoMonitor())).rejects.toThrow("api timeout");
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -7,7 +7,7 @@ import type { IUsersRepository } from "../../../src/domain/users/user.repository
|
|||||||
import type { INotificationMessageBuilder } from "../../../src/domain/notifications/notification.message-builder.ts";
|
import type { INotificationMessageBuilder } from "../../../src/domain/notifications/notification.message-builder.ts";
|
||||||
import type { Monitor } from "../../../src/domain/monitors/monitor.types.ts";
|
import type { Monitor } from "../../../src/domain/monitors/monitor.types.ts";
|
||||||
import type { Incident } from "../../../src/domain/incidents/incident.type.ts";
|
import type { Incident } from "../../../src/domain/incidents/incident.type.ts";
|
||||||
import type { MonitorActionDecision } from "../../../src/service/job-queues/job-queue.helper.ts";
|
import type { MonitorActionDecision } from "../../../src/worker/worker.helper.ts";
|
||||||
|
|
||||||
// ── Helpers ──────────────────────────────────────────────────────────────────
|
// ── Helpers ──────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
|||||||
@@ -44,7 +44,7 @@ jest.unstable_mockModule("less-simple-scheduler", () => ({
|
|||||||
MongoStore: MockMongoStore,
|
MongoStore: MockMongoStore,
|
||||||
}));
|
}));
|
||||||
|
|
||||||
const { LessSimpleQueue } = await import("../../../src/service/job-queues/job-queue.less-simple.ts");
|
const { LessSimpleWorker: LessSimpleQueue } = await import("../../../src/worker/worker.less-simple.ts");
|
||||||
|
|
||||||
const createQueueHelper = () => ({
|
const createQueueHelper = () => ({
|
||||||
getHeartbeatJob: jest.fn().mockReturnValue(() => Promise.resolve()),
|
getHeartbeatJob: jest.fn().mockReturnValue(() => Promise.resolve()),
|
||||||
|
|||||||
@@ -98,7 +98,7 @@ const createService = (
|
|||||||
const jobQueue = overrides.jobQueue ?? createJobQueueMock();
|
const jobQueue = overrides.jobQueue ?? createJobQueueMock();
|
||||||
const logger = overrides.logger ?? createMockLogger();
|
const logger = overrides.logger ?? createMockLogger();
|
||||||
const service = new MonitorService({
|
const service = new MonitorService({
|
||||||
jobQueue: jobQueue as any,
|
worker: jobQueue as any,
|
||||||
logger: logger as any,
|
logger: logger as any,
|
||||||
games: (overrides.games ?? { cs2: { name: "Counter-Strike 2" } }) as any,
|
games: (overrides.games ?? { cs2: { name: "Counter-Strike 2" } }) as any,
|
||||||
monitorsRepository: overrides.monitorsRepository ?? createMonitorsRepositoryMock(),
|
monitorsRepository: overrides.monitorsRepository ?? createMonitorsRepositoryMock(),
|
||||||
@@ -1535,7 +1535,7 @@ describe("MonitorService", () => {
|
|||||||
|
|
||||||
const logger = createMockLogger();
|
const logger = createMockLogger();
|
||||||
const service = new MonitorService({
|
const service = new MonitorService({
|
||||||
jobQueue: jobQueue as any,
|
worker: jobQueue as any,
|
||||||
logger: logger as any,
|
logger: logger as any,
|
||||||
games: {} as any,
|
games: {} as any,
|
||||||
monitorsRepository,
|
monitorsRepository,
|
||||||
|
|||||||
@@ -0,0 +1,74 @@
|
|||||||
|
import { describe, expect, it } from "@jest/globals";
|
||||||
|
import { MonitorStatusPolicy } from "../../../src/worker/worker.monitor-status-policy.ts";
|
||||||
|
import type { StatusChangeResult } from "../../../src/types/network.ts";
|
||||||
|
|
||||||
|
// ── Helpers ──────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
const makeStatusChange = (overrides: Partial<StatusChangeResult> & { status?: string }): StatusChangeResult => {
|
||||||
|
const { status = "up", ...rest } = overrides;
|
||||||
|
return {
|
||||||
|
monitor: { id: "m1", status } as any,
|
||||||
|
statusChanged: false,
|
||||||
|
prevStatus: "up",
|
||||||
|
code: 200,
|
||||||
|
timestamp: 0,
|
||||||
|
...rest,
|
||||||
|
} as StatusChangeResult;
|
||||||
|
};
|
||||||
|
|
||||||
|
// ── Tests ────────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
describe("MonitorStatusPolicy", () => {
|
||||||
|
const policy = new MonitorStatusPolicy();
|
||||||
|
|
||||||
|
it("does nothing when statusChanged is false", () => {
|
||||||
|
const decision = policy.evaluate(makeStatusChange({ status: "down", statusChanged: false, prevStatus: "up" }));
|
||||||
|
expect(decision).toEqual({
|
||||||
|
shouldCreateIncident: false,
|
||||||
|
shouldResolveIncident: false,
|
||||||
|
shouldSendNotification: false,
|
||||||
|
incidentReason: null,
|
||||||
|
notificationReason: null,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("creates incident and notifies when monitor goes down", () => {
|
||||||
|
const decision = policy.evaluate(makeStatusChange({ status: "down", statusChanged: true, prevStatus: "up", code: 500 }));
|
||||||
|
expect(decision).toMatchObject({
|
||||||
|
shouldCreateIncident: true,
|
||||||
|
shouldSendNotification: true,
|
||||||
|
incidentReason: "status_down",
|
||||||
|
notificationReason: "status_change",
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("creates incident and notifies when monitor is breached", () => {
|
||||||
|
const decision = policy.evaluate(makeStatusChange({ status: "breached", statusChanged: true, prevStatus: "up" }));
|
||||||
|
expect(decision).toMatchObject({
|
||||||
|
shouldCreateIncident: true,
|
||||||
|
shouldSendNotification: true,
|
||||||
|
incidentReason: "threshold_breach",
|
||||||
|
notificationReason: "threshold_breach",
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("resolves incident when monitor recovers from down", () => {
|
||||||
|
const decision = policy.evaluate(makeStatusChange({ status: "up", statusChanged: true, prevStatus: "down" }));
|
||||||
|
expect(decision).toMatchObject({
|
||||||
|
shouldResolveIncident: true,
|
||||||
|
shouldSendNotification: true,
|
||||||
|
shouldCreateIncident: false,
|
||||||
|
notificationReason: "status_change",
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("resolves incident when monitor recovers from breached", () => {
|
||||||
|
const decision = policy.evaluate(makeStatusChange({ status: "up", statusChanged: true, prevStatus: "breached" }));
|
||||||
|
expect(decision).toMatchObject({ shouldResolveIncident: true, shouldSendNotification: true });
|
||||||
|
});
|
||||||
|
|
||||||
|
it("does not create or resolve for unhandled status transitions", () => {
|
||||||
|
const decision = policy.evaluate(makeStatusChange({ status: "paused", statusChanged: true, prevStatus: "up" }));
|
||||||
|
expect(decision).toMatchObject({ shouldCreateIncident: false, shouldResolveIncident: false, shouldSendNotification: false });
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -2,7 +2,7 @@ import { describe, expect, it, beforeEach } from "@jest/globals";
|
|||||||
import { NotificationMessageBuilder } from "../../../src/domain/notifications/notification.message-builder.ts";
|
import { NotificationMessageBuilder } from "../../../src/domain/notifications/notification.message-builder.ts";
|
||||||
import type { Monitor } from "../../../src/domain/monitors/monitor.types.ts";
|
import type { Monitor } from "../../../src/domain/monitors/monitor.types.ts";
|
||||||
import type { MonitorStatusResponse, HardwareStatusPayload } from "../../../src/types/network.ts";
|
import type { MonitorStatusResponse, HardwareStatusPayload } from "../../../src/types/network.ts";
|
||||||
import type { MonitorActionDecision } from "../../../src/service/job-queues/job-queue.helper.ts";
|
import type { MonitorActionDecision } from "../../../src/worker/worker.helper.ts";
|
||||||
|
|
||||||
// ── Helpers ──────────────────────────────────────────────────────────────────
|
// ── Helpers ──────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import { createMockLogger } from "../../helpers/createMockLogger.ts";
|
|||||||
import type { Monitor } from "../../../src/domain/monitors/monitor.types.ts";
|
import type { Monitor } from "../../../src/domain/monitors/monitor.types.ts";
|
||||||
import type { Notification } from "../../../src/domain/notifications/notification.type.ts";
|
import type { Notification } from "../../../src/domain/notifications/notification.type.ts";
|
||||||
import type { MonitorStatusResponse } from "../../../src/types/network.ts";
|
import type { MonitorStatusResponse } from "../../../src/types/network.ts";
|
||||||
import type { MonitorActionDecision } from "../../../src/service/job-queues/job-queue.helper.ts";
|
import type { MonitorActionDecision } from "../../../src/worker/worker.helper.ts";
|
||||||
|
|
||||||
// ── Helpers ──────────────────────────────────────────────────────────────────
|
// ── Helpers ──────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,87 @@
|
|||||||
|
import { describe, expect, it, jest } from "@jest/globals";
|
||||||
|
import { ReactorDispatcher } from "../../../src/worker/reactors/reactor.dispatcher.ts";
|
||||||
|
import { createMockLogger } from "../../helpers/createMockLogger.ts";
|
||||||
|
|
||||||
|
const evaluation = { monitor: { id: "m1" } } as any;
|
||||||
|
|
||||||
|
const makeReactor = (name: string, blocking: boolean, impl?: () => Promise<void>) => ({
|
||||||
|
name,
|
||||||
|
blocking,
|
||||||
|
react: jest.fn(impl ?? (() => Promise.resolve())),
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("ReactorDispatcher", () => {
|
||||||
|
it("runs every reactor with the evaluation", async () => {
|
||||||
|
const a = makeReactor("a", true);
|
||||||
|
const b = makeReactor("b", false);
|
||||||
|
const dispatcher = new ReactorDispatcher(createMockLogger() as any, [a, b] as any);
|
||||||
|
|
||||||
|
await dispatcher.dispatch(evaluation);
|
||||||
|
|
||||||
|
expect(a.react).toHaveBeenCalledWith(evaluation);
|
||||||
|
expect(b.react).toHaveBeenCalledWith(evaluation);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("awaits blocking reactors before resolving", async () => {
|
||||||
|
let done = false;
|
||||||
|
const blocking = makeReactor(
|
||||||
|
"incident",
|
||||||
|
true,
|
||||||
|
() =>
|
||||||
|
new Promise((r) =>
|
||||||
|
setTimeout(() => {
|
||||||
|
done = true;
|
||||||
|
r();
|
||||||
|
}, 10)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
const dispatcher = new ReactorDispatcher(createMockLogger() as any, [blocking] as any);
|
||||||
|
|
||||||
|
await dispatcher.dispatch(evaluation);
|
||||||
|
|
||||||
|
expect(done).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("does not await non-blocking reactors", async () => {
|
||||||
|
let done = false;
|
||||||
|
const nonBlocking = makeReactor(
|
||||||
|
"notification",
|
||||||
|
false,
|
||||||
|
() =>
|
||||||
|
new Promise((r) =>
|
||||||
|
setTimeout(() => {
|
||||||
|
done = true;
|
||||||
|
r();
|
||||||
|
}, 20)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
const dispatcher = new ReactorDispatcher(createMockLogger() as any, [nonBlocking] as any);
|
||||||
|
|
||||||
|
await dispatcher.dispatch(evaluation);
|
||||||
|
|
||||||
|
expect(done).toBe(false); // resolved before the slow fire-and-forget reactor finished
|
||||||
|
});
|
||||||
|
|
||||||
|
it("isolates a throwing blocking reactor and still runs later reactors", async () => {
|
||||||
|
const logger = createMockLogger();
|
||||||
|
const bad = makeReactor("incident", true, () => Promise.reject(new Error("db error")));
|
||||||
|
const good = makeReactor("after", true);
|
||||||
|
const dispatcher = new ReactorDispatcher(logger as any, [bad, good] as any);
|
||||||
|
|
||||||
|
await expect(dispatcher.dispatch(evaluation)).resolves.toBeUndefined();
|
||||||
|
|
||||||
|
expect(good.react).toHaveBeenCalled();
|
||||||
|
expect(logger.error).toHaveBeenCalledWith(expect.objectContaining({ message: expect.stringContaining("db error") }));
|
||||||
|
});
|
||||||
|
|
||||||
|
it("isolates a rejecting non-blocking reactor (fire-and-forget)", async () => {
|
||||||
|
const logger = createMockLogger();
|
||||||
|
const bad = makeReactor("notification", false, () => Promise.reject(new Error("smtp down")));
|
||||||
|
const dispatcher = new ReactorDispatcher(logger as any, [bad] as any);
|
||||||
|
|
||||||
|
await dispatcher.dispatch(evaluation);
|
||||||
|
await new Promise((r) => setTimeout(r, 10)); // let the detached catch run
|
||||||
|
|
||||||
|
expect(logger.error).toHaveBeenCalledWith(expect.objectContaining({ message: expect.stringContaining("smtp down") }));
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -35,7 +35,7 @@ jest.unstable_mockModule("super-simple-scheduler", () => ({
|
|||||||
default: MockScheduler,
|
default: MockScheduler,
|
||||||
}));
|
}));
|
||||||
|
|
||||||
const { SuperSimpleQueue } = await import("../../../src/service/job-queues/job-queue.sumper-simple.ts");
|
const { SuperSimpleQueue } = await import("../../../src/worker/worker.super-simple.ts");
|
||||||
|
|
||||||
const createQueueHelper = () => ({
|
const createQueueHelper = () => ({
|
||||||
getHeartbeatJob: jest.fn().mockReturnValue(() => Promise.resolve()),
|
getHeartbeatJob: jest.fn().mockReturnValue(() => Promise.resolve()),
|
||||||
|
|||||||
@@ -1,14 +1,17 @@
|
|||||||
import { describe, expect, it, jest } from "@jest/globals";
|
import { describe, expect, it, jest } from "@jest/globals";
|
||||||
import { QueueHelper } from "../../../src/service/job-queues/job-queue.helper.ts";
|
import { WorkerHelper } from "../../../src/worker/worker.helper.ts";
|
||||||
import type { Monitor } from "../../../src/domain/monitors/monitor.types.ts";
|
import type { Monitor } from "../../../src/domain/monitors/monitor.types.ts";
|
||||||
import { createMockLogger } from "../../helpers/createMockLogger.ts";
|
import { createMockLogger } from "../../helpers/createMockLogger.ts";
|
||||||
|
|
||||||
// ── Helpers ──────────────────────────────────────────────────────────────────
|
// ── Helpers ──────────────────────────────────────────────────────────────────
|
||||||
|
//
|
||||||
|
// WorkerHelper is now thin glue: it wires the scheduler's job functions to the
|
||||||
|
// CheckPipeline / GeoChecksPipeline (acquire→record→decide) and the
|
||||||
|
// ReactorDispatcher (react), and owns the cleanup jobs. The pipeline and
|
||||||
|
// dispatcher internals are tested in checkPipeline.test.ts / reactorDispatcher.test.ts;
|
||||||
|
// here we test only the glue + cleanup jobs.
|
||||||
|
|
||||||
const createHelper = (overrides?: Record<string, unknown>) => {
|
const createHelper = (overrides?: Record<string, unknown>) => {
|
||||||
const maintenanceWindowsRepository = {
|
|
||||||
findByMonitorId: jest.fn().mockResolvedValue([]),
|
|
||||||
};
|
|
||||||
const monitorsRepository = {
|
const monitorsRepository = {
|
||||||
updateById: jest.fn().mockResolvedValue({}),
|
updateById: jest.fn().mockResolvedValue({}),
|
||||||
findAllMonitorIds: jest.fn().mockResolvedValue(["m1"]),
|
findAllMonitorIds: jest.fn().mockResolvedValue(["m1"]),
|
||||||
@@ -29,55 +32,48 @@ const createHelper = (overrides?: Record<string, unknown>) => {
|
|||||||
const geoChecksRepository = {
|
const geoChecksRepository = {
|
||||||
deleteByMonitorIdsNotIn: jest.fn().mockResolvedValue(0),
|
deleteByMonitorIdsNotIn: jest.fn().mockResolvedValue(0),
|
||||||
};
|
};
|
||||||
const statusServiceMock = {
|
const settingsService = {
|
||||||
updateMonitorStatus: jest.fn().mockResolvedValue({ monitor: { id: "m1", status: "up" }, statusChanged: false, prevStatus: "up", code: 200 }),
|
|
||||||
};
|
|
||||||
const settingsServiceMock = {
|
|
||||||
getDBSettings: jest.fn().mockResolvedValue({ checkTTL: 30 }),
|
getDBSettings: jest.fn().mockResolvedValue({ checkTTL: 30 }),
|
||||||
};
|
};
|
||||||
const geoChecksServiceMock = {
|
const checkService = {
|
||||||
buildGeoCheck: jest.fn().mockResolvedValue(null),
|
deleteOlderThan: jest.fn().mockResolvedValue(0),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const checkPipeline = { run: jest.fn().mockResolvedValue(null) };
|
||||||
|
const geoCheckPipeline = { run: jest.fn().mockResolvedValue(null) };
|
||||||
|
const reactorDispatcher = { dispatch: jest.fn().mockResolvedValue(undefined) };
|
||||||
|
|
||||||
const defaults = {
|
const defaults = {
|
||||||
logger: createMockLogger(),
|
logger: createMockLogger(),
|
||||||
networkService: { requestStatus: jest.fn() },
|
checkService,
|
||||||
statusService: statusServiceMock,
|
settingsService,
|
||||||
notificationsService: { handleNotifications: jest.fn().mockResolvedValue(undefined) },
|
|
||||||
checkService: { buildCheck: jest.fn().mockReturnValue({ id: "check-1" }), deleteOlderThan: jest.fn().mockResolvedValue(0) },
|
|
||||||
settingsService: settingsServiceMock,
|
|
||||||
buffer: { addToBuffer: jest.fn(), addGeoCheckToBuffer: jest.fn() },
|
|
||||||
incidentService: { handleIncident: jest.fn().mockResolvedValue(undefined) },
|
|
||||||
maintenanceWindowsRepository,
|
|
||||||
monitorsRepository,
|
monitorsRepository,
|
||||||
teamsRepository,
|
teamsRepository,
|
||||||
monitorStatsRepository,
|
monitorStatsRepository,
|
||||||
checksRepository,
|
checksRepository,
|
||||||
incidentsRepository,
|
incidentsRepository,
|
||||||
geoChecksService: geoChecksServiceMock,
|
|
||||||
geoChecksRepository,
|
geoChecksRepository,
|
||||||
|
reactorDispatcher,
|
||||||
|
checkPipeline,
|
||||||
|
geoCheckPipeline,
|
||||||
...overrides,
|
...overrides,
|
||||||
};
|
};
|
||||||
|
|
||||||
const helper = new QueueHelper(
|
const helper = new WorkerHelper(
|
||||||
defaults.logger as any,
|
defaults.logger as any,
|
||||||
defaults.networkService as any,
|
|
||||||
defaults.statusService as any,
|
|
||||||
defaults.notificationsService as any,
|
|
||||||
defaults.checkService as any,
|
defaults.checkService as any,
|
||||||
defaults.settingsService as any,
|
defaults.settingsService as any,
|
||||||
defaults.buffer as any,
|
|
||||||
defaults.incidentService as any,
|
|
||||||
defaults.maintenanceWindowsRepository as any,
|
|
||||||
defaults.monitorsRepository as any,
|
defaults.monitorsRepository as any,
|
||||||
defaults.teamsRepository as any,
|
defaults.teamsRepository as any,
|
||||||
defaults.monitorStatsRepository as any,
|
defaults.monitorStatsRepository as any,
|
||||||
defaults.checksRepository as any,
|
defaults.checksRepository as any,
|
||||||
defaults.incidentsRepository as any,
|
defaults.incidentsRepository as any,
|
||||||
defaults.geoChecksService as any,
|
defaults.geoChecksRepository as any,
|
||||||
defaults.geoChecksRepository as any
|
defaults.reactorDispatcher as any,
|
||||||
|
defaults.checkPipeline as any,
|
||||||
|
defaults.geoCheckPipeline as any
|
||||||
);
|
);
|
||||||
return { helper, defaults, maintenanceWindowsRepository };
|
return { helper, defaults };
|
||||||
};
|
};
|
||||||
|
|
||||||
const makeMonitor = (overrides?: Partial<Monitor>): Monitor =>
|
const makeMonitor = (overrides?: Partial<Monitor>): Monitor =>
|
||||||
@@ -94,7 +90,7 @@ const makeMonitor = (overrides?: Partial<Monitor>): Monitor =>
|
|||||||
|
|
||||||
// ── Tests ────────────────────────────────────────────────────────────────────
|
// ── Tests ────────────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
describe("QueueHelper", () => {
|
describe("WorkerHelper", () => {
|
||||||
describe("serviceName", () => {
|
describe("serviceName", () => {
|
||||||
it("returns JobQueueHelper", () => {
|
it("returns JobQueueHelper", () => {
|
||||||
const { helper } = createHelper();
|
const { helper } = createHelper();
|
||||||
@@ -102,278 +98,63 @@ describe("QueueHelper", () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// ── getHeartbeatJob ──────────────────────────────────────────────────────
|
// ── getHeartbeatJob (glue) ────────────────────────────────────────────────
|
||||||
|
|
||||||
describe("getHeartbeatJob", () => {
|
describe("getHeartbeatJob", () => {
|
||||||
it("throws when monitor id is missing", async () => {
|
it("runs the pipeline and dispatches the evaluation to reactors", async () => {
|
||||||
const { helper } = createHelper();
|
const evaluation = { monitor: { id: "m1" }, decision: {} };
|
||||||
const job = helper.getHeartbeatJob();
|
const { helper, defaults } = createHelper({ checkPipeline: { run: jest.fn().mockResolvedValue(evaluation) } });
|
||||||
await expect(job({} as Monitor)).rejects.toThrow("No monitor id");
|
|
||||||
|
await helper.getHeartbeatJob()(makeMonitor());
|
||||||
|
|
||||||
|
expect(defaults.checkPipeline.run).toHaveBeenCalled();
|
||||||
|
expect(defaults.reactorDispatcher.dispatch).toHaveBeenCalledWith(evaluation);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("skips execution and sets maintenance status when in maintenance window", async () => {
|
it("does not dispatch when the pipeline returns null (skipped)", async () => {
|
||||||
const { helper, defaults } = createHelper();
|
const { helper, defaults } = createHelper({ checkPipeline: { run: jest.fn().mockResolvedValue(null) } });
|
||||||
jest.spyOn(helper, "isInMaintenanceWindow").mockResolvedValue(true);
|
|
||||||
const job = helper.getHeartbeatJob();
|
|
||||||
|
|
||||||
await job(makeMonitor({ status: "up" }));
|
await helper.getHeartbeatJob()(makeMonitor());
|
||||||
|
|
||||||
expect(defaults.networkService.requestStatus).not.toHaveBeenCalled();
|
expect(defaults.reactorDispatcher.dispatch).not.toHaveBeenCalled();
|
||||||
expect(defaults.monitorsRepository.updateById).toHaveBeenCalledWith("m1", "team", { status: "maintenance" });
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("skips monitor status update when already in maintenance", async () => {
|
it("logs and rethrows when the pipeline throws", async () => {
|
||||||
const { helper, defaults } = createHelper();
|
const { helper, defaults } = createHelper({ checkPipeline: { run: jest.fn().mockRejectedValue(new Error("boom")) } });
|
||||||
jest.spyOn(helper, "isInMaintenanceWindow").mockResolvedValue(true);
|
|
||||||
const job = helper.getHeartbeatJob();
|
|
||||||
|
|
||||||
await job(makeMonitor({ status: "maintenance" }));
|
await expect(helper.getHeartbeatJob()(makeMonitor())).rejects.toThrow("boom");
|
||||||
|
expect(defaults.logger.warn).toHaveBeenCalledWith(expect.objectContaining({ message: "boom" }));
|
||||||
expect(defaults.monitorsRepository.updateById).not.toHaveBeenCalled();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("throws when network response is null", async () => {
|
it("rethrows non-Error failures", async () => {
|
||||||
const { helper } = createHelper({
|
const { helper } = createHelper({ checkPipeline: { run: jest.fn().mockRejectedValue("unexpected") } });
|
||||||
networkService: { requestStatus: jest.fn().mockResolvedValue(null) },
|
|
||||||
});
|
|
||||||
jest.spyOn(helper, "isInMaintenanceWindow").mockResolvedValue(false);
|
|
||||||
const job = helper.getHeartbeatJob();
|
|
||||||
|
|
||||||
await expect(job(makeMonitor())).rejects.toThrow("No network response");
|
await expect(helper.getHeartbeatJob()(makeMonitor())).rejects.toBe("unexpected");
|
||||||
});
|
|
||||||
|
|
||||||
it("returns early and warns when buildCheck returns null", async () => {
|
|
||||||
const networkResponse = { monitorId: "m1", status: true, code: 200, message: "OK" };
|
|
||||||
const { helper, defaults } = createHelper({
|
|
||||||
networkService: { requestStatus: jest.fn().mockResolvedValue(networkResponse) },
|
|
||||||
checkService: { buildCheck: jest.fn().mockReturnValue(null), deleteOlderThan: jest.fn() },
|
|
||||||
});
|
|
||||||
jest.spyOn(helper, "isInMaintenanceWindow").mockResolvedValue(false);
|
|
||||||
const job = helper.getHeartbeatJob();
|
|
||||||
|
|
||||||
await job(makeMonitor());
|
|
||||||
|
|
||||||
expect(defaults.logger.warn).toHaveBeenCalledWith(expect.objectContaining({ message: expect.stringContaining("No check could be built") }));
|
|
||||||
expect(defaults.statusService.updateMonitorStatus).not.toHaveBeenCalled();
|
|
||||||
});
|
|
||||||
|
|
||||||
it("processes full pipeline: network → check → status → incident", async () => {
|
|
||||||
const networkResponse = { monitorId: "m1", status: true, code: 200, message: "OK" };
|
|
||||||
const { helper, defaults } = createHelper({
|
|
||||||
networkService: { requestStatus: jest.fn().mockResolvedValue(networkResponse) },
|
|
||||||
});
|
|
||||||
jest.spyOn(helper, "isInMaintenanceWindow").mockResolvedValue(false);
|
|
||||||
const job = helper.getHeartbeatJob();
|
|
||||||
|
|
||||||
await job(makeMonitor());
|
|
||||||
|
|
||||||
expect(defaults.buffer.addToBuffer).toHaveBeenCalled();
|
|
||||||
expect(defaults.statusService.updateMonitorStatus).toHaveBeenCalled();
|
|
||||||
expect(defaults.incidentService.handleIncident).toHaveBeenCalled();
|
|
||||||
});
|
|
||||||
|
|
||||||
it("sends notifications when decision says shouldSendNotification", async () => {
|
|
||||||
const networkResponse = { monitorId: "m1", status: false, code: 500, message: "Error" };
|
|
||||||
const statusResult = { monitor: { id: "m1", status: "down" }, statusChanged: true, prevStatus: "up", code: 500 };
|
|
||||||
const { helper, defaults } = createHelper({
|
|
||||||
networkService: { requestStatus: jest.fn().mockResolvedValue(networkResponse) },
|
|
||||||
statusService: { updateMonitorStatus: jest.fn().mockResolvedValue(statusResult) },
|
|
||||||
});
|
|
||||||
jest.spyOn(helper, "isInMaintenanceWindow").mockResolvedValue(false);
|
|
||||||
const job = helper.getHeartbeatJob();
|
|
||||||
|
|
||||||
await job(makeMonitor());
|
|
||||||
|
|
||||||
expect(defaults.notificationsService.handleNotifications).toHaveBeenCalledWith(
|
|
||||||
statusResult.monitor,
|
|
||||||
networkResponse,
|
|
||||||
expect.objectContaining({ shouldSendNotification: true, shouldCreateIncident: true })
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("logs error when notifications fail (fire-and-forget)", async () => {
|
|
||||||
const networkResponse = { monitorId: "m1", status: false, code: 500, message: "Error" };
|
|
||||||
const statusResult = { monitor: { id: "m1", status: "down" }, statusChanged: true, prevStatus: "up", code: 500 };
|
|
||||||
const notificationsService = { handleNotifications: jest.fn().mockRejectedValue(new Error("smtp down")) };
|
|
||||||
const { helper, defaults } = createHelper({
|
|
||||||
networkService: { requestStatus: jest.fn().mockResolvedValue(networkResponse) },
|
|
||||||
statusService: { updateMonitorStatus: jest.fn().mockResolvedValue(statusResult) },
|
|
||||||
notificationsService,
|
|
||||||
});
|
|
||||||
jest.spyOn(helper, "isInMaintenanceWindow").mockResolvedValue(false);
|
|
||||||
const job = helper.getHeartbeatJob();
|
|
||||||
|
|
||||||
await job(makeMonitor());
|
|
||||||
|
|
||||||
// Wait for the fire-and-forget catch to execute
|
|
||||||
await new Promise((r) => setTimeout(r, 10));
|
|
||||||
expect(defaults.logger.error).toHaveBeenCalledWith(expect.objectContaining({ message: expect.stringContaining("smtp down") }));
|
|
||||||
});
|
|
||||||
|
|
||||||
it("logs error when notifications fail with non-Error (fire-and-forget)", async () => {
|
|
||||||
const networkResponse = { monitorId: "m1", status: false, code: 500, message: "Error" };
|
|
||||||
const statusResult = { monitor: { id: "m1", status: "down" }, statusChanged: true, prevStatus: "up", code: 500 };
|
|
||||||
const notificationsService = { handleNotifications: jest.fn().mockRejectedValue("string error") };
|
|
||||||
const { helper, defaults } = createHelper({
|
|
||||||
networkService: { requestStatus: jest.fn().mockResolvedValue(networkResponse) },
|
|
||||||
statusService: { updateMonitorStatus: jest.fn().mockResolvedValue(statusResult) },
|
|
||||||
notificationsService,
|
|
||||||
});
|
|
||||||
jest.spyOn(helper, "isInMaintenanceWindow").mockResolvedValue(false);
|
|
||||||
const job = helper.getHeartbeatJob();
|
|
||||||
|
|
||||||
await job(makeMonitor());
|
|
||||||
await new Promise((r) => setTimeout(r, 10));
|
|
||||||
expect(defaults.logger.error).toHaveBeenCalledWith(expect.objectContaining({ message: expect.stringContaining("Unknown error") }));
|
|
||||||
});
|
|
||||||
|
|
||||||
it("logs warning when incident handling fails (fire-and-forget)", async () => {
|
|
||||||
const networkResponse = { monitorId: "m1", status: true, code: 200, message: "OK" };
|
|
||||||
const incidentService = { handleIncident: jest.fn().mockRejectedValue(new Error("db error")) };
|
|
||||||
const { helper, defaults } = createHelper({
|
|
||||||
networkService: { requestStatus: jest.fn().mockResolvedValue(networkResponse) },
|
|
||||||
incidentService,
|
|
||||||
});
|
|
||||||
jest.spyOn(helper, "isInMaintenanceWindow").mockResolvedValue(false);
|
|
||||||
const job = helper.getHeartbeatJob();
|
|
||||||
|
|
||||||
await job(makeMonitor());
|
|
||||||
await new Promise((r) => setTimeout(r, 10));
|
|
||||||
expect(defaults.logger.warn).toHaveBeenCalledWith(expect.objectContaining({ message: expect.stringContaining("db error") }));
|
|
||||||
});
|
|
||||||
|
|
||||||
it("logs warning when incident handling fails with non-Error", async () => {
|
|
||||||
const networkResponse = { monitorId: "m1", status: true, code: 200, message: "OK" };
|
|
||||||
const incidentService = { handleIncident: jest.fn().mockRejectedValue(42) };
|
|
||||||
const { helper, defaults } = createHelper({
|
|
||||||
networkService: { requestStatus: jest.fn().mockResolvedValue(networkResponse) },
|
|
||||||
incidentService,
|
|
||||||
});
|
|
||||||
jest.spyOn(helper, "isInMaintenanceWindow").mockResolvedValue(false);
|
|
||||||
const job = helper.getHeartbeatJob();
|
|
||||||
|
|
||||||
await job(makeMonitor());
|
|
||||||
await new Promise((r) => setTimeout(r, 10));
|
|
||||||
expect(defaults.logger.warn).toHaveBeenCalledWith(expect.objectContaining({ message: expect.stringContaining("Unknown error") }));
|
|
||||||
});
|
|
||||||
|
|
||||||
it("logs and rethrows on unexpected error with non-Error", async () => {
|
|
||||||
const { helper } = createHelper({
|
|
||||||
networkService: { requestStatus: jest.fn().mockRejectedValue("unexpected") },
|
|
||||||
});
|
|
||||||
jest.spyOn(helper, "isInMaintenanceWindow").mockResolvedValue(false);
|
|
||||||
const job = helper.getHeartbeatJob();
|
|
||||||
|
|
||||||
await expect(job(makeMonitor())).rejects.toBe("unexpected");
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// ── getHeartbeatGeoJob ───────────────────────────────────────────────────
|
// ── getHeartbeatGeoJob (glue) ─────────────────────────────────────────────
|
||||||
|
|
||||||
describe("getHeartbeatGeoJob", () => {
|
describe("getHeartbeatGeoJob", () => {
|
||||||
it("throws nothing when monitor id is missing (logs error)", async () => {
|
it("runs the geo pipeline", async () => {
|
||||||
const { helper, defaults } = createHelper();
|
const { helper, defaults } = createHelper();
|
||||||
const job = helper.getHeartbeatGeoJob();
|
|
||||||
|
|
||||||
await job({} as Monitor);
|
await helper.getHeartbeatGeoJob()(makeMonitor());
|
||||||
|
|
||||||
expect(defaults.logger.error).toHaveBeenCalledWith(expect.objectContaining({ message: "No monitor id" }));
|
expect(defaults.geoCheckPipeline.run).toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
|
|
||||||
it("returns early when geoCheckEnabled is false", async () => {
|
it("catches and logs a pipeline error without rethrowing", async () => {
|
||||||
const { helper, defaults } = createHelper();
|
const { helper, defaults } = createHelper({ geoCheckPipeline: { run: jest.fn().mockRejectedValue(new Error("api timeout")) } });
|
||||||
const job = helper.getHeartbeatGeoJob();
|
|
||||||
|
|
||||||
await job(makeMonitor({ geoCheckEnabled: false }));
|
|
||||||
|
|
||||||
expect(defaults.geoChecksService.buildGeoCheck).not.toHaveBeenCalled();
|
|
||||||
});
|
|
||||||
|
|
||||||
it("returns early when monitor type does not support geo checks", async () => {
|
|
||||||
const { helper, defaults } = createHelper();
|
|
||||||
const job = helper.getHeartbeatGeoJob();
|
|
||||||
|
|
||||||
await job(makeMonitor({ geoCheckEnabled: true, type: "hardware", geoCheckLocations: ["us-east"] }));
|
|
||||||
|
|
||||||
expect(defaults.logger.debug).toHaveBeenCalledWith(
|
|
||||||
expect.objectContaining({ message: expect.stringContaining("does not support geo checks") })
|
|
||||||
);
|
|
||||||
expect(defaults.geoChecksService.buildGeoCheck).not.toHaveBeenCalled();
|
|
||||||
});
|
|
||||||
|
|
||||||
it("returns early when geoCheckLocations is empty", async () => {
|
|
||||||
const { helper, defaults } = createHelper();
|
|
||||||
const job = helper.getHeartbeatGeoJob();
|
|
||||||
|
|
||||||
await job(makeMonitor({ geoCheckEnabled: true, type: "http", geoCheckLocations: [] }));
|
|
||||||
|
|
||||||
expect(defaults.logger.warn).toHaveBeenCalledWith(expect.objectContaining({ message: expect.stringContaining("No geo check locations") }));
|
|
||||||
});
|
|
||||||
|
|
||||||
it("returns early when geoCheckLocations is undefined", async () => {
|
|
||||||
const { helper, defaults } = createHelper();
|
|
||||||
const job = helper.getHeartbeatGeoJob();
|
|
||||||
|
|
||||||
await job(makeMonitor({ geoCheckEnabled: true, type: "http", geoCheckLocations: undefined as any }));
|
|
||||||
|
|
||||||
expect(defaults.logger.warn).toHaveBeenCalledWith(expect.objectContaining({ message: expect.stringContaining("No geo check locations") }));
|
|
||||||
});
|
|
||||||
|
|
||||||
it("skips when in maintenance window", async () => {
|
|
||||||
const { helper, defaults } = createHelper();
|
|
||||||
jest.spyOn(helper, "isInMaintenanceWindow").mockResolvedValue(true);
|
|
||||||
const job = helper.getHeartbeatGeoJob();
|
|
||||||
|
|
||||||
await job(makeMonitor({ geoCheckEnabled: true, type: "http", geoCheckLocations: ["us-east"] }));
|
|
||||||
|
|
||||||
expect(defaults.logger.debug).toHaveBeenCalledWith(expect.objectContaining({ message: expect.stringContaining("maintenance window") }));
|
|
||||||
expect(defaults.geoChecksService.buildGeoCheck).not.toHaveBeenCalled();
|
|
||||||
});
|
|
||||||
|
|
||||||
it("warns when buildGeoCheck returns null", async () => {
|
|
||||||
const { helper, defaults } = createHelper();
|
|
||||||
jest.spyOn(helper, "isInMaintenanceWindow").mockResolvedValue(false);
|
|
||||||
const job = helper.getHeartbeatGeoJob();
|
|
||||||
|
|
||||||
await job(makeMonitor({ geoCheckEnabled: true, type: "http", geoCheckLocations: ["us-east"] }));
|
|
||||||
|
|
||||||
expect(defaults.logger.warn).toHaveBeenCalledWith(expect.objectContaining({ message: expect.stringContaining("No geo check could be built") }));
|
|
||||||
});
|
|
||||||
|
|
||||||
it("adds geo check to buffer on success", async () => {
|
|
||||||
const geoCheck = { id: "gc-1", monitorId: "m1" };
|
|
||||||
const { helper, defaults } = createHelper({
|
|
||||||
geoChecksService: { buildGeoCheck: jest.fn().mockResolvedValue(geoCheck) },
|
|
||||||
});
|
|
||||||
jest.spyOn(helper, "isInMaintenanceWindow").mockResolvedValue(false);
|
|
||||||
const job = helper.getHeartbeatGeoJob();
|
|
||||||
|
|
||||||
await job(makeMonitor({ geoCheckEnabled: true, type: "http", geoCheckLocations: ["us-east"] }));
|
|
||||||
|
|
||||||
expect(defaults.buffer.addGeoCheckToBuffer).toHaveBeenCalledWith(geoCheck);
|
|
||||||
expect(defaults.logger.debug).toHaveBeenCalledWith(expect.objectContaining({ message: expect.stringContaining("Geo check job executed") }));
|
|
||||||
});
|
|
||||||
|
|
||||||
it("logs error on unexpected failure without rethrowing", async () => {
|
|
||||||
const { helper, defaults } = createHelper({
|
|
||||||
geoChecksService: { buildGeoCheck: jest.fn().mockRejectedValue(new Error("api timeout")) },
|
|
||||||
});
|
|
||||||
jest.spyOn(helper, "isInMaintenanceWindow").mockResolvedValue(false);
|
|
||||||
const job = helper.getHeartbeatGeoJob();
|
|
||||||
|
|
||||||
await job(makeMonitor({ geoCheckEnabled: true, type: "http", geoCheckLocations: ["us-east"] }));
|
|
||||||
|
|
||||||
|
await expect(helper.getHeartbeatGeoJob()(makeMonitor())).resolves.toBeUndefined();
|
||||||
expect(defaults.logger.error).toHaveBeenCalledWith(expect.objectContaining({ message: "api timeout" }));
|
expect(defaults.logger.error).toHaveBeenCalledWith(expect.objectContaining({ message: "api timeout" }));
|
||||||
});
|
});
|
||||||
|
|
||||||
it("logs 'Unknown error' for non-Error exceptions", async () => {
|
it("logs 'Unknown error' for non-Error failures", async () => {
|
||||||
const { helper, defaults } = createHelper({
|
const { helper, defaults } = createHelper({ geoCheckPipeline: { run: jest.fn().mockRejectedValue("string error") } });
|
||||||
geoChecksService: { buildGeoCheck: jest.fn().mockRejectedValue("string error") },
|
|
||||||
});
|
|
||||||
jest.spyOn(helper, "isInMaintenanceWindow").mockResolvedValue(false);
|
|
||||||
const job = helper.getHeartbeatGeoJob();
|
|
||||||
|
|
||||||
await job(makeMonitor({ geoCheckEnabled: true, type: "http", geoCheckLocations: ["us-east"] }));
|
await helper.getHeartbeatGeoJob()(makeMonitor());
|
||||||
|
|
||||||
expect(defaults.logger.error).toHaveBeenCalledWith(expect.objectContaining({ message: "Unknown error", stack: undefined }));
|
expect(defaults.logger.error).toHaveBeenCalledWith(expect.objectContaining({ message: "Unknown error", stack: undefined }));
|
||||||
});
|
});
|
||||||
@@ -400,7 +181,6 @@ describe("QueueHelper", () => {
|
|||||||
expect(defaults.checksRepository.deleteByMonitorIdsNotIn).toHaveBeenCalledWith(["m1"]);
|
expect(defaults.checksRepository.deleteByMonitorIdsNotIn).toHaveBeenCalledWith(["m1"]);
|
||||||
expect(defaults.incidentsRepository.deleteByMonitorIdsNotIn).toHaveBeenCalledWith(["m1"]);
|
expect(defaults.incidentsRepository.deleteByMonitorIdsNotIn).toHaveBeenCalledWith(["m1"]);
|
||||||
expect(defaults.geoChecksRepository.deleteByMonitorIdsNotIn).toHaveBeenCalledWith(["m1"]);
|
expect(defaults.geoChecksRepository.deleteByMonitorIdsNotIn).toHaveBeenCalledWith(["m1"]);
|
||||||
// Logs info for each deleted count > 0
|
|
||||||
expect(defaults.logger.info).toHaveBeenCalledWith(expect.objectContaining({ message: expect.stringContaining("2 orphaned monitors") }));
|
expect(defaults.logger.info).toHaveBeenCalledWith(expect.objectContaining({ message: expect.stringContaining("2 orphaned monitors") }));
|
||||||
expect(defaults.logger.info).toHaveBeenCalledWith(expect.objectContaining({ message: expect.stringContaining("3 orphaned monitor stats") }));
|
expect(defaults.logger.info).toHaveBeenCalledWith(expect.objectContaining({ message: expect.stringContaining("3 orphaned monitor stats") }));
|
||||||
expect(defaults.logger.info).toHaveBeenCalledWith(expect.objectContaining({ message: expect.stringContaining("4 orphaned checks") }));
|
expect(defaults.logger.info).toHaveBeenCalledWith(expect.objectContaining({ message: expect.stringContaining("4 orphaned checks") }));
|
||||||
@@ -413,7 +193,6 @@ describe("QueueHelper", () => {
|
|||||||
const job = helper.getCleanupOrphanedJob();
|
const job = helper.getCleanupOrphanedJob();
|
||||||
await job();
|
await job();
|
||||||
|
|
||||||
// Only start and completion logs, no deletion logs
|
|
||||||
const infoMessages = (defaults.logger.info as jest.Mock).mock.calls.map((c: any) => c[0].message);
|
const infoMessages = (defaults.logger.info as jest.Mock).mock.calls.map((c: any) => c[0].message);
|
||||||
expect(infoMessages).toContain("Starting cleanup of orphaned data");
|
expect(infoMessages).toContain("Starting cleanup of orphaned data");
|
||||||
expect(infoMessages).toContain("Cleanup of orphaned data completed");
|
expect(infoMessages).toContain("Cleanup of orphaned data completed");
|
||||||
@@ -483,236 +262,4 @@ describe("QueueHelper", () => {
|
|||||||
expect(defaults.logger.error).toHaveBeenCalledWith(expect.objectContaining({ message: "Unknown error", stack: undefined }));
|
expect(defaults.logger.error).toHaveBeenCalledWith(expect.objectContaining({ message: "Unknown error", stack: undefined }));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// ── isInMaintenanceWindow ────────────────────────────────────────────────
|
|
||||||
|
|
||||||
describe("isInMaintenanceWindow", () => {
|
|
||||||
it("returns true when an active window spans now", async () => {
|
|
||||||
const now = new Date();
|
|
||||||
const { helper, maintenanceWindowsRepository } = createHelper();
|
|
||||||
maintenanceWindowsRepository.findByMonitorId.mockResolvedValue([
|
|
||||||
{
|
|
||||||
active: true,
|
|
||||||
start: new Date(now.getTime() - 1000).toISOString(),
|
|
||||||
end: new Date(now.getTime() + 1000).toISOString(),
|
|
||||||
repeat: 0,
|
|
||||||
},
|
|
||||||
]);
|
|
||||||
await expect(helper.isInMaintenanceWindow("m1", "team")).resolves.toBe(true);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("returns true when repeat interval advances window into current time", async () => {
|
|
||||||
const now = Date.now();
|
|
||||||
const { helper, maintenanceWindowsRepository } = createHelper();
|
|
||||||
maintenanceWindowsRepository.findByMonitorId.mockResolvedValue([
|
|
||||||
{
|
|
||||||
active: true,
|
|
||||||
start: new Date(now - 7200000).toISOString(),
|
|
||||||
end: new Date(now - 6600000).toISOString(),
|
|
||||||
repeat: 3600000,
|
|
||||||
},
|
|
||||||
]);
|
|
||||||
await expect(helper.isInMaintenanceWindow("m1", "team")).resolves.toBe(true);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("returns false when no active windows exist", async () => {
|
|
||||||
const { helper } = createHelper();
|
|
||||||
await expect(helper.isInMaintenanceWindow("m1", "team")).resolves.toBe(false);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("returns false when window is inactive", async () => {
|
|
||||||
const now = new Date();
|
|
||||||
const { helper, maintenanceWindowsRepository } = createHelper();
|
|
||||||
maintenanceWindowsRepository.findByMonitorId.mockResolvedValue([
|
|
||||||
{
|
|
||||||
active: false,
|
|
||||||
start: new Date(now.getTime() - 1000).toISOString(),
|
|
||||||
end: new Date(now.getTime() + 1000).toISOString(),
|
|
||||||
repeat: 0,
|
|
||||||
},
|
|
||||||
]);
|
|
||||||
await expect(helper.isInMaintenanceWindow("m1", "team")).resolves.toBe(false);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("returns false when active window is in the past without repeat", async () => {
|
|
||||||
const now = Date.now();
|
|
||||||
const { helper, maintenanceWindowsRepository } = createHelper();
|
|
||||||
maintenanceWindowsRepository.findByMonitorId.mockResolvedValue([
|
|
||||||
{
|
|
||||||
active: true,
|
|
||||||
start: new Date(now - 5000).toISOString(),
|
|
||||||
end: new Date(now - 1000).toISOString(),
|
|
||||||
repeat: 0,
|
|
||||||
},
|
|
||||||
]);
|
|
||||||
await expect(helper.isInMaintenanceWindow("m1", "team")).resolves.toBe(false);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("returns false when repeat advances past current time without match", async () => {
|
|
||||||
const now = Date.now();
|
|
||||||
const { helper, maintenanceWindowsRepository } = createHelper();
|
|
||||||
// Window was 2 hours ago, lasts 10 minutes, repeats every hour
|
|
||||||
// First repeat: -1h to -50min (past), second repeat: now to +10min... actually this would match
|
|
||||||
// Use a repeat that skips over now
|
|
||||||
maintenanceWindowsRepository.findByMonitorId.mockResolvedValue([
|
|
||||||
{
|
|
||||||
active: true,
|
|
||||||
start: new Date(now - 3600000 * 3 - 300000).toISOString(), // 3h5min ago
|
|
||||||
end: new Date(now - 3600000 * 3).toISOString(), // 3h ago, 5min window
|
|
||||||
repeat: 3600000, // 1 hour repeat
|
|
||||||
},
|
|
||||||
]);
|
|
||||||
// Advances: -2h5m to -2h, -1h5m to -1h, -5m to 0 — that last one might match if now is within 5min
|
|
||||||
// Let's use a different setup that clearly doesn't match
|
|
||||||
maintenanceWindowsRepository.findByMonitorId.mockResolvedValue([
|
|
||||||
{
|
|
||||||
active: true,
|
|
||||||
start: new Date(now - 1800000 - 60000).toISOString(), // 31 min ago
|
|
||||||
end: new Date(now - 1800000).toISOString(), // 30 min ago, 1 min window
|
|
||||||
repeat: 3600000, // 1 hour repeat — next window would be in 29 min
|
|
||||||
},
|
|
||||||
]);
|
|
||||||
await expect(helper.isInMaintenanceWindow("m1", "team")).resolves.toBe(false);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("preserves previous true result via accumulator", async () => {
|
|
||||||
const now = new Date();
|
|
||||||
const { helper, maintenanceWindowsRepository } = createHelper();
|
|
||||||
maintenanceWindowsRepository.findByMonitorId.mockResolvedValue([
|
|
||||||
{
|
|
||||||
active: true,
|
|
||||||
start: new Date(now.getTime() - 1000).toISOString(),
|
|
||||||
end: new Date(now.getTime() + 1000).toISOString(),
|
|
||||||
repeat: 0,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
active: false,
|
|
||||||
start: new Date(now.getTime() - 1000).toISOString(),
|
|
||||||
end: new Date(now.getTime() + 1000).toISOString(),
|
|
||||||
repeat: 0,
|
|
||||||
},
|
|
||||||
]);
|
|
||||||
// Second window is inactive but first already matched — accumulator should preserve true
|
|
||||||
await expect(helper.isInMaintenanceWindow("m1", "team")).resolves.toBe(true);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
// ── evaluateMonitorAction (tested indirectly via getHeartbeatJob) ─────────
|
|
||||||
|
|
||||||
describe("evaluateMonitorAction", () => {
|
|
||||||
const runJobAndGetDecision = async (statusResult: Record<string, unknown>) => {
|
|
||||||
const networkResponse = { monitorId: "m1", status: true, code: 200, message: "OK" };
|
|
||||||
const incidentService = { handleIncident: jest.fn().mockResolvedValue(undefined) };
|
|
||||||
const notificationsService = { handleNotifications: jest.fn().mockResolvedValue(undefined) };
|
|
||||||
const { helper } = createHelper({
|
|
||||||
networkService: { requestStatus: jest.fn().mockResolvedValue(networkResponse) },
|
|
||||||
statusService: { updateMonitorStatus: jest.fn().mockResolvedValue(statusResult) },
|
|
||||||
incidentService,
|
|
||||||
notificationsService,
|
|
||||||
});
|
|
||||||
jest.spyOn(helper, "isInMaintenanceWindow").mockResolvedValue(false);
|
|
||||||
const job = helper.getHeartbeatJob();
|
|
||||||
await job(makeMonitor());
|
|
||||||
return { incidentService, notificationsService };
|
|
||||||
};
|
|
||||||
|
|
||||||
it("does nothing when statusChanged is false", async () => {
|
|
||||||
const { incidentService, notificationsService } = await runJobAndGetDecision({
|
|
||||||
monitor: { id: "m1", status: "up" },
|
|
||||||
statusChanged: false,
|
|
||||||
prevStatus: "up",
|
|
||||||
code: 200,
|
|
||||||
});
|
|
||||||
expect(incidentService.handleIncident).toHaveBeenCalledWith(
|
|
||||||
expect.anything(),
|
|
||||||
expect.anything(),
|
|
||||||
expect.objectContaining({ shouldCreateIncident: false, shouldResolveIncident: false, shouldSendNotification: false }),
|
|
||||||
expect.anything()
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("creates incident and notifies when monitor goes down", async () => {
|
|
||||||
const { incidentService } = await runJobAndGetDecision({
|
|
||||||
monitor: { id: "m1", status: "down" },
|
|
||||||
statusChanged: true,
|
|
||||||
prevStatus: "up",
|
|
||||||
code: 500,
|
|
||||||
});
|
|
||||||
expect(incidentService.handleIncident).toHaveBeenCalledWith(
|
|
||||||
expect.anything(),
|
|
||||||
expect.anything(),
|
|
||||||
expect.objectContaining({
|
|
||||||
shouldCreateIncident: true,
|
|
||||||
shouldSendNotification: true,
|
|
||||||
incidentReason: "status_down",
|
|
||||||
notificationReason: "status_change",
|
|
||||||
}),
|
|
||||||
expect.anything()
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("creates incident and notifies when monitor is breached", async () => {
|
|
||||||
const { incidentService } = await runJobAndGetDecision({
|
|
||||||
monitor: { id: "m1", status: "breached" },
|
|
||||||
statusChanged: true,
|
|
||||||
prevStatus: "up",
|
|
||||||
code: 200,
|
|
||||||
});
|
|
||||||
expect(incidentService.handleIncident).toHaveBeenCalledWith(
|
|
||||||
expect.anything(),
|
|
||||||
expect.anything(),
|
|
||||||
expect.objectContaining({
|
|
||||||
shouldCreateIncident: true,
|
|
||||||
incidentReason: "threshold_breach",
|
|
||||||
notificationReason: "threshold_breach",
|
|
||||||
}),
|
|
||||||
expect.anything()
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("resolves incident when monitor recovers from down", async () => {
|
|
||||||
const { incidentService } = await runJobAndGetDecision({
|
|
||||||
monitor: { id: "m1", status: "up" },
|
|
||||||
statusChanged: true,
|
|
||||||
prevStatus: "down",
|
|
||||||
code: 200,
|
|
||||||
});
|
|
||||||
expect(incidentService.handleIncident).toHaveBeenCalledWith(
|
|
||||||
expect.anything(),
|
|
||||||
expect.anything(),
|
|
||||||
expect.objectContaining({ shouldResolveIncident: true, shouldSendNotification: true }),
|
|
||||||
expect.anything()
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("resolves incident when monitor recovers from breached", async () => {
|
|
||||||
const { incidentService } = await runJobAndGetDecision({
|
|
||||||
monitor: { id: "m1", status: "up" },
|
|
||||||
statusChanged: true,
|
|
||||||
prevStatus: "breached",
|
|
||||||
code: 200,
|
|
||||||
});
|
|
||||||
expect(incidentService.handleIncident).toHaveBeenCalledWith(
|
|
||||||
expect.anything(),
|
|
||||||
expect.anything(),
|
|
||||||
expect.objectContaining({ shouldResolveIncident: true }),
|
|
||||||
expect.anything()
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("does not create or resolve for unhandled status transitions", async () => {
|
|
||||||
const { incidentService } = await runJobAndGetDecision({
|
|
||||||
monitor: { id: "m1", status: "paused" },
|
|
||||||
statusChanged: true,
|
|
||||||
prevStatus: "up",
|
|
||||||
code: 200,
|
|
||||||
});
|
|
||||||
expect(incidentService.handleIncident).toHaveBeenCalledWith(
|
|
||||||
expect.anything(),
|
|
||||||
expect.anything(),
|
|
||||||
expect.objectContaining({ shouldCreateIncident: false, shouldResolveIncident: false, shouldSendNotification: false }),
|
|
||||||
expect.anything()
|
|
||||||
);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
@@ -65,7 +65,7 @@ const createService = (overrides?: Record<string, unknown>) => {
|
|||||||
const settingsService = {
|
const settingsService = {
|
||||||
getSettings: jest.fn().mockReturnValue(makeAppSettings()),
|
getSettings: jest.fn().mockReturnValue(makeAppSettings()),
|
||||||
};
|
};
|
||||||
const jobQueue = {
|
const worker = {
|
||||||
deleteJob: jest.fn().mockResolvedValue(undefined),
|
deleteJob: jest.fn().mockResolvedValue(undefined),
|
||||||
};
|
};
|
||||||
const jwtMock = {
|
const jwtMock = {
|
||||||
@@ -85,7 +85,7 @@ const createService = (overrides?: Record<string, unknown>) => {
|
|||||||
monitorsRepository,
|
monitorsRepository,
|
||||||
emailService,
|
emailService,
|
||||||
settingsService,
|
settingsService,
|
||||||
jobQueue,
|
worker,
|
||||||
jwt: jwtMock,
|
jwt: jwtMock,
|
||||||
crypto: cryptoMock,
|
crypto: cryptoMock,
|
||||||
...overrides,
|
...overrides,
|
||||||
@@ -475,23 +475,23 @@ describe("UserService", () => {
|
|||||||
|
|
||||||
describe("deleteUser", () => {
|
describe("deleteUser", () => {
|
||||||
it("deletes user and cleans up monitor jobs for superadmin", async () => {
|
it("deletes user and cleans up monitor jobs for superadmin", async () => {
|
||||||
const { service, usersRepository, jobQueue, monitorsRepository } = createService();
|
const { service, usersRepository, worker, monitorsRepository } = createService();
|
||||||
|
|
||||||
await service.deleteUser({ userId: "user-1", teamId: "team-1", roles: ["superadmin"] });
|
await service.deleteUser({ userId: "user-1", teamId: "team-1", roles: ["superadmin"] });
|
||||||
|
|
||||||
expect(monitorsRepository.findByTeamId).toHaveBeenCalledWith("team-1", {});
|
expect(monitorsRepository.findByTeamId).toHaveBeenCalledWith("team-1", {});
|
||||||
expect(jobQueue.deleteJob).toHaveBeenCalledTimes(2);
|
expect(worker.deleteJob).toHaveBeenCalledTimes(2);
|
||||||
expect(usersRepository.deleteById).toHaveBeenCalledWith("user-1");
|
expect(usersRepository.deleteById).toHaveBeenCalledWith("user-1");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("handles null monitors for superadmin gracefully", async () => {
|
it("handles null monitors for superadmin gracefully", async () => {
|
||||||
const { service, usersRepository, jobQueue } = createService({
|
const { service, usersRepository, worker } = createService({
|
||||||
monitorsRepository: { findByTeamId: jest.fn().mockResolvedValue(null) },
|
monitorsRepository: { findByTeamId: jest.fn().mockResolvedValue(null) },
|
||||||
});
|
});
|
||||||
|
|
||||||
await service.deleteUser({ userId: "user-1", teamId: "team-1", roles: ["superadmin"] });
|
await service.deleteUser({ userId: "user-1", teamId: "team-1", roles: ["superadmin"] });
|
||||||
|
|
||||||
expect(jobQueue.deleteJob).not.toHaveBeenCalled();
|
expect(worker.deleteJob).not.toHaveBeenCalled();
|
||||||
expect(usersRepository.deleteById).toHaveBeenCalledWith("user-1");
|
expect(usersRepository.deleteById).toHaveBeenCalledWith("user-1");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user