Skip to content

Commit 94526fe

Browse files
committed
add condition timeout
1 parent 99838da commit 94526fe

File tree

3 files changed

+53
-10
lines changed

3 files changed

+53
-10
lines changed

core/src/main/java/com/flowci/core/job/config/JobConfig.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import com.flowci.core.common.config.AppProperties;
2020
import com.flowci.core.common.helper.CacheHelper;
21+
import com.flowci.core.common.helper.ThreadHelper;
2122
import com.flowci.core.job.domain.Step;
2223
import com.flowci.tree.NodeTree;
2324
import com.flowci.util.FileHelper;
@@ -26,11 +27,13 @@
2627
import org.springframework.beans.factory.annotation.Autowired;
2728
import org.springframework.context.annotation.Bean;
2829
import org.springframework.context.annotation.Configuration;
30+
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
2931

3032
import java.io.IOException;
3133
import java.nio.file.Path;
3234
import java.nio.file.Paths;
3335
import java.util.List;
36+
import java.util.concurrent.ThreadPoolExecutor;
3437

3538
/**
3639
* @author yang
@@ -58,4 +61,9 @@ public Path pluginDir() throws IOException {
5861
Path pluginDir = Paths.get(workspace, "repos");
5962
return FileHelper.createDirectory(pluginDir);
6063
}
64+
65+
@Bean("jobConditionExecutor")
66+
public ThreadPoolTaskExecutor jobConditionExecutor() {
67+
return ThreadHelper.createTaskExecutor(20, 20, 100, "job-cond-");
68+
}
6169
}

core/src/main/java/com/flowci/core/job/manager/ConditionManagerImpl.java

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,21 @@
77
import groovy.lang.GroovyShell;
88
import groovy.util.ScriptException;
99
import lombok.extern.log4j.Log4j2;
10-
import org.codehaus.groovy.control.CompilationFailedException;
10+
import org.springframework.beans.factory.annotation.Autowired;
11+
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
1112
import org.springframework.stereotype.Component;
1213

14+
import java.util.concurrent.*;
15+
1316
@Log4j2
1417
@Component
1518
public class ConditionManagerImpl implements ConditionManager {
1619

20+
private final int DefaultTimeout = 2; // seconds
21+
22+
@Autowired
23+
private ThreadPoolTaskExecutor jobConditionExecutor;
24+
1725
@Override
1826
public boolean run(CmdIn in) throws ScriptException {
1927
if (!(in instanceof ShellIn)) {
@@ -25,18 +33,31 @@ public boolean run(CmdIn in) throws ScriptException {
2533
return true;
2634
}
2735

28-
Binding binding = new Binding();
29-
shell.getInputs().forEach(binding::setVariable);
36+
Future<Boolean> submit = jobConditionExecutor.submit(() -> {
37+
Binding binding = new Binding();
38+
shell.getInputs().forEach(binding::setVariable);
3039

31-
try {
32-
GroovyShell groovy = new GroovyShell(binding);
33-
Object value = groovy.evaluate(shell.getCondition());
34-
if (value instanceof Boolean) {
35-
return (Boolean) value;
40+
try {
41+
GroovyShell groovy = new GroovyShell(binding);
42+
Object value = groovy.evaluate(shell.getCondition());
43+
if (value instanceof Boolean) {
44+
return (Boolean) value;
45+
}
46+
throw new Exception("The return type is not boolean");
47+
} catch (GroovyRuntimeException e) {
48+
throw new Exception(e.getMessage());
3649
}
37-
throw new ScriptException("The return type is not boolean");
38-
} catch (GroovyRuntimeException e) {
50+
});
51+
52+
try {
53+
return submit.get(DefaultTimeout, TimeUnit.SECONDS);
54+
} catch (InterruptedException e) {
55+
throw new ScriptException("Condition script interrupted");
56+
} catch (ExecutionException e) {
3957
throw new ScriptException(e.getMessage());
58+
} catch (TimeoutException e) {
59+
submit.cancel(true);
60+
throw new ScriptException("Condition script timeout");
4061
}
4162
}
4263
}

core/src/test/java/com/flowci/core/test/job/ConditionManagerTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,18 @@ public void should_throw_exception_if_wrong_return_type() throws ScriptException
3737

3838
conditionManager.run(in);
3939
}
40+
41+
@Test(expected = ScriptException.class)
42+
public void should_throw_exception_if_timeout() throws ScriptException {
43+
ShellIn in = new ShellIn();
44+
in.getInputs().put("foo", "helloword");
45+
in.setCondition(
46+
"sleep(6000); "
47+
+ "println \"$foo\"; "
48+
+ "hello = \"1234\";"
49+
+ "return true;"
50+
);
51+
52+
conditionManager.run(in);
53+
}
4054
}

0 commit comments

Comments
 (0)