use of com.netflix.conductor.common.metadata.tasks.TaskDef in project conductor by Netflix.
the class UserDefinedTaskMapper method getMappedTasks.
/**
* This method maps a {@link WorkflowTask} of type {@link TaskType#USER_DEFINED}
* to a {@link Task} in a {@link Task.Status#SCHEDULED} state
*
* @param taskMapperContext: A wrapper class containing the {@link WorkflowTask}, {@link WorkflowDef}, {@link Workflow} and a string representation of the TaskId
* @return a List with just one User defined task
* @throws TerminateWorkflowException In case if the task definition does not exist
*/
@Override
public List<Task> getMappedTasks(TaskMapperContext taskMapperContext) throws TerminateWorkflowException {
logger.debug("TaskMapperContext {} in UserDefinedTaskMapper", taskMapperContext);
WorkflowTask taskToSchedule = taskMapperContext.getTaskToSchedule();
Workflow workflowInstance = taskMapperContext.getWorkflowInstance();
String taskId = taskMapperContext.getTaskId();
int retryCount = taskMapperContext.getRetryCount();
TaskDef taskDefinition = Optional.ofNullable(taskMapperContext.getTaskDefinition()).orElseGet(() -> Optional.ofNullable(metadataDAO.getTaskDef(taskToSchedule.getName())).orElseThrow(() -> {
String reason = String.format("Invalid task specified. Cannot find task by name %s in the task definitions", taskToSchedule.getName());
return new TerminateWorkflowException(reason);
}));
Map<String, Object> input = parametersUtils.getTaskInputV2(taskToSchedule.getInputParameters(), workflowInstance, taskId, taskDefinition);
Task userDefinedTask = new Task();
userDefinedTask.setTaskType(taskToSchedule.getType());
userDefinedTask.setTaskDefName(taskToSchedule.getName());
userDefinedTask.setReferenceTaskName(taskToSchedule.getTaskReferenceName());
userDefinedTask.setWorkflowInstanceId(workflowInstance.getWorkflowId());
userDefinedTask.setWorkflowType(workflowInstance.getWorkflowName());
userDefinedTask.setCorrelationId(workflowInstance.getCorrelationId());
userDefinedTask.setScheduledTime(System.currentTimeMillis());
userDefinedTask.setTaskId(taskId);
userDefinedTask.setInputData(input);
userDefinedTask.setStatus(Task.Status.SCHEDULED);
userDefinedTask.setRetryCount(retryCount);
userDefinedTask.setCallbackAfterSeconds(taskToSchedule.getStartDelay());
userDefinedTask.setWorkflowTask(taskToSchedule);
userDefinedTask.setWorkflowPriority(workflowInstance.getPriority());
userDefinedTask.setRateLimitPerFrequency(taskDefinition.getRateLimitPerFrequency());
userDefinedTask.setRateLimitFrequencyInSeconds(taskDefinition.getRateLimitFrequencyInSeconds());
return Collections.singletonList(userDefinedTask);
}
use of com.netflix.conductor.common.metadata.tasks.TaskDef in project conductor by Netflix.
the class TestDeciderOutcomes method init.
@Before
public void init() {
MetadataDAO metadataDAO = mock(MetadataDAO.class);
ExternalPayloadStorageUtils externalPayloadStorageUtils = mock(ExternalPayloadStorageUtils.class);
Configuration configuration = mock(Configuration.class);
when(configuration.getTaskInputPayloadSizeThresholdKB()).thenReturn(10L);
when(configuration.getMaxTaskInputPayloadSizeThresholdKB()).thenReturn(10240L);
TaskDef taskDef = new TaskDef();
taskDef.setRetryCount(1);
taskDef.setName("mockTaskDef");
taskDef.setResponseTimeoutSeconds(60 * 60);
when(metadataDAO.getTaskDef(anyString())).thenReturn(taskDef);
ParametersUtils parametersUtils = new ParametersUtils();
Map<String, TaskMapper> taskMappers = new HashMap<>();
taskMappers.put("DECISION", new DecisionTaskMapper());
taskMappers.put("DYNAMIC", new DynamicTaskMapper(parametersUtils, metadataDAO));
taskMappers.put("FORK_JOIN", new ForkJoinTaskMapper());
taskMappers.put("JOIN", new JoinTaskMapper());
taskMappers.put("FORK_JOIN_DYNAMIC", new ForkJoinDynamicTaskMapper(parametersUtils, objectMapper, metadataDAO));
taskMappers.put("USER_DEFINED", new UserDefinedTaskMapper(parametersUtils, metadataDAO));
taskMappers.put("SIMPLE", new SimpleTaskMapper(parametersUtils));
taskMappers.put("SUB_WORKFLOW", new SubWorkflowTaskMapper(parametersUtils, metadataDAO));
taskMappers.put("EVENT", new EventTaskMapper(parametersUtils));
taskMappers.put("WAIT", new WaitTaskMapper(parametersUtils));
taskMappers.put("HTTP", new HTTPTaskMapper(parametersUtils, metadataDAO));
this.deciderService = new DeciderService(parametersUtils, metadataDAO, externalPayloadStorageUtils, taskMappers, configuration);
}
use of com.netflix.conductor.common.metadata.tasks.TaskDef in project conductor by Netflix.
the class TestDeciderOutcomes method testDecisionCases.
@Test
public void testDecisionCases() {
WorkflowDef def = new WorkflowDef();
def.setName("test");
WorkflowTask even = new WorkflowTask();
even.setName("even");
even.setType("SIMPLE");
even.setTaskReferenceName("even");
even.setTaskDefinition(new TaskDef("even"));
WorkflowTask odd = new WorkflowTask();
odd.setName("odd");
odd.setType("SIMPLE");
odd.setTaskReferenceName("odd");
odd.setTaskDefinition(new TaskDef("odd"));
WorkflowTask defaultt = new WorkflowTask();
defaultt.setName("defaultt");
defaultt.setType("SIMPLE");
defaultt.setTaskReferenceName("defaultt");
defaultt.setTaskDefinition(new TaskDef("defaultt"));
WorkflowTask decide = new WorkflowTask();
decide.setName("decide");
decide.setWorkflowTaskType(TaskType.DECISION);
decide.setTaskReferenceName("d0");
decide.getInputParameters().put("Id", "${workflow.input.Id}");
decide.getInputParameters().put("location", "${workflow.input.location}");
decide.setCaseExpression("if ($.Id == null) 'bad input'; else if ( ($.Id != null && $.Id % 2 == 0) || $.location == 'usa') 'even'; else 'odd'; ");
decide.getDecisionCases().put("even", Arrays.asList(even));
decide.getDecisionCases().put("odd", Arrays.asList(odd));
decide.setDefaultCase(Arrays.asList(defaultt));
def.getTasks().add(decide);
def.setSchemaVersion(2);
Workflow workflow = new Workflow();
workflow.setWorkflowDefinition(def);
workflow.setStartTime(System.currentTimeMillis());
DeciderOutcome outcome = deciderService.decide(workflow);
assertNotNull(outcome);
System.out.println("Schedule after starting: " + outcome.tasksToBeScheduled);
assertEquals(2, outcome.tasksToBeScheduled.size());
assertEquals(decide.getTaskReferenceName(), outcome.tasksToBeScheduled.get(0).getReferenceTaskName());
// default
assertEquals(defaultt.getTaskReferenceName(), outcome.tasksToBeScheduled.get(1).getReferenceTaskName());
System.out.println(outcome.tasksToBeScheduled.get(0).getOutputData().get("caseOutput"));
assertEquals(Arrays.asList("bad input"), outcome.tasksToBeScheduled.get(0).getOutputData().get("caseOutput"));
workflow.getInput().put("Id", 9);
workflow.getInput().put("location", "usa");
outcome = deciderService.decide(workflow);
assertEquals(2, outcome.tasksToBeScheduled.size());
assertEquals(decide.getTaskReferenceName(), outcome.tasksToBeScheduled.get(0).getReferenceTaskName());
// even because of location == usa
assertEquals(even.getTaskReferenceName(), outcome.tasksToBeScheduled.get(1).getReferenceTaskName());
assertEquals(Arrays.asList("even"), outcome.tasksToBeScheduled.get(0).getOutputData().get("caseOutput"));
workflow.getInput().put("Id", 9);
workflow.getInput().put("location", "canada");
outcome = deciderService.decide(workflow);
assertEquals(2, outcome.tasksToBeScheduled.size());
assertEquals(decide.getTaskReferenceName(), outcome.tasksToBeScheduled.get(0).getReferenceTaskName());
// odd
assertEquals(odd.getTaskReferenceName(), outcome.tasksToBeScheduled.get(1).getReferenceTaskName());
assertEquals(Arrays.asList("odd"), outcome.tasksToBeScheduled.get(0).getOutputData().get("caseOutput"));
}
use of com.netflix.conductor.common.metadata.tasks.TaskDef in project conductor by Netflix.
the class TestDeciderOutcomes method testRetries.
@Test
public void testRetries() {
WorkflowDef def = new WorkflowDef();
def.setName("test");
WorkflowTask workflowTask = new WorkflowTask();
workflowTask.setName("test_task");
workflowTask.setType("USER_TASK");
workflowTask.setTaskReferenceName("t0");
workflowTask.getInputParameters().put("taskId", "${CPEWF_TASK_ID}");
workflowTask.getInputParameters().put("requestId", "${workflow.input.requestId}");
workflowTask.setTaskDefinition(new TaskDef("test_task"));
def.getTasks().add(workflowTask);
def.setSchemaVersion(2);
Workflow workflow = new Workflow();
workflow.setWorkflowDefinition(def);
workflow.getInput().put("requestId", 123);
workflow.setStartTime(System.currentTimeMillis());
DeciderOutcome outcome = deciderService.decide(workflow);
assertNotNull(outcome);
assertEquals(1, outcome.tasksToBeScheduled.size());
assertEquals(workflowTask.getTaskReferenceName(), outcome.tasksToBeScheduled.get(0).getReferenceTaskName());
String task1Id = outcome.tasksToBeScheduled.get(0).getTaskId();
assertEquals(task1Id, outcome.tasksToBeScheduled.get(0).getInputData().get("taskId"));
assertEquals(123, outcome.tasksToBeScheduled.get(0).getInputData().get("requestId"));
outcome.tasksToBeScheduled.get(0).setStatus(Status.FAILED);
workflow.getTasks().addAll(outcome.tasksToBeScheduled);
outcome = deciderService.decide(workflow);
assertNotNull(outcome);
assertEquals(1, outcome.tasksToBeUpdated.size());
assertEquals(1, outcome.tasksToBeScheduled.size());
assertEquals(task1Id, outcome.tasksToBeUpdated.get(0).getTaskId());
assertNotSame(task1Id, outcome.tasksToBeScheduled.get(0).getTaskId());
assertEquals(outcome.tasksToBeScheduled.get(0).getTaskId(), outcome.tasksToBeScheduled.get(0).getInputData().get("taskId"));
assertEquals(task1Id, outcome.tasksToBeScheduled.get(0).getRetriedTaskId());
assertEquals(123, outcome.tasksToBeScheduled.get(0).getInputData().get("requestId"));
WorkflowTask fork = new WorkflowTask();
fork.setName("fork0");
fork.setWorkflowTaskType(TaskType.FORK_JOIN_DYNAMIC);
fork.setTaskReferenceName("fork0");
fork.setDynamicForkTasksInputParamName("forkedInputs");
fork.setDynamicForkTasksParam("forks");
fork.getInputParameters().put("forks", "${workflow.input.forks}");
fork.getInputParameters().put("forkedInputs", "${workflow.input.forkedInputs}");
WorkflowTask join = new WorkflowTask();
join.setName("join0");
join.setType("JOIN");
join.setTaskReferenceName("join0");
def.getTasks().clear();
def.getTasks().add(fork);
def.getTasks().add(join);
List<WorkflowTask> forks = new LinkedList<>();
Map<String, Map<String, Object>> forkedInputs = new HashMap<>();
for (int i = 0; i < 1; i++) {
WorkflowTask wft = new WorkflowTask();
wft.setName("f" + i);
wft.setTaskReferenceName("f" + i);
wft.setWorkflowTaskType(TaskType.SIMPLE);
wft.getInputParameters().put("requestId", "${workflow.input.requestId}");
wft.getInputParameters().put("taskId", "${CPEWF_TASK_ID}");
wft.setTaskDefinition(new TaskDef("f" + i));
forks.add(wft);
Map<String, Object> input = new HashMap<>();
input.put("k", "v");
input.put("k1", 1);
forkedInputs.put(wft.getTaskReferenceName(), input);
}
workflow = new Workflow();
workflow.setWorkflowDefinition(def);
workflow.getInput().put("requestId", 123);
workflow.setStartTime(System.currentTimeMillis());
workflow.getInput().put("forks", forks);
workflow.getInput().put("forkedInputs", forkedInputs);
outcome = deciderService.decide(workflow);
assertNotNull(outcome);
assertEquals(3, outcome.tasksToBeScheduled.size());
assertEquals(0, outcome.tasksToBeUpdated.size());
assertEquals("v", outcome.tasksToBeScheduled.get(1).getInputData().get("k"));
assertEquals(1, outcome.tasksToBeScheduled.get(1).getInputData().get("k1"));
assertEquals(outcome.tasksToBeScheduled.get(1).getTaskId(), outcome.tasksToBeScheduled.get(1).getInputData().get("taskId"));
System.out.println(outcome.tasksToBeScheduled.get(1).getInputData());
task1Id = outcome.tasksToBeScheduled.get(1).getTaskId();
outcome.tasksToBeScheduled.get(1).setStatus(Status.FAILED);
for (Task taskToBeScheduled : outcome.tasksToBeScheduled) {
taskToBeScheduled.setUpdateTime(System.currentTimeMillis());
}
workflow.getTasks().addAll(outcome.tasksToBeScheduled);
outcome = deciderService.decide(workflow);
assertTrue(outcome.tasksToBeScheduled.stream().anyMatch(task1 -> task1.getReferenceTaskName().equals("f0")));
// noinspection ConstantConditions
Task task1 = outcome.tasksToBeScheduled.stream().filter(t -> t.getReferenceTaskName().equals("f0")).findFirst().get();
assertEquals("v", task1.getInputData().get("k"));
assertEquals(1, task1.getInputData().get("k1"));
assertEquals(task1.getTaskId(), task1.getInputData().get("taskId"));
assertNotSame(task1Id, task1.getTaskId());
assertEquals(task1Id, task1.getRetriedTaskId());
System.out.println(task1.getInputData());
}
use of com.netflix.conductor.common.metadata.tasks.TaskDef in project conductor by Netflix.
the class DecisionTaskMapperTest method getMappedTasks.
@Test
public void getMappedTasks() {
// 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 workflowDef = new WorkflowDef();
workflowDef.setSchemaVersion(2);
Workflow workflowInstance = new Workflow();
workflowInstance.setWorkflowDefinition(workflowDef);
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> input = parametersUtils.getTaskInput(decisionTask.getInputParameters(), workflowInstance, null, null);
Task theTask = new Task();
theTask.setReferenceTaskName("Foo");
theTask.setTaskId(IDGenerator.generate());
when(deciderService.getTasksToBeScheduled(workflowInstance, task2, 0, null)).thenReturn(Arrays.asList(theTask));
TaskMapperContext taskMapperContext = TaskMapperContext.newBuilder().withWorkflowDefinition(workflowDef).withWorkflowInstance(workflowInstance).withTaskToSchedule(decisionTask).withTaskInput(input).withRetryCount(0).withTaskId(IDGenerator.generate()).withDeciderService(deciderService).build();
// When
List<Task> mappedTasks = decisionTaskMapper.getMappedTasks(taskMapperContext);
// Then
assertEquals(2, mappedTasks.size());
assertEquals("decisionTask", mappedTasks.get(0).getReferenceTaskName());
assertEquals("Foo", mappedTasks.get(1).getReferenceTaskName());
}
Aggregations