Search in sources :

Example 6 with TaskResult

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);
}
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 7 with TaskResult

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);
}
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 8 with TaskResult

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());
    }
}
Also used : TaskResult(com.walmartlabs.concord.runtime.v2.sdk.TaskResult) ApiClient(com.walmartlabs.concord.ApiClient)

Example 9 with TaskResult

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());
}
Also used : Path(java.nio.file.Path) com.walmartlabs.concord.runtime.v2.sdk(com.walmartlabs.concord.runtime.v2.sdk) Inject(javax.inject.Inject) Logger(org.slf4j.Logger) Files(java.nio.file.Files) Paths(java.nio.file.Paths) SUCCESS_EXIT_CODE(com.walmartlabs.concord.plugins.docker.DockerConstants.SUCCESS_EXIT_CODE) LoggerFactory(org.slf4j.LoggerFactory) Named(javax.inject.Named) Path(java.nio.file.Path) InputStream(java.io.InputStream) InputStream(java.io.InputStream)

Example 10 with TaskResult

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);
}
Also used : ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) TaskResult(com.walmartlabs.concord.runtime.v2.sdk.TaskResult) Result(com.walmartlabs.concord.plugins.s3.Result)

Aggregations

TaskResult (com.walmartlabs.concord.runtime.v2.sdk.TaskResult)6 ApiClient (com.walmartlabs.concord.ApiClient)3 Path (java.nio.file.Path)3 TaskCallInterceptor (com.walmartlabs.concord.runtime.v2.runner.tasks.TaskCallInterceptor)2 TaskException (com.walmartlabs.concord.runtime.v2.runner.tasks.TaskException)2 TaskProviders (com.walmartlabs.concord.runtime.v2.runner.tasks.TaskProviders)2 com.walmartlabs.concord.runtime.v2.sdk (com.walmartlabs.concord.runtime.v2.sdk)2 Frame (com.walmartlabs.concord.svm.Frame)2 Inject (javax.inject.Inject)2 Named (javax.inject.Named)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 AnsibleSecretService (com.walmartlabs.concord.plugins.ansible.secrets.AnsibleSecretService)1 ConfluenceTaskCommon (com.walmartlabs.concord.plugins.confluence.ConfluenceTaskCommon)1 Result (com.walmartlabs.concord.plugins.confluence.Result)1 SUCCESS_EXIT_CODE (com.walmartlabs.concord.plugins.docker.DockerConstants.SUCCESS_EXIT_CODE)1 Result (com.walmartlabs.concord.plugins.msteams.Result)1 Result (com.walmartlabs.concord.plugins.s3.Result)1 TerraformTaskCommon.getAction (com.walmartlabs.concord.plugins.terraform.TerraformTaskCommon.getAction)1 TerraformTaskCommon.getEnv (com.walmartlabs.concord.plugins.terraform.TerraformTaskCommon.getEnv)1 TerraformActionResult (com.walmartlabs.concord.plugins.terraform.actions.TerraformActionResult)1