Skip to content
Snippets Groups Projects
Commit b94a0b0a authored by C. Alexander Leigh's avatar C. Alexander Leigh
Browse files

lc-esp-script-engine: Added support for configuring script timeout

parent 3603fb17
No related branches found
No related tags found
No related merge requests found
......@@ -28,6 +28,7 @@ import java.util.concurrent.*;
public class ESPScriptEngine {
private final ScriptEngine engine;
private static final MechaLogger logger = MechaLoggerFactory.getLogger(ESPScriptEngine.class);
private int timeoutSeconds = 10;
public ESPScriptEngine() {
engine = GraalJSScriptEngine.create(null,
......@@ -42,7 +43,7 @@ public class ESPScriptEngine {
final Context context = Context.newBuilder("js").build();
final Future<Object> futureResult = executor.submit(() -> context.eval("js", script));
try {
final Object result = futureResult.get(10, TimeUnit.SECONDS);
final Object result = futureResult.get(timeoutSeconds, TimeUnit.SECONDS);
logger.info("Script evaluated within 10 seconds, result: " + result);
} catch (TimeoutException e) {
context.interrupt(Duration.ZERO);
......@@ -51,6 +52,14 @@ public class ESPScriptEngine {
}
}
public int getTimeoutSeconds() {
return timeoutSeconds;
}
public void setTimeoutSeconds(int timeoutSeconds) {
this.timeoutSeconds = timeoutSeconds;
}
public Object get(String key) {
return engine.get(key);
}
......
......@@ -26,6 +26,7 @@ public class ScriptContextTest {
@Test
public void scriptInterruptionTest() throws ScriptException, ExecutionException, InterruptedException {
ESPScriptEngine sc = new ESPScriptEngine();
sc.setTimeoutSeconds(1);
boolean worked = false;
try {
sc.eval("while(true){}");
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment