use of com.walmartlabs.concord.runtime.v2.sdk.TaskResult 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.sdk.TaskResult 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.sdk.TaskResult in project concord by walmartlabs.
the class SleepTask method execute.
@Override
public void execute(Context ctx) throws Exception {
Supplier<Suspender> suspender = () -> {
ApiClient apiClient = apiClientFactory.create(ApiClientConfiguration.builder().context(ctx).build());
return new Suspender(apiClient, ContextUtils.getTxId(ctx));
};
Map<String, Object> cfg = createCfg(ctx);
TaskResult taskResult = new SleepTaskCommon(suspender).execute(new TaskParams(cfg));
if (taskResult instanceof TaskResult.SuspendResult) {
ctx.suspend(((TaskResult.SuspendResult) taskResult).eventName());
}
}
use of com.walmartlabs.concord.runtime.v2.sdk.TaskResult in project concord by walmartlabs.
the class DockerTaskV2 method execute.
@Override
public TaskResult execute(Variables input) throws Exception {
Path workDir = this.workDir.getValue();
TaskParams params = new TaskParams(input);
boolean logStdOut = input.getBoolean(LOG_STD_OUT_KEY, true);
boolean logStdError = input.getBoolean(LOG_STD_ERR_KEY, true);
boolean saveStdOut = input.getBoolean(SAVE_STD_OUT_KEY, false);
boolean saveStdError = input.getBoolean(SAVE_STD_ERR_KEY, false);
boolean redirectErrorStream = input.getBoolean(REDIRECT_ERROR_STREAM_KEY, false);
String stdOutFilePath = null;
if (saveStdOut) {
Path logFile = DockerTaskCommon.createTmpFile(workDir, "stdout", ".log");
stdOutFilePath = workDir.relativize(logFile).toString();
}
DockerContainerSpec spec = DockerContainerSpec.builder().image(params.image()).env(DockerTaskCommon.stringify(params.env())).envFile(DockerTaskCommon.getEnvFile(workDir, params)).entryPoint(DockerTaskCommon.prepareEntryPoint(workDir, params)).forcePull(params.forcePull()).options(DockerContainerSpec.Options.builder().hosts(params.hosts()).build()).debug(params.debug()).redirectErrorStream(redirectErrorStream).stdOutFilePath(stdOutFilePath).pullRetryCount(params.pullRetryCount()).pullRetryInterval(params.pullRetryInterval()).build();
StringBuilder stdErr = new StringBuilder();
int code = dockerService.start(spec, logStdOut ? line -> processLog.info("DOCKER: {}", line) : null, logStdError || saveStdError ? line -> {
if (logStdError) {
processLog.info("DOCKER: {}", line);
}
if (saveStdError) {
stdErr.append(line).append("\n");
}
} : null);
String stdOut = null;
if (stdOutFilePath != null) {
InputStream inputStream = Files.newInputStream(Paths.get(stdOutFilePath));
stdOut = DockerTaskCommon.toString(inputStream);
}
if (code != SUCCESS_EXIT_CODE) {
log.warn("call ['{}', '{}', '{}'] -> finished with code {}", params.image(), params.cmd(), workDir, code);
return TaskResult.fail("Docker process finished with exit code " + code).value("stdout", stdOut).value("stderr", stdErr.toString());
}
log.info("call ['{}', '{}', '{}', '{}'] -> done", params.image(), params.cmd(), workDir, params.hosts());
return TaskResult.success().value("stdout", stdOut).value("stderr", stdErr.toString());
}
use of com.walmartlabs.concord.runtime.v2.sdk.TaskResult in project concord-plugins by walmartlabs.
the class S3TaskV2 method execute.
@Override
@SuppressWarnings("unchecked")
public TaskResult execute(Variables input) throws Exception {
Result result = delegate.execute(TaskParams.of(input, context.defaultVariables().toMap()));
ObjectMapper om = new ObjectMapper();
Map<String, Object> r = om.convertValue(result, Map.class);
return TaskResult.of(MapUtils.getBoolean(r, "ok", false), MapUtils.getString(r, "error")).values(r);
}
Aggregations