Search in sources :

Example 11 with Context

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

the class ScriptCallCommand method execute.

@Override
protected void execute(Runtime runtime, State state, ThreadId threadId) {
    state.peekFrame(threadId).pop();
    Context ctx = runtime.getService(Context.class);
    ExpressionEvaluator expressionEvaluator = runtime.getService(ExpressionEvaluator.class);
    ScriptEvaluator scriptEvaluator = runtime.getService(ScriptEvaluator.class);
    ResourceResolver resourceResolver = runtime.getService(ResourceResolver.class);
    ScriptCall call = getStep();
    ScriptCallOptions opts = Objects.requireNonNull(call.getOptions());
    Map<String, Object> input = VMUtils.prepareInput(expressionEvaluator, ctx, opts.input(), opts.inputExpression());
    String language = getLanguage(expressionEvaluator, scriptEvaluator, ctx, call);
    Reader content = getContent(expressionEvaluator, resourceResolver, ctx, call);
    ScriptResult scriptResult;
    try {
        scriptResult = scriptEvaluator.eval(ctx, language, content, input);
    } finally {
        try {
            content.close();
        } catch (IOException e) {
            // we don't have to do anything about it, but we're going to log the error just in case
            log.warn("Error while closing the script's reader: {}", e.getMessage() + ". This is most likely a bug.");
        }
    }
    OutputUtils.process(runtime, ctx, scriptResult.items(), opts.out(), opts.outExpr());
}
Also used : Context(com.walmartlabs.concord.runtime.v2.sdk.Context) ScriptResult(com.walmartlabs.concord.runtime.v2.runner.script.ScriptResult) ScriptEvaluator(com.walmartlabs.concord.runtime.v2.runner.script.ScriptEvaluator) ScriptCallOptions(com.walmartlabs.concord.runtime.v2.model.ScriptCallOptions) ScriptCall(com.walmartlabs.concord.runtime.v2.model.ScriptCall) ResourceResolver(com.walmartlabs.concord.runtime.v2.runner.ResourceResolver) ExpressionEvaluator(com.walmartlabs.concord.runtime.v2.runner.el.ExpressionEvaluator)

Example 12 with Context

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

the class SetVariablesCommand method execute.

@Override
@SuppressWarnings("unchecked")
protected void execute(Runtime runtime, State state, ThreadId threadId) {
    state.peekFrame(threadId).pop();
    SetVariablesStep step = getStep();
    Context ctx = runtime.getService(Context.class);
    // eval the input
    ExpressionEvaluator ee = runtime.getService(ExpressionEvaluator.class);
    Map<String, Object> vars = ee.evalAsMap(EvalContextFactory.scope(ctx), step.getVars());
    vars.forEach((k, v) -> {
        Object o = ctx.variables().get(k);
        if (o instanceof Map && v instanceof Map) {
            v = deepMerge((Map<String, Object>) o, (Map<String, Object>) v);
        }
        ctx.variables().set(k, v);
    });
}
Also used : Context(com.walmartlabs.concord.runtime.v2.sdk.Context) SetVariablesStep(com.walmartlabs.concord.runtime.v2.model.SetVariablesStep) ExpressionEvaluator(com.walmartlabs.concord.runtime.v2.runner.el.ExpressionEvaluator) Map(java.util.Map)

Example 13 with Context

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

the class EvalAsMapFunction method evalAsMap.

@SuppressWarnings("unchecked")
public static Map<String, Object> evalAsMap(Object value) {
    Context ctx = ThreadLocalEvalContext.get().context();
    if (ctx == null) {
        return null;
    }
    ExpressionEvaluator ee = ctx.execution().runtime().getService(ExpressionEvaluator.class);
    Map<String, Object> evalValue = ee.evalAsMap(EvalContextFactory.scope(ctx), value);
    Map<String, Object> result = new HashMap<>();
    evalValue.forEach((k, v) -> {
        Object o = ctx.variables().get(k);
        if (o instanceof Map && v instanceof Map) {
            v = deepMerge((Map<String, Object>) o, (Map<String, Object>) v);
        }
        if (v != null) {
            result.put(k, v);
        }
    });
    return result;
}
Also used : Context(com.walmartlabs.concord.runtime.v2.sdk.Context) ThreadLocalEvalContext(com.walmartlabs.concord.runtime.v2.runner.el.ThreadLocalEvalContext) HashMap(java.util.HashMap) ExpressionEvaluator(com.walmartlabs.concord.runtime.v2.runner.el.ExpressionEvaluator) Map(java.util.Map) HashMap(java.util.HashMap)

Example 14 with Context

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

the class CheckpointCommand method execute.

