inject logger
This commit is contained in:
1 parent
7c9410f9d7
commit
bebb5d6942
9 files changed
+125
-18
No files matched your search
@@ -228,12 +228,12 @@ export const initializeServices = async ({
|
|||||||
settingsService,
|
settingsService,
|
||||||
});
|
});
|
||||||
|
|
||||||
const webhookProvider = new WebhookProvider();
|
const webhookProvider = new WebhookProvider(logger);
|
||||||
const slackProvider = new SlackProvider();
|
const slackProvider = new SlackProvider(logger);
|
||||||
const emailProvider = new EmailProvider(emailService);
|
const emailProvider = new EmailProvider(emailService, logger);
|
||||||
const discordProvider = new DiscordProvider();
|
const discordProvider = new DiscordProvider(logger);
|
||||||
const pagerDutyProvider = new PagerDutyProvider();
|
const pagerDutyProvider = new PagerDutyProvider(logger);
|
||||||
const matrixProvider = new MatrixProvider();
|
const matrixProvider = new MatrixProvider(logger);
|
||||||
|
|
||||||
const notificationsService = new NotificationsService(
|
const notificationsService = new NotificationsService(
|
||||||
notificationsRepository,
|
notificationsRepository,
|
||||||
|
|||||||
@@ -18,7 +18,6 @@ class SuperSimpleQueueHelper {
|
|||||||
logger,
|
logger,
|
||||||
networkService,
|
networkService,
|
||||||
statusService,
|
statusService,
|
||||||
notificationService,
|
|
||||||
notificationsService,
|
notificationsService,
|
||||||
checkService,
|
checkService,
|
||||||
buffer,
|
buffer,
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
import type { Monitor, Notification, MonitorStatusResponse } from "@/types/index.js";
|
import type { Monitor, Notification, MonitorStatusResponse } from "@/types/index.js";
|
||||||
import { INotificationProvider } from "@/service/index.js";
|
import { INotificationProvider } from "@/service/index.js";
|
||||||
import { buildHardwareAlerts, buildDiscordBody } from "@/service/infrastructure/notificationProviders/utils.js";
|
import { buildHardwareAlerts, buildDiscordBody, getTestMessage } from "@/service/infrastructure/notificationProviders/utils.js";
|
||||||
import got from "got";
|
import got from "got";
|
||||||
|
|
||||||
export class DiscordProvider implements INotificationProvider {
|
export class DiscordProvider implements INotificationProvider {
|
||||||
|
constructor(private logger: any) {}
|
||||||
private getHardwareContent = (monitor: Monitor, monitorStatusResponse: MonitorStatusResponse) => {
|
private getHardwareContent = (monitor: Monitor, monitorStatusResponse: MonitorStatusResponse) => {
|
||||||
const { discordPayload } = buildHardwareAlerts("HOST_PLACEHOLDER", monitor, monitorStatusResponse);
|
const { discordPayload } = buildHardwareAlerts("HOST_PLACEHOLDER", monitor, monitorStatusResponse);
|
||||||
return discordPayload;
|
return discordPayload;
|
||||||
@@ -22,6 +23,7 @@ export class DiscordProvider implements INotificationProvider {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
this.logger?.debug?.("Sending Discord alert", { address: notification.address });
|
||||||
await got.post(notification.address, {
|
await got.post(notification.address, {
|
||||||
json: body,
|
json: body,
|
||||||
headers: {
|
headers: {
|
||||||
@@ -31,10 +33,27 @@ export class DiscordProvider implements INotificationProvider {
|
|||||||
});
|
});
|
||||||
return true;
|
return true;
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
|
this.logger?.error?.("Discord alert failed", { error });
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
sendTestAlert = async (notification: Notification) => {
|
sendTestAlert = async (notification: Notification) => {
|
||||||
return false;
|
if (!notification.address) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
this.logger?.debug?.("Sending Discord test alert", { address: notification.address });
|
||||||
|
await got.post(notification.address, {
|
||||||
|
json: { content: getTestMessage() },
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
},
|
||||||
|
responseType: "json",
|
||||||
|
});
|
||||||
|
return true;
|
||||||
|
} catch (error) {
|
||||||
|
this.logger?.error?.("Discord test alert failed", { error });
|
||||||
|
return false;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -4,7 +4,7 @@ import { buildHardwareAlerts, buildHardwareEmail, buildEmail, buildTestEmail } f
|
|||||||
|
|
||||||
export class EmailProvider implements INotificationProvider {
|
export class EmailProvider implements INotificationProvider {
|
||||||
private emailService: any;
|
private emailService: any;
|
||||||
constructor(emailService: any) {
|
constructor(emailService: any, private logger: any) {
|
||||||
this.emailService = emailService;
|
this.emailService = emailService;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -27,6 +27,7 @@ export class EmailProvider implements INotificationProvider {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.logger?.debug?.("Sending Email alert", { address: notification.address });
|
||||||
const messageId = await this.emailService.sendEmail(notification.address, subject, html);
|
const messageId = await this.emailService.sendEmail(notification.address, subject, html);
|
||||||
if (!messageId) return false;
|
if (!messageId) return false;
|
||||||
return true;
|
return true;
|
||||||
@@ -35,6 +36,7 @@ export class EmailProvider implements INotificationProvider {
|
|||||||
async sendTestAlert(notification: Notification): Promise<boolean> {
|
async sendTestAlert(notification: Notification): Promise<boolean> {
|
||||||
const subject = "Test notification";
|
const subject = "Test notification";
|
||||||
const html = await buildTestEmail(this.emailService);
|
const html = await buildTestEmail(this.emailService);
|
||||||
|
this.logger?.debug?.("Sending Email test alert", { address: notification.address });
|
||||||
const messageId = await this.emailService.sendEmail(notification.address, subject, html);
|
const messageId = await this.emailService.sendEmail(notification.address, subject, html);
|
||||||
if (!messageId) return false;
|
if (!messageId) return false;
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
@@ -1,9 +1,15 @@
|
|||||||
import got from "got";
|
import got from "got";
|
||||||
import type { INotificationProvider } from "@/service/index.js";
|
import type { INotificationProvider } from "@/service/index.js";
|
||||||
import type { Notification, Monitor, MonitorStatusResponse } from "@/types/index.js";
|
import type { Notification, Monitor, MonitorStatusResponse } from "@/types/index.js";
|
||||||
import { buildHardwareAlerts, buildHardwareWebhookBody, buildWebhookBody } from "@/service/infrastructure/notificationProviders/utils.js";
|
import {
|
||||||
|
buildHardwareAlerts,
|
||||||
|
buildHardwareWebhookBody,
|
||||||
|
buildWebhookBody,
|
||||||
|
getTestMessage,
|
||||||
|
} from "@/service/infrastructure/notificationProviders/utils.js";
|
||||||
|
|
||||||
export class MatrixProvider implements INotificationProvider {
|
export class MatrixProvider implements INotificationProvider {
|
||||||
|
constructor(private logger: any) {}
|
||||||
private getHardwareContent = (monitor: Monitor, monitorStatusResponse: MonitorStatusResponse) => {
|
private getHardwareContent = (monitor: Monitor, monitorStatusResponse: MonitorStatusResponse) => {
|
||||||
const { alertsToSend } = buildHardwareAlerts("HOST_PLACEHOLDER", monitor, monitorStatusResponse);
|
const { alertsToSend } = buildHardwareAlerts("HOST_PLACEHOLDER", monitor, monitorStatusResponse);
|
||||||
const body = buildHardwareWebhookBody(alertsToSend, monitor);
|
const body = buildHardwareWebhookBody(alertsToSend, monitor);
|
||||||
@@ -37,6 +43,7 @@ export class MatrixProvider implements INotificationProvider {
|
|||||||
formatted_body: formattedMessage,
|
formatted_body: formattedMessage,
|
||||||
};
|
};
|
||||||
try {
|
try {
|
||||||
|
this.logger?.debug?.("Sending Matrix alert", { room: roomId });
|
||||||
await got.post(url, {
|
await got.post(url, {
|
||||||
json: body,
|
json: body,
|
||||||
headers: {
|
headers: {
|
||||||
@@ -46,11 +53,34 @@ export class MatrixProvider implements INotificationProvider {
|
|||||||
});
|
});
|
||||||
return true;
|
return true;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
this.logger?.error?.("Matrix alert failed", { error });
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
sendTestAlert = async (notification: Notification) => {
|
sendTestAlert = async (notification: Notification) => {
|
||||||
return false;
|
const { homeserverUrl, accessToken, roomId } = notification;
|
||||||
|
if (!homeserverUrl || !accessToken || !roomId) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
const url = `${homeserverUrl}/_matrix/client/v3/rooms/${roomId}/send/m.room.message?access_token=${accessToken}`;
|
||||||
|
const body = {
|
||||||
|
msgtype: "m.text",
|
||||||
|
body: getTestMessage(),
|
||||||
|
};
|
||||||
|
try {
|
||||||
|
this.logger?.debug?.("Sending Matrix test alert", { room: roomId });
|
||||||
|
await got.post(url, {
|
||||||
|
json: body,
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
},
|
||||||
|
responseType: "json",
|
||||||
|
});
|
||||||
|
return true;
|
||||||
|
} catch (error) {
|
||||||
|
this.logger?.error?.("Matrix test alert failed", { error });
|
||||||
|
return false;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -1,9 +1,15 @@
|
|||||||
import got from "got";
|
import got from "got";
|
||||||
import type { Monitor, Notification, MonitorStatusResponse } from "@/types/index.js";
|
import type { Monitor, Notification, MonitorStatusResponse } from "@/types/index.js";
|
||||||
import { INotificationProvider } from "@/service/index.js";
|
import { INotificationProvider } from "@/service/index.js";
|
||||||
import { buildHardwareAlerts, buildHardwareWebhookBody, buildWebhookBody } from "@/service/infrastructure/notificationProviders/utils.js";
|
import {
|
||||||
|
buildHardwareAlerts,
|
||||||
|
buildHardwareWebhookBody,
|
||||||
|
buildWebhookBody,
|
||||||
|
getTestMessage,
|
||||||
|
} from "@/service/infrastructure/notificationProviders/utils.js";
|
||||||
|
|
||||||
export class PagerDutyProvider implements INotificationProvider {
|
export class PagerDutyProvider implements INotificationProvider {
|
||||||
|
constructor(private logger: any) {}
|
||||||
private getHardwareContent = (monitor: Monitor, monitorStatusResponse: MonitorStatusResponse) => {
|
private getHardwareContent = (monitor: Monitor, monitorStatusResponse: MonitorStatusResponse) => {
|
||||||
const { alertsToSend } = buildHardwareAlerts("HOST_PLACEHOLDER", monitor, monitorStatusResponse);
|
const { alertsToSend } = buildHardwareAlerts("HOST_PLACEHOLDER", monitor, monitorStatusResponse);
|
||||||
const body = buildHardwareWebhookBody(alertsToSend, monitor);
|
const body = buildHardwareWebhookBody(alertsToSend, monitor);
|
||||||
@@ -24,6 +30,7 @@ export class PagerDutyProvider implements INotificationProvider {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
this.logger?.debug?.("Sending PagerDuty alert", { routingKey: notification.address });
|
||||||
await got.post("https://events.pagerduty.com/v2/enqueue", {
|
await got.post("https://events.pagerduty.com/v2/enqueue", {
|
||||||
json: {
|
json: {
|
||||||
routing_key: notification.address,
|
routing_key: notification.address,
|
||||||
@@ -40,11 +47,31 @@ export class PagerDutyProvider implements INotificationProvider {
|
|||||||
|
|
||||||
return true;
|
return true;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
this.logger?.error?.("PagerDuty alert failed", { error });
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async sendTestAlert(notification: Notification): Promise<boolean> {
|
async sendTestAlert(notification: Notification): Promise<boolean> {
|
||||||
return false;
|
try {
|
||||||
|
this.logger?.debug?.("Sending PagerDuty test alert", { routingKey: notification.address });
|
||||||
|
await got.post("https://events.pagerduty.com/v2/enqueue", {
|
||||||
|
json: {
|
||||||
|
routing_key: notification.address,
|
||||||
|
event_action: "trigger",
|
||||||
|
payload: {
|
||||||
|
summary: getTestMessage(),
|
||||||
|
severity: "info",
|
||||||
|
source: "checkmate",
|
||||||
|
timestamp: new Date().toISOString(),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
responseType: "json",
|
||||||
|
});
|
||||||
|
return true;
|
||||||
|
} catch (error) {
|
||||||
|
this.logger?.error?.("PagerDuty test alert failed", { error });
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -9,6 +9,7 @@ import {
|
|||||||
import got from "got";
|
import got from "got";
|
||||||
|
|
||||||
export class SlackProvider implements INotificationProvider {
|
export class SlackProvider implements INotificationProvider {
|
||||||
|
constructor(private logger: any) {}
|
||||||
private getHardwareContent = (monitor: Monitor, monitorStatusResponse: MonitorStatusResponse) => {
|
private getHardwareContent = (monitor: Monitor, monitorStatusResponse: MonitorStatusResponse) => {
|
||||||
const { alertsToSend } = buildHardwareAlerts("HOST_PLACEHOLDER", monitor, monitorStatusResponse);
|
const { alertsToSend } = buildHardwareAlerts("HOST_PLACEHOLDER", monitor, monitorStatusResponse);
|
||||||
const body = buildHardwareWebhookBody(alertsToSend, monitor);
|
const body = buildHardwareWebhookBody(alertsToSend, monitor);
|
||||||
@@ -33,6 +34,7 @@ export class SlackProvider implements INotificationProvider {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
this.logger?.debug?.("Sending Slack alert", { address: notification.address });
|
||||||
await got.post(notification.address, {
|
await got.post(notification.address, {
|
||||||
json: { text: body },
|
json: { text: body },
|
||||||
headers: {
|
headers: {
|
||||||
@@ -42,6 +44,7 @@ export class SlackProvider implements INotificationProvider {
|
|||||||
});
|
});
|
||||||
return true;
|
return true;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
this.logger?.error?.("Slack alert failed", { error });
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -52,6 +55,7 @@ export class SlackProvider implements INotificationProvider {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
this.logger?.debug?.("Sending Slack test alert", { address: notification.address });
|
||||||
await got.post(notification.address, {
|
await got.post(notification.address, {
|
||||||
json: { text: getTestMessage() },
|
json: { text: getTestMessage() },
|
||||||
headers: {
|
headers: {
|
||||||
@@ -61,6 +65,7 @@ export class SlackProvider implements INotificationProvider {
|
|||||||
});
|
});
|
||||||
return true;
|
return true;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
this.logger?.error?.("Slack test alert failed", { error });
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,16 +1,22 @@
|
|||||||
import type { Monitor, Alert, Notification, MonitorStatusResponse } from "@/types/index.js";
|
import type { Monitor, Alert, Notification, MonitorStatusResponse } from "@/types/index.js";
|
||||||
import { INotificationProvider } from "@/service/index.js";
|
import { INotificationProvider } from "@/service/index.js";
|
||||||
import { buildHardwareAlerts, buildHardwareWebhookBody, buildWebhookBody } from "@/service/infrastructure/notificationProviders/utils.js";
|
import {
|
||||||
|
buildHardwareAlerts,
|
||||||
|
buildHardwareWebhookBody,
|
||||||
|
buildWebhookBody,
|
||||||
|
getTestMessage,
|
||||||
|
} from "@/service/infrastructure/notificationProviders/utils.js";
|
||||||
import got from "got";
|
import got from "got";
|
||||||
|
|
||||||
export class WebhookProvider implements INotificationProvider {
|
export class WebhookProvider implements INotificationProvider {
|
||||||
|
constructor(private logger: any) {}
|
||||||
private getHardwareContent = (monitor: Monitor, monitorStatusResponse: MonitorStatusResponse) => {
|
private getHardwareContent = (monitor: Monitor, monitorStatusResponse: MonitorStatusResponse) => {
|
||||||
const { alertsToSend } = buildHardwareAlerts("HOST_PLACEHOLDER", monitor, monitorStatusResponse);
|
const { alertsToSend } = buildHardwareAlerts("HOST_PLACEHOLDER", monitor, monitorStatusResponse);
|
||||||
const body = buildHardwareWebhookBody(alertsToSend, monitor);
|
const body = buildHardwareWebhookBody(alertsToSend, monitor);
|
||||||
return body;
|
return body;
|
||||||
};
|
};
|
||||||
|
|
||||||
private getContent = (monitor: Monitor, monitorStatusResponse: MonitorStatusResponse) => {
|
private getContent = (monitor: Monitor, monitorStatusResponse: MonitorStatusResponse): string => {
|
||||||
const body = buildWebhookBody(monitor, monitorStatusResponse);
|
const body = buildWebhookBody(monitor, monitorStatusResponse);
|
||||||
return body;
|
return body;
|
||||||
};
|
};
|
||||||
@@ -28,8 +34,9 @@ export class WebhookProvider implements INotificationProvider {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
this.logger?.debug?.("Sending webhook alert", { address: notification.address });
|
||||||
await got.post(notification.address, {
|
await got.post(notification.address, {
|
||||||
json: body,
|
json: { text: body },
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
},
|
},
|
||||||
@@ -37,11 +44,28 @@ export class WebhookProvider implements INotificationProvider {
|
|||||||
});
|
});
|
||||||
return true;
|
return true;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
this.logger?.error?.("Webhook alert failed", { error });
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
sendTestAlert = async (notification: Notification) => {
|
sendTestAlert = async (notification: Notification) => {
|
||||||
return false;
|
if (!notification.address) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
this.logger?.debug?.("Sending webhook test alert", { address: notification.address });
|
||||||
|
await got.post(notification.address, {
|
||||||
|
json: { text: getTestMessage() },
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
},
|
||||||
|
responseType: "json",
|
||||||
|
});
|
||||||
|
return true;
|
||||||
|
} catch (error) {
|
||||||
|
this.logger?.error?.("Webhook test alert failed", { error });
|
||||||
|
return false;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -140,6 +140,7 @@ export class NotificationsService implements INotificationsService {
|
|||||||
const notifications = await this.notificationsRepository.findNotificationsByIds(notificationIds);
|
const notifications = await this.notificationsRepository.findNotificationsByIds(notificationIds);
|
||||||
const tasks = notifications.map((notification) => this.sendTestNotification(notification));
|
const tasks = notifications.map((notification) => this.sendTestNotification(notification));
|
||||||
const outcomes = await Promise.all(tasks);
|
const outcomes = await Promise.all(tasks);
|
||||||
|
console.log(outcomes);
|
||||||
const succeeded = outcomes.filter(Boolean).length;
|
const succeeded = outcomes.filter(Boolean).length;
|
||||||
const failed = outcomes.length - succeeded;
|
const failed = outcomes.length - succeeded;
|
||||||
if (failed > 0) {
|
if (failed > 0) {
|
||||||
|
|||||||
Reference in new issue
Block a user