Search in sources :

Example 6 with TaskProviders

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());
}
Also used : TaskProviders(com.walmartlabs.concord.runtime.v2.runner.tasks.TaskProviders) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.jupiter.api.Test)

Example 7 with TaskProviders

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);
}
Also used : CallContext(com.walmartlabs.concord.runtime.v2.runner.tasks.TaskCallInterceptor.CallContext) Frame(com.walmartlabs.concord.svm.Frame) TaskCall(com.walmartlabs.concord.runtime.v2.model.TaskCall) ExpressionEvaluator(com.walmartlabs.concord.runtime.v2.runner.el.ExpressionEvaluator) CallContext(com.walmartlabs.concord.runtime.v2.runner.tasks.TaskCallInterceptor.CallContext) TaskException(com.walmartlabs.concord.runtime.v2.runner.tasks.TaskException) TaskCallOptions(com.walmartlabs.concord.runtime.v2.model.TaskCallOptions) TaskException(com.walmartlabs.concord.runtime.v2.runner.tasks.TaskException) TaskProviders(com.walmartlabs.concord.runtime.v2.runner.tasks.TaskProviders) TaskCallInterceptor(com.walmartlabs.concord.runtime.v2.runner.tasks.TaskCallInterceptor)

Example 8 with TaskProviders

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);
}
Also used : LogContext(com.walmartlabs.concord.runtime.v2.runner.logging.LogContext) Frame(com.walmartlabs.concord.svm.Frame) TaskException(com.walmartlabs.concord.runtime.v2.runner.tasks.TaskException) TaskException(com.walmartlabs.concord.runtime.v2.runner.tasks.TaskException) TaskProviders(com.walmartlabs.concord.runtime.v2.runner.tasks.TaskProviders) TaskCallInterceptor(com.walmartlabs.concord.runtime.v2.runner.tasks.TaskCallInterceptor)

Example 9 with TaskProviders

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);
}
Also used : TaskProviders(com.walmartlabs.concord.runtime.v2.runner.tasks.TaskProviders) Test(org.junit.jupiter.api.Test)

Example 10 with TaskProviders

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);
}
Also used : ThreadId(com.walmartlabs.concord.svm.ThreadId) TaskProviders(com.walmartlabs.concord.runtime.v2.runner.tasks.TaskProviders) State(com.walmartlabs.concord.svm.State)

Aggregations

TaskProviders (com.walmartlabs.concord.runtime.v2.runner.tasks.TaskProviders)18 Test (org.junit.jupiter.api.Test)15 Matchers.containsString (org.hamcrest.Matchers.containsString)14 TaskCallInterceptor (com.walmartlabs.concord.runtime.v2.runner.tasks.TaskCallInterceptor)2 TaskException (com.walmartlabs.concord.runtime.v2.runner.tasks.TaskException)2 Frame (com.walmartlabs.concord.svm.Frame)2 AbstractModule (com.google.inject.AbstractModule)1 Multibinder (com.google.inject.multibindings.Multibinder)1 FormService (com.walmartlabs.concord.runtime.common.FormService)1 TaskCall (com.walmartlabs.concord.runtime.v2.model.TaskCall)1 TaskCallOptions (com.walmartlabs.concord.runtime.v2.model.TaskCallOptions)1 DefaultCheckpointService (com.walmartlabs.concord.runtime.v2.runner.checkpoints.DefaultCheckpointService)1 ExpressionEvaluator (com.walmartlabs.concord.runtime.v2.runner.el.ExpressionEvaluator)1 BeanELResolver (com.walmartlabs.concord.runtime.v2.runner.el.resolvers.BeanELResolver)1 BaseRunnerModule (com.walmartlabs.concord.runtime.v2.runner.guice.BaseRunnerModule)1 LogContext (com.walmartlabs.concord.runtime.v2.runner.logging.LogContext)1 CallContext (com.walmartlabs.concord.runtime.v2.runner.tasks.TaskCallInterceptor.CallContext)1 TaskCallListener (com.walmartlabs.concord.runtime.v2.runner.tasks.TaskCallListener)1 TaskCallPolicyChecker (com.walmartlabs.concord.runtime.v2.runner.tasks.TaskCallPolicyChecker)1 TaskResultListener (com.walmartlabs.concord.runtime.v2.runner.tasks.TaskResultListener)1