From 5b00b5dac10086164a3a6fc410a5276297f7915c Mon Sep 17 00:00:00 2001 From: Simon Larsen Date: Fri, 5 Jul 2024 12:56:53 +0100 Subject: [PATCH] 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. --- Ingestor/API/Monitor.ts | 42 ++++++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/Ingestor/API/Monitor.ts b/Ingestor/API/Monitor.ts index ad0d8f31b0..607b6f9100 100644 --- a/Ingestor/API/Monitor.ts +++ b/Ingestor/API/Monitor.ts @@ -275,6 +275,8 @@ router.post( // update the lastMonitoredAt field of the monitors + const updatePromises: Array> = []; + 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 = []; + let monitorsWithSecretPopulated: Array = []; + const monitorWithSecretsPopulatePromises: Array> = []; 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,