extract reactors and pipeline
This commit is contained in:
1 parent
7ee30e12f5
commit
ee9c08b1bb
9 files changed
+380
-234
No files matched your search
@@ -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);
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user