use of com.netflix.conductor.common.metadata.tasks.TaskDef in project conductor by Netflix.
the class DecisionTaskMapperTest method getEvaluatedCaseValueUsingExpression.
@Test
public void getEvaluatedCaseValueUsingExpression() {
// Given
// Task Definition
TaskDef taskDef = new TaskDef();
Map<String, Object> inputMap = new HashMap<>();
inputMap.put("Id", "${workflow.input.Id}");
List<Map<String, Object>> taskDefinitionInput = new LinkedList<>();
taskDefinitionInput.add(inputMap);
// Decision task instance
WorkflowTask decisionTask = new WorkflowTask();
decisionTask.setType(TaskType.DECISION.name());
decisionTask.setName("Decision");
decisionTask.setTaskReferenceName("decisionTask");
decisionTask.setDefaultCase(Arrays.asList(task1));
decisionTask.setCaseValueParam("case");
decisionTask.getInputParameters().put("Id", "${workflow.input.Id}");
decisionTask.setCaseExpression("if ($.Id == null) 'bad input'; else if ( ($.Id != null && $.Id % 2 == 0)) 'even'; else 'odd'; ");
Map<String, List<WorkflowTask>> decisionCases = new HashMap<>();
decisionCases.put("even", Arrays.asList(task2));
decisionCases.put("odd", Arrays.asList(task3));
decisionTask.setDecisionCases(decisionCases);
// Workflow instance
WorkflowDef def = new WorkflowDef();
def.setSchemaVersion(2);
Workflow workflowInstance = new Workflow();
workflowInstance.setWorkflowDefinition(def);
Map<String, Object> workflowInput = new HashMap<>();
workflowInput.put("Id", "22");
workflowInstance.setInput(workflowInput);
Map<String, Object> body = new HashMap<>();
body.put("input", taskDefinitionInput);
taskDef.getInputTemplate().putAll(body);
Map<String, Object> evaluatorInput = parametersUtils.getTaskInput(decisionTask.getInputParameters(), workflowInstance, taskDef, null);
assertEquals("even", decisionTaskMapper.getEvaluatedCaseValue(decisionTask, evaluatorInput));
}
use of com.netflix.conductor.common.metadata.tasks.TaskDef in project conductor by Netflix.
the class DecisionTaskMapperTest method getEvaluatedCaseValueException.
@Test
public void getEvaluatedCaseValueException() {
// Given
// Task Definition
TaskDef taskDef = new TaskDef();
Map<String, Object> inputMap = new HashMap<>();
inputMap.put("Id", "${workflow.input.Id}");
List<Map<String, Object>> taskDefinitionInput = new LinkedList<>();
taskDefinitionInput.add(inputMap);
// Decision task instance
WorkflowTask decisionTask = new WorkflowTask();
decisionTask.setType(TaskType.DECISION.name());
decisionTask.setName("Decision");
decisionTask.setTaskReferenceName("decisionTask");
decisionTask.setDefaultCase(Collections.singletonList(task1));
decisionTask.setCaseValueParam("case");
decisionTask.getInputParameters().put("Id", "${workflow.input.Id}");
decisionTask.setCaseExpression("if ($Id == null) 'bad input'; else if ( ($Id != null && $Id % 2 == 0)) 'even'; else 'odd'; ");
Map<String, List<WorkflowTask>> decisionCases = new HashMap<>();
decisionCases.put("even", Collections.singletonList(task2));
decisionCases.put("odd", Collections.singletonList(task3));
decisionTask.setDecisionCases(decisionCases);
// Workflow instance
WorkflowDef def = new WorkflowDef();
def.setSchemaVersion(2);
Workflow workflowInstance = new Workflow();
workflowInstance.setWorkflowDefinition(def);
Map<String, Object> workflowInput = new HashMap<>();
workflowInput.put(".Id", "22");
workflowInstance.setInput(workflowInput);
Map<String, Object> body = new HashMap<>();
body.put("input", taskDefinitionInput);
taskDef.getInputTemplate().putAll(body);
Map<String, Object> evaluatorInput = parametersUtils.getTaskInput(decisionTask.getInputParameters(), workflowInstance, taskDef, null);
expectedException.expect(TerminateWorkflowException.class);
expectedException.expectMessage("Error while evaluating script: " + decisionTask.getCaseExpression());
decisionTaskMapper.getEvaluatedCaseValue(decisionTask, evaluatorInput);
}
use of com.netflix.conductor.common.metadata.tasks.TaskDef in project conductor by Netflix.
the class TestDeciderService method testTaskTimeout.
@Test
public void testTaskTimeout() {
Counter counter = registry.counter("task_timeout", "class", "WorkflowMonitor", "taskType", "test");
long counterCount = counter.count();
TaskDef taskType = new TaskDef();
taskType.setName("test");
taskType.setTimeoutPolicy(TimeoutPolicy.RETRY);
taskType.setTimeoutSeconds(1);
Task task = new Task();
task.setTaskType(taskType.getName());
// 2 seconds ago!
task.setStartTime(System.currentTimeMillis() - 2_000);
task.setStatus(Status.IN_PROGRESS);
deciderService.checkTaskTimeout(taskType, task);
// Task should be marked as timed out
assertEquals(Status.TIMED_OUT, task.getStatus());
assertNotNull(task.getReasonForIncompletion());
assertEquals(++counterCount, counter.count());
taskType.setTimeoutPolicy(TimeoutPolicy.ALERT_ONLY);
task.setStatus(Status.IN_PROGRESS);
task.setReasonForIncompletion(null);
deciderService.checkTaskTimeout(taskType, task);
// Nothing will happen
assertEquals(Status.IN_PROGRESS, task.getStatus());
assertNull(task.getReasonForIncompletion());
assertEquals(++counterCount, counter.count());
boolean exception = false;
taskType.setTimeoutPolicy(TimeoutPolicy.TIME_OUT_WF);
task.setStatus(Status.IN_PROGRESS);
task.setReasonForIncompletion(null);
try {
deciderService.checkTaskTimeout(taskType, task);
} catch (TerminateWorkflowException tw) {
exception = true;
}
assertTrue(exception);
assertEquals(Status.TIMED_OUT, task.getStatus());
assertNotNull(task.getReasonForIncompletion());
assertEquals(++counterCount, counter.count());
taskType.setTimeoutPolicy(TimeoutPolicy.TIME_OUT_WF);
task.setStatus(Status.IN_PROGRESS);
task.setReasonForIncompletion(null);
// this will be a no-op
deciderService.checkTaskTimeout(null, task);
assertEquals(Status.IN_PROGRESS, task.getStatus());
assertNull(task.getReasonForIncompletion());
assertEquals(counterCount, counter.count());
}
use of com.netflix.conductor.common.metadata.tasks.TaskDef in project conductor by Netflix.
the class TestDeciderService method testCustomRetryPolicy.
@Test
public void testCustomRetryPolicy() {
Workflow workflow = createDefaultWorkflow();
Task task = new Task();
task.setStatus(Status.FAILED);
task.setTaskId("t1");
task.setStartDelayInSeconds(30);
TaskDef taskDef = new TaskDef();
taskDef.setRetryDelaySeconds(60);
taskDef.setRetryLogic(TaskDef.RetryLogic.CUSTOM);
WorkflowTask workflowTask = new WorkflowTask();
// Retry delay from the task
Optional<Task> task2 = deciderService.retry(taskDef, workflowTask, task, workflow);
assertEquals(30, task2.get().getCallbackAfterSeconds());
// No retry delay
task2.get().setStartDelayInSeconds(-1);
Optional<Task> task3 = deciderService.retry(taskDef, workflowTask, task2.get(), workflow);
assertEquals(0, task3.get().getCallbackAfterSeconds());
// Retry delay from task definition
task3.get().setStartDelayInSeconds(0);
Optional<Task> task4 = deciderService.retry(taskDef, workflowTask, task3.get(), workflow);
assertEquals(60, task4.get().getCallbackAfterSeconds());
}
use of com.netflix.conductor.common.metadata.tasks.TaskDef in project conductor by Netflix.
the class TestDeciderService method testGetTasksToBeScheduled.
@Test
public void testGetTasksToBeScheduled() {
WorkflowDef workflowDef = createLinearWorkflow();
Workflow workflow = new Workflow();
workflow.setWorkflowDefinition(workflowDef);
workflow.setStatus(WorkflowStatus.RUNNING);
WorkflowTask workflowTask1 = new WorkflowTask();
workflowTask1.setName("s1");
workflowTask1.setTaskReferenceName("s1");
workflowTask1.setType(TaskType.SIMPLE.name());
workflowTask1.setTaskDefinition(new TaskDef("s1"));
List<Task> tasksToBeScheduled = deciderService.getTasksToBeScheduled(workflow, workflowTask1, 0, null);
assertNotNull(tasksToBeScheduled);
assertEquals(1, tasksToBeScheduled.size());
assertEquals("s1", tasksToBeScheduled.get(0).getReferenceTaskName());
WorkflowTask workflowTask2 = new WorkflowTask();
workflowTask2.setName("s2");
workflowTask2.setTaskReferenceName("s2");
workflowTask2.setType(TaskType.SIMPLE.name());
workflowTask2.setTaskDefinition(new TaskDef("s2"));
tasksToBeScheduled = deciderService.getTasksToBeScheduled(workflow, workflowTask2, 0, null);
assertNotNull(tasksToBeScheduled);
assertEquals(1, tasksToBeScheduled.size());
assertEquals("s2", tasksToBeScheduled.get(0).getReferenceTaskName());
}
Aggregations