parallelization

This commit is contained in:
Alex Holliday committed 2025-11-26 19:56:47 +00:00
1 parent 4e8281aefb
commit cb5d8c3468
1 file changed
+43 -30
@@ -73,21 +73,14 @@ class NotificationService implements INotificationService {
this.sendForChannel(channel, monitor, incident) this.sendForChannel(channel, monitor, incident)
); );
const results = await Promise.allSettled(tasks); const outcomes = await Promise.all(tasks);
const succeeded = outcomes.filter(Boolean).length;
const failed = outcomes.length - succeeded;
const succeeded = results.filter((r) => r.status === "fulfilled").length; if (failed > 0) {
const failed = results.filter((r) => r.status === "rejected");
if (failed.length > 0) {
logger.warn( logger.warn(
`Notification send completed with ${succeeded} success, ${failed.length} failure(s)` `Notification send completed with ${succeeded} success, ${failed} failure(s)`
); );
failed.forEach((r) => {
logger.debug(
"Notification send failure",
(r as PromiseRejectedResult).reason
);
});
} }
return; return;
@@ -97,35 +90,45 @@ class NotificationService implements INotificationService {
channel: INotificationChannel, channel: INotificationChannel,
monitor: IMonitor, monitor: IMonitor,
incident: IIncident incident: IIncident
): Promise<void> => { ): Promise<boolean> => {
try {
switch (channel.type) { switch (channel.type) {
case "email": case "email": {
await this.emailService.sendMessage( const sent = await this.emailService.sendMessage(
this.emailService.buildAlert(monitor, incident), this.emailService.buildAlert(monitor, incident),
channel channel
); );
return; return Boolean(sent);
case "slack": }
await this.slackService.sendMessage( case "slack": {
const sent = await this.slackService.sendMessage(
this.slackService.buildAlert(monitor, incident), this.slackService.buildAlert(monitor, incident),
channel channel
); );
return; return Boolean(sent);
case "discord": }
await this.discordService.sendMessage( case "discord": {
const sent = await this.discordService.sendMessage(
this.discordService.buildAlert(monitor, incident), this.discordService.buildAlert(monitor, incident),
channel channel
); );
return; return Boolean(sent);
case "webhook": }
await this.webhookService.sendMessage( case "webhook": {
const sent = await this.webhookService.sendMessage(
this.webhookService.buildAlert(monitor, incident), this.webhookService.buildAlert(monitor, incident),
channel channel
); );
return; return Boolean(sent);
default: }
default: {
logger.warn(`Unknown notification channel type: ${channel.type}`); logger.warn(`Unknown notification channel type: ${channel.type}`);
return; return false;
}
}
} catch (error) {
logger.debug("Notification send failure", error);
return false;
} }
}; };
@@ -195,10 +198,20 @@ class NotificationService implements INotificationService {
_id: { $in: notificationIds }, _id: { $in: notificationIds },
}).lean(); }).lean();
const results: any[] = []; if (notificationChannels.length === 0) {
return [];
}
for (const channel of notificationChannels) { const results: any[] = [];
await this.testNotification(channel, results); await Promise.all(
notificationChannels.map((ch) => this.testNotification(ch, results))
);
const succeeded = results.filter((r) => r.sent).length;
const failed = results.length - succeeded;
if (failed > 0) {
logger.warn(
`Notification channel test completed with ${succeeded} success, ${failed} failure(s)`
);
} }
return results; return results;
}; };