LambdaTaskMapper implements TaskMapper { @Override public List<Task> getMappedTasks(TaskMapperContext taskMapperContext) { LOGGER.debug("TaskMapperContext {} in LambdaTaskMapper", taskMapperContext); WorkflowTask taskToSchedule = taskMapperContext.getTaskToSchedule(); Workflow workflowInstance = taskMapperContext.getWorkflowInstance(); String taskId = taskMapperContext.getTaskId(); TaskDef taskDefinition = Optional.ofNullable(taskMapperContext.getTaskDefinition()) .orElseGet(() -> Optional.ofNullable(metadataDAO.getTaskDef(taskToSchedule.getName())) .orElse(null)); Map<String, Object> taskInput = parametersUtils .getTaskInputV2(taskMapperContext.getTaskToSchedule().getInputParameters(), workflowInstance, taskId, taskDefinition); Task lambdaTask = new Task(); lambdaTask.setTaskType(Lambda.TASK_NAME); lambdaTask.setTaskDefName(taskMapperContext.getTaskToSchedule().getName()); lambdaTask.setReferenceTaskName(taskMapperContext.getTaskToSchedule().getTaskReferenceName()); lambdaTask.setWorkflowInstanceId(workflowInstance.getWorkflowId()); lambdaTask.setWorkflowType(workflowInstance.getWorkflowName()); lambdaTask.setCorrelationId(workflowInstance.getCorrelationId()); lambdaTask.setStartTime(System.currentTimeMillis()); lambdaTask.setScheduledTime(System.currentTimeMillis()); lambdaTask.setInputData(taskInput); lambdaTask.setTaskId(taskId); lambdaTask.setStatus(Task.Status.IN_PROGRESS); lambdaTask.setWorkflowTask(taskToSchedule); lambdaTask.setWorkflowPriority(workflowInstance.getPriority()); return Collections.singletonList(lambdaTask); } LambdaTaskMapper(ParametersUtils parametersUtils, MetadataDAO metadataDAO); @Override List<Task> getMappedTasks(TaskMapperContext taskMapperContext); static final Logger LOGGER; }
@Test public void getMappedTasks() throws Exception { WorkflowTask taskToSchedule = new WorkflowTask(); taskToSchedule.setName("lambda_task"); taskToSchedule.setType(TaskType.LAMBDA.name()); taskToSchedule.setTaskDefinition(new TaskDef("lambda_task")); taskToSchedule.setScriptExpression("if ($.input.a==1){return {testValue: true}} else{return {testValue: false} }"); String taskId = IDGenerator.generate(); WorkflowDef wd = new WorkflowDef(); Workflow w = new Workflow(); w.setWorkflowDefinition(wd); TaskMapperContext taskMapperContext = TaskMapperContext.newBuilder() .withWorkflowDefinition(wd) .withWorkflowInstance(w) .withTaskDefinition(new TaskDef()) .withTaskToSchedule(taskToSchedule) .withRetryCount(0) .withTaskId(taskId) .build(); List<Task> mappedTasks = new LambdaTaskMapper(parametersUtils, metadataDAO).getMappedTasks(taskMapperContext); assertEquals(1, mappedTasks.size()); assertNotNull(mappedTasks); assertEquals(TaskType.LAMBDA.name(), mappedTasks.get(0).getTaskType()); }
@Test public void getMappedTasks_WithoutTaskDef() throws Exception { WorkflowTask taskToSchedule = new WorkflowTask(); taskToSchedule.setType(TaskType.LAMBDA.name()); taskToSchedule.setScriptExpression("if ($.input.a==1){return {testValue: true}} else{return {testValue: false} }"); String taskId = IDGenerator.generate(); WorkflowDef wd = new WorkflowDef(); Workflow w = new Workflow(); w.setWorkflowDefinition(wd); TaskMapperContext taskMapperContext = TaskMapperContext.newBuilder() .withWorkflowDefinition(wd) .withWorkflowInstance(w) .withTaskDefinition(null) .withTaskToSchedule(taskToSchedule) .withRetryCount(0) .withTaskId(taskId) .build(); List<Task> mappedTasks = new LambdaTaskMapper(parametersUtils, metadataDAO).getMappedTasks(taskMapperContext); assertEquals(1, mappedTasks.size()); assertNotNull(mappedTasks); assertEquals(TaskType.LAMBDA.name(), mappedTasks.get(0).getTaskType()); }