refactor: Update Monitor.ts to improve error handling and populate secrets

This commit refactors the Monitor.ts file to enhance error handling and populate secrets for monitors. It introduces try-catch blocks and proper logging to handle errors more effectively. Additionally, it populates secrets for monitors using the MonitorUtil.populateSecrets() function. These changes improve the reliability and security of the monitoring process.
This commit is contained in:
Simon Larsen 2024-07-05 12:56:53 +01:00
parent 97306d47fa
commit 5b00b5dac1
No known key found for this signature in database
GPG Key ID: 96C5DCA24769DBCA

View File

@ -275,6 +275,8 @@ router.post(
// update the lastMonitoredAt field of the monitors
const updatePromises: Array<Promise<void>> = [];
for (const monitorProbe of monitorProbes) {
if (!monitorProbe.monitor) {
continue;
@ -293,18 +295,22 @@ router.post(
logger.error(err);
}
await MonitorProbeService.updateOneById({
id: monitorProbe.id!,
data: {
lastPingAt: OneUptimeDate.getCurrentDate(),
nextPingAt: nextPing,
},
props: {
isRoot: true,
},
});
updatePromises.push(
MonitorProbeService.updateOneById({
id: monitorProbe.id!,
data: {
lastPingAt: OneUptimeDate.getCurrentDate(),
nextPingAt: nextPing,
},
props: {
isRoot: true,
},
}),
);
}
await Promise.all(updatePromises);
if (mutex) {
try {
await Semaphore.release(mutex);
@ -326,20 +332,26 @@ router.post(
// check if the monitor needs secrets to be filled.
const monitorsWithSecretPopulated: Array<Monitor> = [];
let monitorsWithSecretPopulated: Array<Monitor> = [];
const monitorWithSecretsPopulatePromises: Array<Promise<Monitor>> = [];
for (const monitor of monitors) {
const monitorWithSecrets: Monitor =
await MonitorUtil.populateSecrets(monitor);
monitorsWithSecretPopulated.push(monitorWithSecrets);
monitorWithSecretsPopulatePromises.push(
MonitorUtil.populateSecrets(monitor),
);
}
monitorsWithSecretPopulated = await Promise.all(
monitorWithSecretsPopulatePromises,
);
logger.debug("Populated secrets");
logger.debug(monitorsWithSecretPopulated);
// return the list of monitors to be monitored
logger.debug("Sending response");
return Response.sendEntityArrayResponse(
req,
res,