parallelize notifications

This commit is contained in:
Alex Holliday committed 2025-11-26 19:26:16 +00:00
1 parent 92750ce8f9
commit 4e8281aefb
3 files changed
+40 -9

No files matched your search

+3
View File
@@ -49,6 +49,9 @@ const createApp = async () => {
app.listen(port, "0.0.0.0", () => { app.listen(port, "0.0.0.0", () => {
indexLogger.info(`Server is running on http://localhost:${port}`); indexLogger.info(`Server is running on http://localhost:${port}`);
indexLogger.error(new ApiError("Test error logging", 500)); indexLogger.error(new ApiError("Test error logging", 500));
indexLogger.warn("test", {
testKey: "testValue",
});
}); });
}; };
+3
View File
@@ -11,6 +11,9 @@ const buildLogMsg = (info: winston.Logform.TransformableInfo) => {
const message = info.message; const message = info.message;
const service = info.service; const service = info.service;
const stack = info.stack || ""; const stack = info.stack || "";
const { ...meta } = info;
const metaString =
Object.keys(meta).length > 0 ? ` | meta: ${JSON.stringify(meta)}` : "";
return `[${timestamp}]${ return `[${timestamp}]${
service ? ` [${service}]` : "" service ? ` [${service}]` : ""
} ${level}: ${message} ${stack}`; } ${level}: ${message} ${stack}`;
@@ -69,39 +69,64 @@ class NotificationService implements INotificationService {
_id: { $in: notificationIds }, _id: { $in: notificationIds },
}); });
for (const channel of notificationChannels) { const tasks = notificationChannels.map((channel) =>
// Implement sending logic based on channel.type and channel.config this.sendForChannel(channel, monitor, incident)
let service; );
const results = await Promise.allSettled(tasks);
const succeeded = results.filter((r) => r.status === "fulfilled").length;
const failed = results.filter((r) => r.status === "rejected");
if (failed.length > 0) {
logger.warn(
`Notification send completed with ${succeeded} success, ${failed.length} failure(s)`
);
failed.forEach((r) => {
logger.debug(
"Notification send failure",
(r as PromiseRejectedResult).reason
);
});
}
return;
};
private sendForChannel = async (
channel: INotificationChannel,
monitor: IMonitor,
incident: IIncident
): Promise<void> => {
switch (channel.type) { switch (channel.type) {
case "email": case "email":
await this.emailService.sendMessage( await this.emailService.sendMessage(
this.emailService.buildAlert(monitor, incident), this.emailService.buildAlert(monitor, incident),
channel channel
); );
break; return;
case "slack": case "slack":
await this.slackService.sendMessage( await this.slackService.sendMessage(
this.slackService.buildAlert(monitor, incident), this.slackService.buildAlert(monitor, incident),
channel channel
); );
break; return;
case "discord": case "discord":
await this.discordService.sendMessage( await this.discordService.sendMessage(
this.discordService.buildAlert(monitor, incident), this.discordService.buildAlert(monitor, incident),
channel channel
); );
break; return;
case "webhook": case "webhook":
await this.webhookService.sendMessage( await this.webhookService.sendMessage(
this.webhookService.buildAlert(monitor, incident), this.webhookService.buildAlert(monitor, incident),
channel channel
); );
break; return;
default: default:
logger.warn(`Unknown notification channel type: ${channel.type}`); logger.warn(`Unknown notification channel type: ${channel.type}`);
}
}
return; return;
}
}; };
private testNotification = async ( private testNotification = async (