Skip to content

Commit

Permalink
fix: restore previous scenario state when its run can't be started
Browse files Browse the repository at this point in the history
  • Loading branch information
csm-thu committed Apr 26, 2024
1 parent 1b31032 commit c58c616
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions src/state/sagas/scenario/LaunchScenario/LaunchScenario.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Cosmo Tech.
// Licensed under the MIT license.
import { t } from 'i18next';
import { takeEvery, call, put } from 'redux-saga/effects';
import { call, put, select, takeEvery } from 'redux-saga/effects';
import { AppInsights } from '../../../../services/AppInsights';
import { Api } from '../../../../services/config/Api';
import { SCENARIO_RUN_STATE } from '../../../../services/config/ApiConstants';
Expand All @@ -10,6 +10,7 @@ import { SCENARIO_ACTIONS_KEY } from '../../../commons/ScenarioConstants';
import { dispatchSetApplicationErrorMessage } from '../../../dispatchers/app/ApplicationDispatcher';

const appInsights = AppInsights.getInstance();
const getScenarios = (state) => state.scenario?.list?.data;

export function* launchScenario(action) {
try {
Expand All @@ -19,13 +20,36 @@ export function* launchScenario(action) {
const scenarioId = action.scenarioId;
const runStartTime = new Date().getTime();

const scenarios = yield select(getScenarios);
const scenario = scenarios?.find((item) => item.id === scenarioId);
if (scenario === undefined) console.warn(`Couldn't retrieve scenario with id "${scenarioId}"`);
const previousScenarioState = scenario?.scenarioState;

yield put({
type: SCENARIO_ACTIONS_KEY.SET_CURRENT_SCENARIO,
scenario: { state: SCENARIO_RUN_STATE.RUNNING },
});

// Launch scenario if parameters update succeeded
const response = yield call(Api.ScenarioRuns.runScenario, organizationId, workspaceId, scenarioId);
let response;
try {
response = yield call(Api.ScenarioRuns.runScenario, organizationId, workspaceId, scenarioId);
} catch (error) {
console.error(error);
yield put(
dispatchSetApplicationErrorMessage(
error,
t('commoncomponents.banner.run', 'A problem occurred when starting the scenario run.')
)
);
yield put({
type: SCENARIO_ACTIONS_KEY.SET_CURRENT_SCENARIO,
status: STATUSES.ERROR,
scenario: { state: previousScenarioState }, // Do not force scenario state to "Failed", restore previous state
});
return;
}

const newLastRun = {
csmSimulationRun: response?.data?.csmSimulationRun,
scenarioRunId: response?.data?.id,
Expand Down

0 comments on commit c58c616

Please sign in to comment.