Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fallback mechanism #1400

Merged
merged 6 commits into from
Jan 13, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@
import io.cloudslang.runtime.api.python.PythonEvaluationResult;
import io.cloudslang.runtime.api.python.PythonRuntimeService;
import io.cloudslang.runtime.api.python.enums.PythonStrategy;
import io.cloudslang.runtime.impl.python.external.ExternalPythonScriptException;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.python.core.Py;
import org.python.core.PyObject;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -44,6 +47,8 @@
*/
@Component
public class ScriptEvaluator extends ScriptProcessor {

private static final Logger logger = LogManager.getLogger(ScriptEvaluator.class);
private static String LINE_SEPARATOR = "\n";
private static final String SYSTEM_PROPERTIES_MAP = "sys_prop";
private static final String ACCESSED_RESOURCES_SET = "accessed_resources_set";
Expand Down Expand Up @@ -128,9 +133,9 @@ private Value doEvaluateExpressionExternalPython(String expr,
}

private Value doEvaluateExpressionPythonExecutor(String expr,
Map<String, Value> context,
Set<SystemProperty> systemProperties,
Set<ScriptFunction> functionDependencies) {
Map<String, Value> context,
Set<SystemProperty> systemProperties,
Set<ScriptFunction> functionDependencies) {
Map<String, Serializable> pythonContext = createExternalPythonContext(context);
boolean systemPropertiesDefined = false;
if (functionDependencies.contains(ScriptFunction.GET_SYSTEM_PROPERTY) ||
Expand All @@ -142,8 +147,15 @@ private Value doEvaluateExpressionPythonExecutor(String expr,
(Serializable) prepareSystemPropertiesForExternalPython(systemProperties));
}

PythonEvaluationResult result = pythonExecutorService.eval(
buildAddFunctionsScriptForExternalPython(functionDependencies), expr, pythonContext);
PythonEvaluationResult result;
try {
result = pythonExecutorService.eval(
buildAddFunctionsScriptForExternalPython(functionDependencies), expr, pythonContext);
} catch (ExternalPythonScriptException exception) {
logger.error("Could not evaluate expressions on python executor, retrying with python");
result = pythonRuntimeService.eval(
buildAddFunctionsScriptForExternalPython(functionDependencies), expr, pythonContext);
}

//noinspection unchecked
Set<String> accessedResources = (Set<String>) result.getResultContext().get(ACCESSED_RESOURCES_SET);
Expand Down