Skip to content
This repository has been archived by the owner on Dec 13, 2023. It is now read-only.

FIX: Switch Task remains in PENDING state when evaluator throws exception #3673

Merged
merged 1 commit into from
Jul 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,22 @@ public List<TaskModel> getMappedTasks(TaskMapperContext taskMapperContext) {
LOGGER.error(errorMsg);
throw new TerminateWorkflowException(errorMsg);
}
String evalResult = "" + evaluator.evaluate(workflowTask.getExpression(), taskInput);

String evalResult = "";
try {
evalResult = "" + evaluator.evaluate(workflowTask.getExpression(), taskInput);
} catch (Exception exception) {
TaskModel switchTask = taskMapperContext.createTaskModel();
switchTask.setTaskType(TaskType.TASK_TYPE_SWITCH);
switchTask.setTaskDefName(TaskType.TASK_TYPE_SWITCH);
switchTask.getInputData().putAll(taskInput);
switchTask.setStartTime(System.currentTimeMillis());
switchTask.setStatus(TaskModel.Status.FAILED);
switchTask.setReasonForIncompletion(exception.getMessage());
tasksToBeScheduled.add(switchTask);

return tasksToBeScheduled;
}

// QQ why is the case value and the caseValue passed and caseOutput passes as the same ??
TaskModel switchTask = taskMapperContext.createTaskModel();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,4 +242,66 @@ public void getMappedTasksWithValueParamEvaluator() {
assertEquals("switchTask", mappedTasks.get(0).getReferenceTaskName());
assertEquals("Foo", mappedTasks.get(1).getReferenceTaskName());
}

@Test
public void getMappedTasksWhenEvaluatorThrowsException() {

// Given
// Task Definition
TaskDef taskDef = new TaskDef();
Map<String, Object> inputMap = new HashMap<>();
List<Map<String, Object>> taskDefinitionInput = new LinkedList<>();
taskDefinitionInput.add(inputMap);

// Switch task instance
WorkflowTask switchTask = new WorkflowTask();
switchTask.setType(TaskType.SWITCH.name());
switchTask.setName("Switch");
switchTask.setTaskReferenceName("switchTask");
switchTask.setDefaultCase(Collections.singletonList(task1));
switchTask.setEvaluatorType(JavascriptEvaluator.NAME);
switchTask.setExpression("undefinedVariable");
Map<String, List<WorkflowTask>> decisionCases = new HashMap<>();
decisionCases.put("even", Collections.singletonList(task2));
switchTask.setDecisionCases(decisionCases);
// Workflow instance
WorkflowDef workflowDef = new WorkflowDef();
workflowDef.setSchemaVersion(2);

WorkflowModel workflowModel = new WorkflowModel();
workflowModel.setWorkflowDefinition(workflowDef);

Map<String, Object> body = new HashMap<>();
body.put("input", taskDefinitionInput);
taskDef.getInputTemplate().putAll(body);

Map<String, Object> input =
parametersUtils.getTaskInput(
switchTask.getInputParameters(), workflowModel, null, null);

TaskModel theTask = new TaskModel();
theTask.setReferenceTaskName("Foo");
theTask.setTaskId(idGenerator.generate());

when(deciderService.getTasksToBeScheduled(workflowModel, task2, 0, null))
.thenReturn(Collections.singletonList(theTask));

TaskMapperContext taskMapperContext =
TaskMapperContext.newBuilder()
.withWorkflowModel(workflowModel)
.withWorkflowTask(switchTask)
.withTaskInput(input)
.withRetryCount(0)
.withTaskId(idGenerator.generate())
.withDeciderService(deciderService)
.build();

// When
List<TaskModel> mappedTasks = switchTaskMapper.getMappedTasks(taskMapperContext);

// Then
assertEquals(1, mappedTasks.size());
assertEquals("switchTask", mappedTasks.get(0).getReferenceTaskName());
assertEquals(TaskModel.Status.FAILED, mappedTasks.get(0).getStatus());
}
}