Search in sources :

Example 66 with Task

use of com.netflix.conductor.common.metadata.tasks.Task in project conductor by Netflix.

the class TestDeciderService method testCheckForWorkflowCompletion.

@Test
public void testCheckForWorkflowCompletion() {
    WorkflowDef conditionalWorkflowDef = createConditionalWF();
    WorkflowTask terminateWT = new WorkflowTask();
    terminateWT.setType(TaskType.TERMINATE.name());
    terminateWT.setTaskReferenceName("terminate");
    terminateWT.setName("terminate");
    terminateWT.getInputParameters().put("terminationStatus", "COMPLETED");
    conditionalWorkflowDef.getTasks().add(terminateWT);
    // when workflow has no tasks
    Workflow workflow = new Workflow();
    workflow.setWorkflowDefinition(conditionalWorkflowDef);
    // then workflow completion check returns false
    assertFalse(deciderService.checkForWorkflowCompletion(workflow));
    // when only part of the tasks are completed
    Task decTask = new Task();
    decTask.setTaskType(TaskType.DECISION.name());
    decTask.setReferenceTaskName("conditional2");
    decTask.setStatus(Status.COMPLETED);
    Task task1 = new Task();
    decTask.setTaskType(TaskType.SIMPLE.name());
    task1.setReferenceTaskName("t1");
    task1.setStatus(Status.COMPLETED);
    workflow.getTasks().addAll(Arrays.asList(decTask, task1));
    // then workflow completion check returns false
    assertFalse(deciderService.checkForWorkflowCompletion(workflow));
    // when the terminate task is COMPLETED
    Task task2 = new Task();
    decTask.setTaskType(TaskType.SIMPLE.name());
    task2.setReferenceTaskName("t2");
    task2.setStatus(Status.SCHEDULED);
    Task terminateTask = new Task();
    decTask.setTaskType(TaskType.TERMINATE.name());
    terminateTask.setReferenceTaskName("terminate");
    terminateTask.setStatus(Status.COMPLETED);
    workflow.getTasks().addAll(Arrays.asList(task2, terminateTask));
    // then the workflow completion check returns true
    assertTrue(deciderService.checkForWorkflowCompletion(workflow));
}
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) Workflow(com.netflix.conductor.common.run.Workflow) WorkflowTask(com.netflix.conductor.common.metadata.workflow.WorkflowTask) Test(org.junit.Test)

Example 67 with Task

use of com.netflix.conductor.common.metadata.tasks.Task in project conductor by Netflix.

the class TestDeciderService method testCustomRetryWithWorkflowTask.

@Test
public void testCustomRetryWithWorkflowTask() {
    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();
    workflowTask.setRetryLogic(RetryLogic.FIXED);
    workflowTask.setStartDelay(80);
    // Retry delay from the task as tasDef retry policy is CUSTOM,
    // but workflow task would be preferred which is FIXED (retryDelay will come from workflowTask)
    Optional<Task> task2 = deciderService.retry(taskDef, workflowTask, task, workflow);
    assertEquals(80, task2.get().getStartDelayInSeconds());
    // Retry delay from the task as tasDef retry policy is CUSTOM,
    // but workflow task would be preferred which is CUSTOM (retryDelay will be 0 since task.retryDelay = -1)
    task2.get().setStartDelayInSeconds(-1);
    workflowTask.setRetryLogic(RetryLogic.CUSTOM);
    workflowTask.setStartDelay(90);
    // Custom retry policy from Workflow,
    Optional<Task> task3 = deciderService.retry(taskDef, workflowTask, task2.get(), workflow);
    assertEquals(0, task3.get().getStartDelayInSeconds());
    // Retry delay from the task as tasDef retry policy is CUSTOM,
    // workflow task would be preferred which is CUSTOM (retryDelay will come from the workflowTask,
    // since task.retryDelay = 0)
    task3.get().setStartDelayInSeconds(0);
    Optional<Task> task4 = deciderService.retry(taskDef, workflowTask, task3.get(), workflow);
    assertEquals(90, 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 68 with Task

use of com.netflix.conductor.common.metadata.tasks.Task in project conductor by Netflix.

the class TestDeciderService method createDefaultWorkflow.

private Workflow createDefaultWorkflow() {
    WorkflowDef workflowDef = new WorkflowDef();
    workflowDef.setName("TestDeciderService");
    workflowDef.setVersion(1);
    Workflow workflow = new Workflow();
    workflow.setWorkflowDefinition(workflowDef);
    workflow.getInput().put("requestId", "request id 001");
    workflow.getInput().put("hasAwards", true);
    workflow.getInput().put("channelMapping", 5);
    Map<String, Object> name = new HashMap<>();
    name.put("name", "The Who");
    name.put("year", 1970);
    Map<String, Object> name2 = new HashMap<>();
    name2.put("name", "The Doors");
    name2.put("year", 1975);
    List<Object> names = new LinkedList<>();
    names.add(name);
    names.add(name2);
    workflow.getOutput().put("name", name);
    workflow.getOutput().put("names", names);
    workflow.getOutput().put("awards", 200);
    Task task = new Task();
    task.setReferenceTaskName("task2");
    task.getOutputData().put("location", "http://location");
    task.setStatus(Status.COMPLETED);
    Task task2 = new Task();
    task2.setReferenceTaskName("task3");
    task2.getOutputData().put("refId", "abcddef_1234_7890_aaffcc");
    task2.setStatus(Status.SCHEDULED);
    workflow.getTasks().add(task);
    workflow.getTasks().add(task2);
    return workflow;
}
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) HashMap(java.util.HashMap) Workflow(com.netflix.conductor.common.run.Workflow) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) LinkedList(java.util.LinkedList)

