Search in sources :

Example 86 with TaskDef

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));
}
Also used : WorkflowDef(com.netflix.conductor.common.metadata.workflow.WorkflowDef) HashMap(java.util.HashMap) Workflow(com.netflix.conductor.common.run.Workflow) WorkflowTask(com.netflix.conductor.common.metadata.workflow.WorkflowTask) LinkedList(java.util.LinkedList) TaskDef(com.netflix.conductor.common.metadata.tasks.TaskDef) List(java.util.List) LinkedList(java.util.LinkedList) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 87 with TaskDef

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);
}
Also used : WorkflowDef(com.netflix.conductor.common.metadata.workflow.WorkflowDef) HashMap(java.util.HashMap) Workflow(com.netflix.conductor.common.run.Workflow) WorkflowTask(com.netflix.conductor.common.metadata.workflow.WorkflowTask) LinkedList(java.util.LinkedList) TaskDef(com.netflix.conductor.common.metadata.tasks.TaskDef) List(java.util.List) LinkedList(java.util.LinkedList) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 88 with TaskDef

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());
}
Also used : Task(com.netflix.conductor.common.metadata.tasks.Task) WorkflowTask(com.netflix.conductor.common.metadata.workflow.WorkflowTask) Counter(com.netflix.spectator.api.Counter) TaskDef(com.netflix.conductor.common.metadata.tasks.TaskDef) Test(org.junit.Test)

Example 89 with TaskDef

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());
}
Also used : Task(com.netflix.conductor.common.metadata.tasks.Task) WorkflowTask(com.netflix.conductor.common.metadata.workflow.WorkflowTask) TaskDef(com.netflix.conductor.common.metadata.tasks.TaskDef) Workflow(com.netflix.conductor.common.run.Workflow) WorkflowTask(com.netflix.conductor.common.metadata.workflow.WorkflowTask) Test(org.junit.Test)

Example 90 with TaskDef

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());
}
Also used : Task(com.netflix.conductor.common.metadata.tasks.Task) WorkflowTask(com.netflix.conductor.common.metadata.workflow.WorkflowTask) WorkflowDef(com.netflix.conductor.common.metadata.workflow.WorkflowDef) TaskDef(com.netflix.conductor.common.metadata.tasks.TaskDef) Workflow(com.netflix.conductor.common.run.Workflow) WorkflowTask(com.netflix.conductor.common.metadata.workflow.WorkflowTask) Test(org.junit.Test)

Aggregations

TaskDef (com.netflix.conductor.common.metadata.tasks.TaskDef)172 Test (org.junit.Test)128 WorkflowTask (com.netflix.conductor.common.metadata.workflow.WorkflowTask)121 Task (com.netflix.conductor.common.metadata.tasks.Task)77 Workflow (com.netflix.conductor.common.run.Workflow)76 WorkflowDef (com.netflix.conductor.common.metadata.workflow.WorkflowDef)73 HashMap (java.util.HashMap)56 ArrayList (java.util.ArrayList)32 ConstraintViolation (javax.validation.ConstraintViolation)31 SubWorkflow (com.netflix.conductor.core.execution.tasks.SubWorkflow)30 UserTask (com.netflix.conductor.tests.utils.UserTask)28 LinkedList (java.util.LinkedList)28 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)27 List (java.util.List)22 Map (java.util.Map)19 ApplicationException (com.netflix.conductor.core.execution.ApplicationException)18 Before (org.junit.Before)14 ExpectedException (org.junit.rules.ExpectedException)13 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)12 Collectors (java.util.stream.Collectors)11