From 7dc8d4c71a04e324422cc26c5474426537e6030e Mon Sep 17 00:00:00 2001 From: opreateodora <74269902+opreateodora@users.noreply.github.com> Date: Fri, 13 Jan 2023 13:49:38 +0200 Subject: [PATCH] Add fallback mechanism (#1400) * add fallback mechanism from python-executor to python when python server is down * refactor * refactor * catch ExternalPythonScriptException * rollback score version * add log in ScriptEvaluator Co-authored-by: TVerdesOprea --- .../bindings/scripts/ScriptEvaluator.java | 22 ++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/cloudslang-runtime/src/main/java/io/cloudslang/lang/runtime/bindings/scripts/ScriptEvaluator.java b/cloudslang-runtime/src/main/java/io/cloudslang/lang/runtime/bindings/scripts/ScriptEvaluator.java index cedf108700..c68e374f6c 100644 --- a/cloudslang-runtime/src/main/java/io/cloudslang/lang/runtime/bindings/scripts/ScriptEvaluator.java +++ b/cloudslang-runtime/src/main/java/io/cloudslang/lang/runtime/bindings/scripts/ScriptEvaluator.java @@ -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; @@ -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"; @@ -128,9 +133,9 @@ private Value doEvaluateExpressionExternalPython(String expr, } private Value doEvaluateExpressionPythonExecutor(String expr, - Map context, - Set systemProperties, - Set functionDependencies) { + Map context, + Set systemProperties, + Set functionDependencies) { Map pythonContext = createExternalPythonContext(context); boolean systemPropertiesDefined = false; if (functionDependencies.contains(ScriptFunction.GET_SYSTEM_PROPERTY) || @@ -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 accessedResources = (Set) result.getResultContext().get(ACCESSED_RESOURCES_SET);