parallelization
This commit is contained in:
1 parent
4e8281aefb
commit
cb5d8c3468
1 file changed
+57
-44
@@ -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> => {
|
||||||
switch (channel.type) {
|
try {
|
||||||
case "email":
|
switch (channel.type) {
|
||||||
await this.emailService.sendMessage(
|
case "email": {
|
||||||
this.emailService.buildAlert(monitor, incident),
|
const sent = await this.emailService.sendMessage(
|
||||||
channel
|
this.emailService.buildAlert(monitor, incident),
|
||||||
);
|
channel
|
||||||
return;
|
);
|
||||||
case "slack":
|
return Boolean(sent);
|
||||||
await this.slackService.sendMessage(
|
}
|
||||||
this.slackService.buildAlert(monitor, incident),
|
case "slack": {
|
||||||
channel
|
const sent = await this.slackService.sendMessage(
|
||||||
);
|
this.slackService.buildAlert(monitor, incident),
|
||||||
return;
|
channel
|
||||||
case "discord":
|
);
|
||||||
await this.discordService.sendMessage(
|
return Boolean(sent);
|
||||||
this.discordService.buildAlert(monitor, incident),
|
}
|
||||||
channel
|
case "discord": {
|
||||||
);
|
const sent = await this.discordService.sendMessage(
|
||||||
return;
|
this.discordService.buildAlert(monitor, incident),
|
||||||
case "webhook":
|
channel
|
||||||
await this.webhookService.sendMessage(
|
);
|
||||||
this.webhookService.buildAlert(monitor, incident),
|
return Boolean(sent);
|
||||||
channel
|
}
|
||||||
);
|
case "webhook": {
|
||||||
return;
|
const sent = await this.webhookService.sendMessage(
|
||||||
default:
|
this.webhookService.buildAlert(monitor, incident),
|
||||||
logger.warn(`Unknown notification channel type: ${channel.type}`);
|
channel
|
||||||
return;
|
);
|
||||||
|
return Boolean(sent);
|
||||||
|
}
|
||||||
|
default: {
|
||||||
|
logger.warn(`Unknown notification channel type: ${channel.type}`);
|
||||||
|
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;
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in new issue
Block a user