use of com.walmartlabs.concord.runtime.v2.runner.tasks.TaskProviders in project concord by walmartlabs.
the class ExpressionEvaluatorTest method testEval4.
@Test
public void testEval4() {
/*
* configuration:
* arguments:
* x: ${y}
* z: ${y.y1}
* y:
* y1: ${task(y.y2)}
* y2: "asdasd"
* y3: ${z}
*/
Map<Object, Object> input = map("x", "${y}", "z", "${y.y1}", "y", map("y1", "${task.foo(y.y2)}", "y2", "abc", "y3", "${z}"));
TaskProviders providers = mock(TaskProviders.class);
TestTask2 task = spy(new TestTask2());
when(providers.createTask(any(), eq("task"))).thenReturn(task);
ExpressionEvaluator ee = new DefaultExpressionEvaluator(providers);
Map<String, Object> vars = Collections.emptyMap();
// scope:
// ---
Map<Object, Object> output = ee.evalAsMap(scope(vars), input);
Map<Object, Object> y = map("y1", "${abc}", "y2", "abc", "y3", "${abc}");
assertThat(output, is(map("x", y, "z", "${abc}", "y", y)));
verify(task, times(1)).foo(anyString());
}
use of com.walmartlabs.concord.runtime.v2.runner.tasks.TaskProviders in project concord by walmartlabs.
the class TaskCallCommand method execute.
@Override
protected void execute(Runtime runtime, State state, ThreadId threadId) {
Frame frame = state.peekFrame(threadId);
frame.pop();
Context ctx = runtime.getService(Context.class);
TaskProviders taskProviders = runtime.getService(TaskProviders.class);
ExpressionEvaluator expressionEvaluator = runtime.getService(ExpressionEvaluator.class);
TaskCall call = getStep();
String taskName = call.getName();
Task t = taskProviders.createTask(ctx, taskName);
if (t == null) {
throw new IllegalStateException("Task not found: '" + taskName + "'");
}
TaskCallInterceptor interceptor = runtime.getService(TaskCallInterceptor.class);
CallContext callContext = CallContext.builder().taskName(taskName).correlationId(ctx.execution().correlationId()).currentStep(getStep()).processDefinition(ctx.execution().processDefinition()).build();
TaskCallOptions opts = Objects.requireNonNull(call.getOptions());
Variables input = new MapBackedVariables(VMUtils.prepareInput(expressionEvaluator, ctx, opts.input(), opts.inputExpression()));
TaskResult result;
try {
result = interceptor.invoke(callContext, Method.of(t, "execute", Collections.singletonList(input)), () -> t.execute(input));
} catch (TaskException e) {
result = TaskResult.fail(e.getCause());
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new RuntimeException(e);
}
TaskCallUtils.processTaskResult(runtime, ctx, taskName, opts, result);
}
use of com.walmartlabs.concord.runtime.v2.runner.tasks.TaskProviders in project concord by walmartlabs.
the class TaskResumeCommand method execute.
@Override
protected void execute(Runtime runtime, State state, ThreadId threadId) {
Frame frame = state.peekFrame(threadId);
frame.pop();
Context ctx = runtime.getService(Context.class);
String taskName = getStep().getName();
TaskProviders taskProviders = runtime.getService(TaskProviders.class);
Task task = taskProviders.createTask(ctx, getStep().getName());
if (task == null) {
throw new IllegalStateException("Task not found: " + taskName);
}
if (!(task instanceof ReentrantTask)) {
throw new IllegalStateException("The task doesn't implement the " + ReentrantTask.class.getSimpleName() + " interface and cannot be used as a \"reentrant\" task: " + taskName);
}
ReentrantTask rt = (ReentrantTask) task;
TaskCallInterceptor.CallContext callContext = TaskCallInterceptor.CallContext.builder().taskName(taskName).correlationId(ctx.execution().correlationId()).currentStep(getStep()).processDefinition(ctx.execution().processDefinition()).build();
TaskCallInterceptor interceptor = runtime.getService(TaskCallInterceptor.class);
TaskResult result;
try {
result = interceptor.invoke(callContext, TaskCallInterceptor.Method.of(rt, "resume", Collections.singletonList(event)), () -> rt.resume(event));
} catch (TaskException e) {
result = TaskResult.fail(e.getCause());
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new RuntimeException(e);
}
TaskCallUtils.processTaskResult(runtime, ctx, taskName, getStep().getOptions(), result);
}
use of com.walmartlabs.concord.runtime.v2.runner.tasks.TaskProviders in project concord by walmartlabs.
the class ImmutablesTest method test.
@Test
public void test() throws Exception {
TestBean testBean = ImmutableTestBean.builder().foo("foo").build();
ExpressionEvaluator ee = new DefaultExpressionEvaluator(new TaskProviders());
Map<String, Object> vars = Collections.singletonMap("testBean", testBean);
// ---
String str = ee.eval(EvalContextFactory.global(new SingleFrameContext(vars)), "Hello ${testBean.foo}", String.class);
assertEquals("Hello foo", str);
}
use of com.walmartlabs.concord.runtime.v2.runner.tasks.TaskProviders in project concord by walmartlabs.
the class ContextVariables method set.
@Override
public void set(String key, Object value) {
TaskProviders providers = ctx.execution().runtime().getService(TaskProviders.class);
if (providers.hasTask(key)) {
log.warn("Local variable '{}' shadows a task. This may cause issues calling '{}' task in expressions. " + "Avoid using same names for tasks and variables.", key, key);
}
ThreadId threadId = ctx.execution().currentThreadId();
State state = ctx.execution().state();
VMUtils.putLocal(state, threadId, key, value);
}
Aggregations