@Override
protected void execute(Runtime runtime, State state, ThreadId threadId) {
    state.peekFrame(threadId).pop();
    // eval the name in case it contains an expression
    Context ctx = runtime.getService(Context.class);
    ExpressionEvaluator ee = runtime.getService(ExpressionEvaluator.class);
    String name = ee.eval(EvalContextFactory.global(ctx), getStep().getName(), String.class);
    runtime.getService(SynchronizationService.class).point(() -> {
        CheckpointService checkpointService = runtime.getService(CheckpointService.class);
        ProcessDefinition processDefinition = runtime.getService(ProcessDefinition.class);
        // cleanup the internal state to reduce the serialized data size
        state.gc();
        // TODO validate checkpoint name
        checkpointService.create(threadId, getCorrelationId(), name, runtime, ProcessSnapshot.builder().vmState(state).processDefinition(processDefinition).build());
    });
}
Also used : Context(com.walmartlabs.concord.runtime.v2.sdk.Context) SynchronizationService(com.walmartlabs.concord.runtime.v2.runner.SynchronizationService) CheckpointService(com.walmartlabs.concord.runtime.v2.runner.checkpoints.CheckpointService) ProcessDefinition(com.walmartlabs.concord.runtime.v2.model.ProcessDefinition) ExpressionEvaluator(com.walmartlabs.concord.runtime.v2.runner.el.ExpressionEvaluator)

Example 15 with Context

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

the class FormCallCommand method execute.

@Override
protected void execute(Runtime runtime, State state, ThreadId threadId) {
    String eventRef = UUID.randomUUID().toString();
    Context ctx = runtime.getService(Context.class);
    ExpressionEvaluator expressionEvaluator = runtime.getService(ExpressionEvaluator.class);
    EvalContext evalContext = EvalContextFactory.global(ctx);
    FormCall call = getStep();
    String formName = expressionEvaluator.eval(evalContext, call.getName(), String.class);
    ProcessDefinition processDefinition = runtime.getService(ProcessDefinition.class);
    ProcessConfiguration processConfiguration = runtime.getService(ProcessConfiguration.class);
    List<FormField> fields = assertFormFields(expressionEvaluator, evalContext, processConfiguration, processDefinition, formName, call);
    Form form = Form.builder().name(formName).eventName(eventRef).options(buildFormOptions(expressionEvaluator, evalContext, call)).fields(buildFormFields(expressionEvaluator, evalContext, fields, Objects.requireNonNull(call.getOptions()).values())).build();
    FormService formService = runtime.getService(FormService.class);
    formService.save(form);
    state.peekFrame(threadId).pop();
    state.setEventRef(threadId, eventRef);
    state.setStatus(threadId, ThreadStatus.SUSPENDED);
}
Also used : EvalContext(com.walmartlabs.concord.runtime.v2.runner.el.EvalContext) Context(com.walmartlabs.concord.runtime.v2.sdk.Context) ProcessConfiguration(com.walmartlabs.concord.runtime.v2.sdk.ProcessConfiguration) Form(com.walmartlabs.concord.forms.Form) FormService(com.walmartlabs.concord.runtime.common.FormService) EvalContext(com.walmartlabs.concord.runtime.v2.runner.el.EvalContext) ExpressionEvaluator(com.walmartlabs.concord.runtime.v2.runner.el.ExpressionEvaluator)

Aggregations

Context (com.walmartlabs.concord.runtime.v2.sdk.Context)18 ExpressionEvaluator (com.walmartlabs.concord.runtime.v2.runner.el.ExpressionEvaluator)15 MapBackedVariables (com.walmartlabs.concord.runtime.v2.sdk.MapBackedVariables)6 HashMap (java.util.HashMap)6 ContextFactory (com.walmartlabs.concord.runtime.v2.runner.context.ContextFactory)5 EvalContext (com.walmartlabs.concord.runtime.v2.runner.el.EvalContext)4 EvalContextFactory (com.walmartlabs.concord.runtime.v2.runner.el.EvalContextFactory)4 TaskResult (com.walmartlabs.concord.runtime.v2.sdk.TaskResult)4 Map (java.util.Map)4 ServerSetupTest (com.icegreen.greenmail.util.ServerSetupTest)3 ApiClient (com.walmartlabs.concord.ApiClient)3 ProcessDefinition (com.walmartlabs.concord.runtime.v2.model.ProcessDefinition)3 BlockCommand (com.walmartlabs.concord.runtime.v2.runner.vm.BlockCommand)3 Variables (com.walmartlabs.concord.runtime.v2.sdk.Variables)3 Command (com.walmartlabs.concord.svm.Command)3 MimeMessage (javax.mail.internet.MimeMessage)3 Test (org.junit.jupiter.api.Test)3 HashiVaultTask (com.walmartlabs.concord.plugins.hashivault.v2.HashiVaultTask)2 Step (com.walmartlabs.concord.runtime.v2.model.Step)2 LogContext (com.walmartlabs.concord.runtime.v2.runner.logging.LogContext)2