This repository has been archived by the owner on Dec 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1032 from X-Ultra/dev_script_task
Add new system task type Lambda
- Loading branch information
Showing
10 changed files
with
403 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
core/src/main/java/com/netflix/conductor/core/execution/mapper/LambdaTaskMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/** | ||
* Copyright 2018 Netflix, Inc. | ||
* <p> | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* <p> | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.netflix.conductor.core.execution.mapper; | ||
|
||
import com.netflix.conductor.common.metadata.tasks.Task; | ||
import com.netflix.conductor.common.metadata.workflow.TaskType; | ||
import com.netflix.conductor.common.metadata.workflow.WorkflowDef; | ||
import com.netflix.conductor.common.metadata.workflow.WorkflowTask; | ||
import com.netflix.conductor.common.run.Workflow; | ||
import com.netflix.conductor.core.execution.ParametersUtils; | ||
import com.netflix.conductor.core.execution.SystemTaskType; | ||
import com.netflix.conductor.core.execution.tasks.Lambda; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.Arrays; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* @author x-ultra | ||
*/ | ||
public class LambdaTaskMapper implements TaskMapper { | ||
|
||
public static final Logger logger = LoggerFactory.getLogger(LambdaTaskMapper.class); | ||
private ParametersUtils parametersUtils; | ||
|
||
public LambdaTaskMapper(ParametersUtils parametersUtils) { | ||
this.parametersUtils = parametersUtils; | ||
} | ||
|
||
|
||
@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(); | ||
|
||
Map<String, Object> taskInput = parametersUtils.getTaskInputV2(taskMapperContext.getTaskToSchedule().getInputParameters(), workflowInstance, taskId, null); | ||
|
||
Task task = new Task(); | ||
task.setTaskType(Lambda.TASK_NAME); | ||
task.setTaskDefName(taskMapperContext.getTaskToSchedule().getName()); | ||
task.setReferenceTaskName(taskMapperContext.getTaskToSchedule().getTaskReferenceName()); | ||
task.setWorkflowInstanceId(workflowInstance.getWorkflowId()); | ||
task.setWorkflowType(workflowInstance.getWorkflowName()); | ||
task.setCorrelationId(workflowInstance.getCorrelationId()); | ||
task.setScheduledTime(System.currentTimeMillis()); | ||
task.setInputData(taskInput); | ||
task.setTaskId(taskId); | ||
task.setStatus(Task.Status.IN_PROGRESS); | ||
task.setWorkflowTask(taskToSchedule); | ||
return Arrays.asList(task); | ||
} | ||
} |
90 changes: 90 additions & 0 deletions
90
core/src/main/java/com/netflix/conductor/core/execution/tasks/Lambda.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package com.netflix.conductor.core.execution.tasks; | ||
|
||
import com.netflix.conductor.common.metadata.tasks.Task; | ||
import com.netflix.conductor.common.run.Workflow; | ||
import com.netflix.conductor.core.events.ScriptEvaluator; | ||
import com.netflix.conductor.core.execution.WorkflowExecutor; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import javax.inject.Singleton; | ||
import javax.script.ScriptException; | ||
import java.util.Map; | ||
|
||
|
||
/** | ||
* @author X-Ultra | ||
* Task that enables execute Lambda script at workflow execution, For example, | ||
* <pre> | ||
... | ||
{ | ||
"tasks": [ | ||
{ | ||
"name": "LAMBDA", | ||
"taskReferenceName": "lambda_test", | ||
"type": "LAMBDA", | ||
"inputParameters": { | ||
"input": "${workflow.input}", | ||
"scriptExpression": "if ($.input.a==1){return {testvalue: true}} else{return {testvalue: false} }" | ||
} | ||
} | ||
] | ||
} | ||
... | ||
* </pre> | ||
* then to use task output, e.g. <code>script_test.output.testvalue</code> | ||
*/ | ||
public class Lambda extends WorkflowSystemTask { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(Lambda.class); | ||
private static final String QUERY_EXPRESSION_PARAMETER = "scriptExpression"; | ||
public static final String TASK_NAME = "LAMBDA"; | ||
|
||
public Lambda() { | ||
super(TASK_NAME); | ||
logger.info(TASK_NAME + " task initialized..."); | ||
} | ||
|
||
@Override | ||
public void start(Workflow workflow, Task task, WorkflowExecutor executor) { | ||
|
||
|
||
} | ||
|
||
|
||
@Override | ||
public boolean execute(Workflow workflow, Task task, WorkflowExecutor executor) { | ||
Map<String, Object> taskInput = task.getInputData(); | ||
Map<String, Object> taskOutput = task.getOutputData(); | ||
|
||
String scriptExpression = (String) taskInput.get(QUERY_EXPRESSION_PARAMETER); | ||
|
||
if(scriptExpression == null) { | ||
task.setReasonForIncompletion("Missing '" + QUERY_EXPRESSION_PARAMETER + "' in input parameters"); | ||
task.setStatus(Task.Status.FAILED); | ||
return true; | ||
} | ||
|
||
String scriptExpressionBuilder = "function scriptFun(){" + | ||
scriptExpression + | ||
"} scriptFun();"; | ||
|
||
logger.info("scriptExpressionBuilder: {}" , scriptExpressionBuilder); | ||
|
||
try { | ||
Object returnValue = null; | ||
returnValue = ScriptEvaluator.eval(scriptExpressionBuilder, taskInput); | ||
taskOutput.put("result", returnValue); | ||
task.setStatus(Task.Status.COMPLETED); | ||
} catch (ScriptException e) { | ||
logger.error("Failed to execute Lambda: {}", scriptExpression, e); | ||
task.setStatus(Task.Status.FAILED); | ||
task.setReasonForIncompletion(e.getMessage()); | ||
taskOutput.put("error", e.getCause() != null ? e.getCause().getMessage() : e.getMessage()); | ||
} | ||
|
||
return true; | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
core/src/test/java/com/netflix/conductor/core/execution/mapper/LambdaTaskMapperTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package com.netflix.conductor.core.execution.mapper; | ||
|
||
import com.netflix.conductor.common.metadata.tasks.Task; | ||
import com.netflix.conductor.common.metadata.tasks.TaskDef; | ||
import com.netflix.conductor.common.metadata.workflow.TaskType; | ||
import com.netflix.conductor.common.metadata.workflow.WorkflowDef; | ||
import com.netflix.conductor.common.metadata.workflow.WorkflowTask; | ||
import com.netflix.conductor.common.run.Workflow; | ||
import com.netflix.conductor.core.execution.ParametersUtils; | ||
import com.netflix.conductor.core.execution.SystemTaskType; | ||
import com.netflix.conductor.core.utils.IDGenerator; | ||
import com.netflix.conductor.dao.MetadataDAO; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNotNull; | ||
import static org.mockito.Mockito.mock; | ||
|
||
public class LambdaTaskMapperTest { | ||
|
||
private ParametersUtils parametersUtils; | ||
|
||
@Before | ||
public void setUp() { | ||
parametersUtils = mock(ParametersUtils.class); | ||
} | ||
|
||
@Test | ||
public void getMappedTasks() 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(new TaskDef()) | ||
.withTaskToSchedule(taskToSchedule) | ||
.withRetryCount(0) | ||
.withTaskId(taskId) | ||
.build(); | ||
|
||
|
||
List<Task> mappedTasks = new LambdaTaskMapper(parametersUtils).getMappedTasks(taskMapperContext); | ||
|
||
assertEquals(1, mappedTasks.size()); | ||
assertNotNull(mappedTasks); | ||
assertEquals(TaskType.LAMBDA.name(), mappedTasks.get(0).getTaskType()); | ||
} | ||
|
||
} |
Oops, something went wrong.