Search in sources :

Example 16 with Variables

use of com.walmartlabs.concord.runtime.v2.sdk.Variables in project concord by walmartlabs.

the class Runner method resume.

public ProcessSnapshot resume(ProcessSnapshot snapshot, Map<String, Object> input) throws Exception {
    statusCallback.onRunning(instanceId.getValue());
    log.debug("resume -> running...");
    State state = snapshot.vmState();
    VM vm = createVM(snapshot.processDefinition());
    // update the global variables using the input map by running a special command
    vm.run(state, new UpdateLocalsCommand(input));
    // continue as usual
    vm.start(state);
    log.debug("resume -> done");
    return ProcessSnapshot.builder().from(snapshot).vmState(state).build();
}
Also used : UpdateLocalsCommand(com.walmartlabs.concord.runtime.v2.runner.vm.UpdateLocalsCommand)

Example 17 with Variables

use of com.walmartlabs.concord.runtime.v2.sdk.Variables in project concord by walmartlabs.

the class Runner method start.

public ProcessSnapshot start(ProcessConfiguration processConfiguration, ProcessDefinition processDefinition, Map<String, Object> input) throws Exception {
    statusCallback.onRunning(instanceId.getValue());
    log.debug("start ['{}'] -> running...", processConfiguration.entryPoint());
    Command cmd = CompilerUtils.compile(compiler, processConfiguration, processDefinition, processConfiguration.entryPoint());
    State state = new InMemoryState(cmd);
    // install the exception handler into the root frame
    // takes care of all unhandled errors bubbling up
    VMUtils.assertNearestRoot(state, state.getRootThreadId()).setExceptionHandler(new SaveLastErrorCommand());
    VM vm = createVM(processDefinition);
    // update the global variables using the input map by running a special command
    // TODO merge with the cfg's arguments
    vm.run(state, new UpdateLocalsCommand(input));
    // start the normal execution
    vm.start(state);
    log.debug("start ['{}'] -> done", processConfiguration.entryPoint());
    return ProcessSnapshot.builder().vmState(state).processDefinition(processDefinition).build();
}
Also used : SaveLastErrorCommand(com.walmartlabs.concord.runtime.v2.runner.vm.SaveLastErrorCommand) UpdateLocalsCommand(com.walmartlabs.concord.runtime.v2.runner.vm.UpdateLocalsCommand) SaveLastErrorCommand(com.walmartlabs.concord.runtime.v2.runner.vm.SaveLastErrorCommand) UpdateLocalsCommand(com.walmartlabs.concord.runtime.v2.runner.vm.UpdateLocalsCommand)

Example 18 with Variables

use of com.walmartlabs.concord.runtime.v2.sdk.Variables 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 19 with Variables

use of com.walmartlabs.concord.runtime.v2.sdk.Variables 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)

Example 20 with Variables

use of com.walmartlabs.concord.runtime.v2.sdk.Variables in project concord by walmartlabs.

the class Runner method resume.

public ProcessSnapshot resume(ProcessSnapshot snapshot, Set<String> eventRefs, Map<String, Object> input) throws Exception {
    statusCallback.onRunning(instanceId.getValue());
    log.debug("resume ['{}'] -> running...", eventRefs);
    State state = snapshot.vmState();
    VM vm = createVM(snapshot.processDefinition());
    // update the global variables using the input map by running a special command
    // only the threads with the specified eventRef will receive the input
    Collection<ThreadId> resumingThreads = state.getEventRefs().entrySet().stream().filter(kv -> eventRefs.contains(kv.getValue())).map(Map.Entry::getKey).collect(Collectors.toList());
    vm.run(state, new UpdateLocalsCommand(input, resumingThreads));
    // resume normally
    vm.resume(state, eventRefs);
    log.debug("resume ['{}'] -> done", eventRefs);
    return ProcessSnapshot.builder().from(snapshot).vmState(state).build();
}
Also used : Map(java.util.Map) UpdateLocalsCommand(com.walmartlabs.concord.runtime.v2.runner.vm.UpdateLocalsCommand)

Aggregations

Variables (com.walmartlabs.concord.runtime.v2.sdk.Variables)24 MapBackedVariables (com.walmartlabs.concord.runtime.v2.sdk.MapBackedVariables)16 HashMap (java.util.HashMap)7 Test (org.junit.Test)6 SimpleResult (com.walmartlabs.concord.runtime.v2.sdk.TaskResult.SimpleResult)5 Path (java.nio.file.Path)4 Map (java.util.Map)4 TaskProviders (com.walmartlabs.concord.runtime.v2.runner.tasks.TaskProviders)3 UpdateLocalsCommand (com.walmartlabs.concord.runtime.v2.runner.vm.UpdateLocalsCommand)3 TaskResult (com.walmartlabs.concord.runtime.v2.sdk.TaskResult)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 ProcessDefinition (com.walmartlabs.concord.runtime.v2.model.ProcessDefinition)2 ExpressionEvaluator (com.walmartlabs.concord.runtime.v2.runner.el.ExpressionEvaluator)2 com.walmartlabs.concord.runtime.v2.sdk (com.walmartlabs.concord.runtime.v2.sdk)2 ProcessConfiguration (com.walmartlabs.concord.runtime.v2.sdk.ProcessConfiguration)2 Inject (javax.inject.Inject)2 Named (javax.inject.Named)2 Injector (com.google.inject.Injector)1 ApiClient (com.walmartlabs.concord.ApiClient)1 DependencyManager (com.walmartlabs.concord.dependencymanager.DependencyManager)1