From 601a1bad729299075f4cff2d262e54c00982690e Mon Sep 17 00:00:00 2001 From: Guy Abramovich Date: Thu, 21 Sep 2023 19:00:01 +0300 Subject: [PATCH] Code review fixes --- .../models/database/databaseConnector.js | 5 --- src/jobs/models/jobExperimentsHandler.js | 36 ++++++++++--------- 2 files changed, 20 insertions(+), 21 deletions(-) diff --git a/src/chaos-experiments/models/database/databaseConnector.js b/src/chaos-experiments/models/database/databaseConnector.js index e224d2a2..a12f6d70 100644 --- a/src/chaos-experiments/models/database/databaseConnector.js +++ b/src/chaos-experiments/models/database/databaseConnector.js @@ -5,7 +5,6 @@ module.exports = { getAllChaosExperiments, insertChaosExperiment, getChaosExperimentById, - getChaosExperimentsByIds, getChaosExperimentByName, getChaosExperimentsByIds, deleteChaosExperiment, @@ -45,10 +44,6 @@ async function getChaosExperimentByName(name, contextId) { return databaseConnector.getChaosExperimentByName(name, contextId); } -async function getChaosExperimentsByIds(experimentIds, contextId) { - return databaseConnector.getChaosExperimentsByIds(experimentIds, contextId); -} - async function updateChaosExperiment(experimentId, updatedProcessor) { return databaseConnector.updateChaosExperiment(experimentId, updatedProcessor); } diff --git a/src/jobs/models/jobExperimentsHandler.js b/src/jobs/models/jobExperimentsHandler.js index f3eff5bc..919bb112 100644 --- a/src/jobs/models/jobExperimentsHandler.js +++ b/src/jobs/models/jobExperimentsHandler.js @@ -16,27 +16,31 @@ async function setChaosExperimentsIfExist(jobId, jobExperiments) { try { const baseTimestamp = Date.now(); const experimentIds = jobExperiments.map(experiment => experiment.experiment_id); - const experiments = await chaosExperimentsDbConnector.getChaosExperimentsByIds(experimentIds); - for (const experimentRequest of jobExperiments) { - try { - const experiment = experiments.find(e => e.id === experimentRequest.experiment_id); - const startTime = baseTimestamp + experimentRequest.start_after; - const endTime = startTime + convertDurationStringToMillisecond(experiment.kubeObject.spec.duration); - const jobExperimentId = uuid(); - await chaosExperimentsDbConnector.insertChaosJobExperiment(jobExperimentId, jobId, experiment.id, startTime, endTime); - const kubeObject = experiment.kubeObject; - kubeObject.name = kubeObject.metadata.name.concat(`_${jobExperimentId}`); - const timeout = setTimeout(() => runChaosExperiment(kubeObject, jobId, jobExperimentId), experimentRequest.start_after); - jobExperimentsIdToTimeout.set(jobExperimentId, timeout); - } catch (error){ - logger.error(error, `error while setting chaos experiment ${experimentRequest.experiment_id} for job ${jobId}`); - } - } + const experimentsFromDb = await chaosExperimentsDbConnector.getChaosExperimentsByIds(experimentIds); + await Promise.all(jobExperiments.map(async(experimentRequest) => + await setSingleJobExperiment(experimentRequest, experimentsFromDb, baseTimestamp, jobId) + )); } catch (error){ logger.error(error, `error while setting chaos experiments for job ${jobId}`); } }; +async function setSingleJobExperiment(experimentRequest, experimentsFromDb, baseTimestamp, jobId) { + try { + const experiment = experimentsFromDb.find(e => e.id === experimentRequest.experiment_id); + const startTime = baseTimestamp + experimentRequest.start_after; + const endTime = startTime + convertDurationStringToMillisecond(experiment.kubeObject.spec.duration); + const jobExperimentId = uuid(); + await chaosExperimentsDbConnector.insertChaosJobExperiment(jobExperimentId, jobId, experiment.id, startTime, endTime); + const kubeObject = experiment.kubeObject; + kubeObject.name = kubeObject.metadata.name.concat(`_${jobExperimentId}`); + const timeout = setTimeout(() => runChaosExperiment(kubeObject, jobId, jobExperimentId), experimentRequest.start_after); + jobExperimentsIdToTimeout.set(jobExperimentId, timeout); + } catch (error){ + logger.error(error, `error while setting chaos experiment ${experimentRequest.experiment_id} for job ${jobId}`); + } +} + async function runChaosExperiment(kubeObject, jobId, jobExperimentId) { }