Example 69 with Task

use of com.netflix.conductor.common.metadata.tasks.Task in project conductor by Netflix.

the class TaskServiceImpl method ackTaskReceived.

/**
 * Ack Task is received.
 *
 * @param taskId Id of the task
 * @return `true|false` if task if received or not
 */
@Service
public boolean ackTaskReceived(String taskId) {
    LOGGER.debug("Ack received for task: {}", taskId);
    String ackTaskDesc = "Ack Task with taskId: " + taskId;
    String ackTaskOperation = "ackTaskReceived";
    AtomicBoolean ackResult = new AtomicBoolean(false);
    try {
        new RetryUtil<>().retryOnException(() -> {
            ackResult.set(executionService.ackTaskReceived(taskId));
            return null;
        }, null, null, 3, ackTaskDesc, ackTaskOperation);
    } catch (Exception e) {
        // Fail the task and let decide reevaluate the workflow, thereby preventing workflow being stuck from transient ack errors.
        String errorMsg = String.format("Error when trying to ack task %s", taskId);
        LOGGER.error(errorMsg, e);
        Task task = executionService.getTask(taskId);
        Monitors.recordAckTaskError(task.getTaskType());
        failTask(task, errorMsg);
        ackResult.set(false);
    }
    return ackResult.get();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Task(com.netflix.conductor.common.metadata.tasks.Task) Service(com.netflix.conductor.annotations.Service)

Example 70 with Task

use of com.netflix.conductor.common.metadata.tasks.Task in project conductor by Netflix.

the class TestDeciderService method testCheckTaskPollTimeout.

@Test
public void testCheckTaskPollTimeout() {
    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.setPollTimeoutSeconds(1);
    Task task = new Task();
    task.setTaskType(taskType.getName());
    task.setScheduledTime(System.currentTimeMillis() - 2_000);
    task.setStatus(Status.SCHEDULED);
    deciderService.checkTaskPollTimeout(taskType, task);
    assertEquals(++counterCount, counter.count());
    assertEquals(Status.TIMED_OUT, task.getStatus());
    assertNotNull(task.getReasonForIncompletion());
    task.setScheduledTime(System.currentTimeMillis());
    task.setReasonForIncompletion(null);
    task.setStatus(Status.SCHEDULED);
    deciderService.checkTaskPollTimeout(taskType, task);
    assertEquals(counterCount, counter.count());
    assertEquals(Status.SCHEDULED, task.getStatus());
    assertNull(task.getReasonForIncompletion());
}
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)

Aggregations

Task (com.netflix.conductor.common.metadata.tasks.Task)357 Workflow (com.netflix.conductor.common.run.Workflow)249 Test (org.junit.Test)248 WorkflowTask (com.netflix.conductor.common.metadata.workflow.WorkflowTask)227 HashMap (java.util.HashMap)147 WorkflowDef (com.netflix.conductor.common.metadata.workflow.WorkflowDef)121 SubWorkflow (com.netflix.conductor.core.execution.tasks.SubWorkflow)110 TaskDef (com.netflix.conductor.common.metadata.tasks.TaskDef)95 UserTask (com.netflix.conductor.tests.utils.UserTask)73 Map (java.util.Map)53 LinkedList (java.util.LinkedList)51 WorkflowSystemTask (com.netflix.conductor.core.execution.tasks.WorkflowSystemTask)45 List (java.util.List)45 ApplicationException (com.netflix.conductor.core.execution.ApplicationException)41 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)39 TaskResult (com.netflix.conductor.common.metadata.tasks.TaskResult)38 Status (com.netflix.conductor.common.metadata.tasks.Task.Status)32 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)29 Collectors (java.util.stream.Collectors)29 TaskType (com.netflix.conductor.common.metadata.workflow.TaskType)28