parallelize notifications
This commit is contained in:
1 parent
92750ce8f9
commit
4e8281aefb
3 files changed
+62
-31
No files matched your search
@@ -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",
|
||||||
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -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,41 +69,66 @@ 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;
|
);
|
||||||
switch (channel.type) {
|
|
||||||
case "email":
|
const results = await Promise.allSettled(tasks);
|
||||||
await this.emailService.sendMessage(
|
|
||||||
this.emailService.buildAlert(monitor, incident),
|
const succeeded = results.filter((r) => r.status === "fulfilled").length;
|
||||||
channel
|
const failed = results.filter((r) => r.status === "rejected");
|
||||||
);
|
|
||||||
break;
|
if (failed.length > 0) {
|
||||||
case "slack":
|
logger.warn(
|
||||||
await this.slackService.sendMessage(
|
`Notification send completed with ${succeeded} success, ${failed.length} failure(s)`
|
||||||
this.slackService.buildAlert(monitor, incident),
|
);
|
||||||
channel
|
failed.forEach((r) => {
|
||||||
);
|
logger.debug(
|
||||||
break;
|
"Notification send failure",
|
||||||
case "discord":
|
(r as PromiseRejectedResult).reason
|
||||||
await this.discordService.sendMessage(
|
);
|
||||||
this.discordService.buildAlert(monitor, incident),
|
});
|
||||||
channel
|
|
||||||
);
|
|
||||||
break;
|
|
||||||
case "webhook":
|
|
||||||
await this.webhookService.sendMessage(
|
|
||||||
this.webhookService.buildAlert(monitor, incident),
|
|
||||||
channel
|
|
||||||
);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
logger.warn(`Unknown notification channel type: ${channel.type}`);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
private sendForChannel = async (
|
||||||
|
channel: INotificationChannel,
|
||||||
|
monitor: IMonitor,
|
||||||
|
incident: IIncident
|
||||||
|
): Promise<void> => {
|
||||||
|
switch (channel.type) {
|
||||||
|
case "email":
|
||||||
|
await this.emailService.sendMessage(
|
||||||
|
this.emailService.buildAlert(monitor, incident),
|
||||||
|
channel
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
case "slack":
|
||||||
|
await this.slackService.sendMessage(
|
||||||
|
this.slackService.buildAlert(monitor, incident),
|
||||||
|
channel
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
case "discord":
|
||||||
|
await this.discordService.sendMessage(
|
||||||
|
this.discordService.buildAlert(monitor, incident),
|
||||||
|
channel
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
case "webhook":
|
||||||
|
await this.webhookService.sendMessage(
|
||||||
|
this.webhookService.buildAlert(monitor, incident),
|
||||||
|
channel
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
default:
|
||||||
|
logger.warn(`Unknown notification channel type: ${channel.type}`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
private testNotification = async (
|
private testNotification = async (
|
||||||
channel: INotificationChannel,
|
channel: INotificationChannel,
|
||||||
results: any[]
|
results: any[]
|
||||||
|
|||||||
Reference in new issue
Block a user