Skip to content

Commit

Permalink
Code review fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
guy-abramovich-payu-gpo committed Sep 21, 2023
1 parent 30c4aa9 commit 601a1ba
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 21 deletions.
5 changes: 0 additions & 5 deletions src/chaos-experiments/models/database/databaseConnector.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ module.exports = {
getAllChaosExperiments,
insertChaosExperiment,
getChaosExperimentById,
getChaosExperimentsByIds,
getChaosExperimentByName,
getChaosExperimentsByIds,
deleteChaosExperiment,
Expand Down Expand Up @@ -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);
}
Expand Down
36 changes: 20 additions & 16 deletions src/jobs/models/jobExperimentsHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

}
Expand Down

0 comments on commit 601a1ba

Please sign in to comment.