notificationsService

This commit is contained in:
Alex Holliday
2026-01-18 22:52:59 +00:00
parent 391f42ee53
commit 4e9839e2e5
6 changed files with 46 additions and 52 deletions
+7 -2
View File
@@ -8,6 +8,7 @@ import BufferService from "../service/infrastructure/bufferService.js";
import StatusService from "../service/infrastructure/statusService.js";
import NotificationUtils from "../service/infrastructure/notificationUtils.js";
import NotificationService from "../service/infrastructure/notificationService.js";
import { NotificationsService } from "@/service/index.js";
import ErrorService from "../service/infrastructure/errorService.js";
import SuperSimpleQueueHelper from "../service/infrastructure/SuperSimpleQueue/SuperSimpleQueueHelper.js";
import SuperSimpleQueue from "../service/infrastructure/SuperSimpleQueue/SuperSimpleQueue.js";
@@ -206,9 +207,9 @@ export const initializeServices = async ({
checksRepository,
});
const bufferService = new BufferService({ db, logger, envSettings, incidentService, checkService });
const bufferService = new BufferService({ logger, incidentService, checkService });
const statusService = new StatusService({ db, logger, buffer: bufferService, incidentService, monitorsRepository });
const statusService = new StatusService({ db, logger, buffer: bufferService, monitorsRepository });
const notificationUtils = new NotificationUtils({
stringService,
@@ -225,12 +226,15 @@ export const initializeServices = async ({
notificationUtils,
});
const notificationsService = new NotificationsService();
const superSimpleQueueHelper = new SuperSimpleQueueHelper({
db,
logger,
networkService,
statusService,
notificationService,
notificationsService,
checkService,
buffer: bufferService,
});
@@ -317,6 +321,7 @@ export const initializeServices = async ({
invitesRepository,
recoveryTokensRepository,
settingsRepository,
notificationsService,
};
Object.values(services).forEach((service) => {
+1
View File
@@ -2,3 +2,4 @@ export * from "@/service/business/monitorService.js";
export * from "@/service/infrastructure/networkService.js";
export * from "@/service/infrastructure/notificationProviders/webhook.js";
export * from "@/service/infrastructure/notificationProviders/INotificationProvider.js";
export * from "@/service/infrastructure/notificationsService.js";
@@ -1,7 +1,7 @@
const SERVICE_NAME = "JobQueueHelper";
import type { Monitor } from "@/types/monitor.js";
import { AppError } from "@/utils/AppError.js";
import { INetworkService } from "@/service/index.js";
import { INetworkService, INotificationsService } from "@/service/index.js";
class SuperSimpleQueueHelper {
static SERVICE_NAME = SERVICE_NAME;
@@ -10,6 +10,7 @@ class SuperSimpleQueueHelper {
private networkService: INetworkService;
private statusService: any;
private notificationService: any;
private notificationsService: INotificationsService;
private checkService: any;
private buffer: any;
@@ -19,6 +20,7 @@ class SuperSimpleQueueHelper {
networkService,
statusService,
notificationService,
notificationsService,
checkService,
buffer,
}: {
@@ -27,6 +29,7 @@ class SuperSimpleQueueHelper {
networkService: INetworkService;
statusService: any;
notificationService: any;
notificationsService: INotificationsService;
checkService: any;
buffer: any;
}) {
@@ -37,6 +40,7 @@ class SuperSimpleQueueHelper {
this.notificationService = notificationService;
this.checkService = checkService;
this.buffer = buffer;
this.notificationsService = notificationsService;
}
get serviceName() {
@@ -78,22 +82,27 @@ class SuperSimpleQueueHelper {
// Step 4. Update monitor status
const statusChangeResult = await this.statusService.updateMonitorStatus(status, check);
this.notificationService
.handleNotifications({
...status,
monitor: statusChangeResult.monitor,
prevStatus: statusChangeResult.prevStatus,
statusChanged: statusChangeResult.statusChanged,
})
.catch((error: any) => {
this.logger.error({
message: error.message,
service: SERVICE_NAME,
method: "getMonitorJob",
details: `Error sending notifications for job ${monitor.id}: ${error.message}`,
stack: error.stack,
});
});
// Step 5 handle incidents and notifications
if (statusChangeResult.statusChanged === true) {
this.notificationsService.handleNotifications(monitor, status);
}
// this.notificationService
// .handleNotifications({
// ...status,
// monitor: statusChangeResult.monitor,
// prevStatus: statusChangeResult.prevStatus,
// statusChanged: statusChangeResult.statusChanged,
// })
// .catch((error: any) => {
// this.logger.error({
// message: error.message,
// service: SERVICE_NAME,
// method: "getMonitorJob",
// details: `Error sending notifications for job ${monitor.id}: ${error.message}`,
// stack: error.stack,
// });
// });
} catch (error: any) {
this.logger.warn({
message: error.message,
@@ -6,7 +6,6 @@ const SERVICE_NAME = "BufferService";
class BufferService {
static SERVICE_NAME = SERVICE_NAME;
private BUFFER_TIMEOUT: number;
private db: any;
private logger: any;
private incidentService: any;
private SERVICE_NAME: string;
@@ -15,21 +14,8 @@ class BufferService {
private bufferTimer: NodeJS.Timeout | null = null;
private checksService: any;
constructor({
db,
logger,
envSettings,
incidentService,
checkService,
}: {
db: any;
logger: any;
envSettings: any;
incidentService: any;
checkService: any;
}) {
constructor({ logger, incidentService, checkService }: { logger: any; incidentService: any; checkService: any }) {
this.BUFFER_TIMEOUT = config.NODE_ENV === "development" ? 10 : 1000 * 60 * 1; // 1 minute
this.db = db;
this.logger = logger;
this.incidentService = incidentService;
this.checksService = checkService;
@@ -1,4 +1,11 @@
import type { Monitor } from "@/types/index.js";
interface INotificationSerivce {
handleNotifications: (moniotr: Monitor) => Promise<void>;
import type { Monitor, MonitorStatusResponse } from "@/types/index.js";
export interface INotificationsService {
handleNotifications: (monitor: Monitor, monitorStatusResponse: MonitorStatusResponse) => Promise<void>;
}
export class NotificationsService implements INotificationsService {
handleNotifications = async (monitor: Monitor, monitorStatusResponse: MonitorStatusResponse) => {
console.log(JSON.stringify(monitor, null, 2));
console.log(JSON.stringify(monitorStatusResponse, null, 2));
};
}
@@ -17,26 +17,12 @@ class StatusService {
private db: any;
private logger: any;
private buffer: any;
private incidentService: any;
private monitorsRepository: IMonitorsRepository;
constructor({
db,
logger,
buffer,
incidentService,
monitorsRepository,
}: {
db: any;
logger: any;
buffer: any;
incidentService: any;
monitorsRepository: IMonitorsRepository;
}) {
constructor({ db, logger, buffer, monitorsRepository }: { db: any; logger: any; buffer: any; monitorsRepository: IMonitorsRepository }) {
this.db = db;
this.logger = logger;
this.buffer = buffer;
this.incidentService = incidentService;
this.monitorsRepository = monitorsRepository;
}