Skip to content

Commit

Permalink
runtime-v2: save out variables for failed process
Browse files Browse the repository at this point in the history
  • Loading branch information
brig committed Dec 19, 2024
1 parent 5ab8d98 commit eab75df
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand All @@ -20,6 +20,7 @@
* =====
*/

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.walmartlabs.concord.runtime.v2.sdk.EvalContext;
import com.walmartlabs.concord.runtime.v2.sdk.EvalContextFactory;
Expand All @@ -43,6 +44,9 @@
*/
public class OutVariablesProcessor implements ExecutionListener {

private static final TypeReference<Map<String, Object>> MAP_TYPE = new TypeReference<>() {
};

private final ObjectMapper objectMapper;
private final PersistenceService persistenceService;
private final List<String> outVariables;
Expand Down Expand Up @@ -84,7 +88,13 @@ public void afterProcessEnds(Runtime runtime, State state, Frame lastFrame) {
return;
}

Map<String, Object> currentOut = persistenceService.loadPersistedFile(Constants.Files.OUT_VALUES_FILE_NAME,
in -> objectMapper.readValue(in, MAP_TYPE));

Map<String, Object> result = new HashMap<>(currentOut != null ? currentOut : Collections.emptyMap());
result.putAll(outValues);

persistenceService.persistFile(Constants.Files.OUT_VALUES_FILE_NAME,
out -> objectMapper.writeValue(out, outValues));
out -> objectMapper.writeValue(out, result));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@
import com.walmartlabs.concord.runtime.common.injector.InstanceId;
import com.walmartlabs.concord.runtime.v2.model.ProcessDefinition;
import com.walmartlabs.concord.runtime.v2.runner.compiler.CompilerUtils;
import com.walmartlabs.concord.runtime.v2.runner.vm.SaveLastErrorCommand;
import com.walmartlabs.concord.runtime.v2.runner.vm.UpdateLocalsCommand;
import com.walmartlabs.concord.runtime.v2.runner.vm.VMUtils;
import com.walmartlabs.concord.runtime.v2.runner.vm.*;
import com.walmartlabs.concord.runtime.v2.sdk.Compiler;
import com.walmartlabs.concord.runtime.v2.sdk.ProcessConfiguration;
import com.walmartlabs.concord.svm.*;
Expand Down Expand Up @@ -77,7 +75,7 @@ public ProcessSnapshot start(ProcessConfiguration processConfiguration, ProcessD
// install the exception handler into the root frame
// takes care of all unhandled errors bubbling up
VMUtils.assertNearestRoot(state, state.getRootThreadId())
.setExceptionHandler(new SaveLastErrorCommand());
.setExceptionHandler(new BlockCommand(new SaveOutVariablesOnErrorCommand(), new SaveLastErrorCommand()));

VM vm = createVM(processDefinition);
// update the global variables using the input map by running a special command
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.walmartlabs.concord.runtime.v2.runner.vm;

/*-
* *****
* Concord
* -----
* Copyright (C) 2017 - 2023 Walmart Inc.
* -----
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =====
*/

import com.walmartlabs.concord.runtime.v2.runner.OutVariablesProcessor;
import com.walmartlabs.concord.svm.Runtime;
import com.walmartlabs.concord.svm.*;

import java.io.Serial;

public class SaveOutVariablesOnErrorCommand implements Command {

@Serial
private static final long serialVersionUID = 1L;

@Override
public void eval(Runtime runtime, State state, ThreadId threadId) throws Exception {
Frame frame = state.peekFrame(threadId);
frame.pop();

runtime.getService(OutVariablesProcessor.class).afterProcessEnds(runtime, state, frame);
}
}

0 comments on commit eab75df

Please sign in to comment.