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

[JBPM-9334] Process execution stuck at ExecWorkItemHandler node if co… #147

Merged
merged 18 commits into from Oct 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
b7efa41
[JBPM-9334] Process execution stuck at ExecWorkItemHandler node if co…
abhijithumbe Sep 10, 2020
c5cebe6
[JBPM-9334] Process execution stuck at ExecWorkItemHandler node if co…
abhijithumbe Sep 13, 2020
f39cade
[JBPM-9334] Process execution stuck at ExecWorkItemHandler node if co…
abhijithumbe Sep 14, 2020
90d129e
[JBPM-9334] Process execution stuck at ExecWorkItemHandler node if co…
abhijithumbe Oct 5, 2020
5d01ab3
[JBPM-9334] Process execution stuck at ExecWorkItemHandler node if co…
abhijithumbe Oct 6, 2020
7814e8c
[JBPM-9334] Process execution stuck at ExecWorkItemHandler node if co…
abhijithumbe Oct 6, 2020
99b15a2
[JBPM-9334] Process execution stuck at ExecWorkItemHandler node if co…
abhijithumbe Oct 6, 2020
5a11cfc
[JBPM-9334] Process execution stuck at ExecWorkItemHandler node if co…
abhijithumbe Oct 7, 2020
087cf79
[JBPM-9334] Process execution stuck at ExecWorkItemHandler node if co…
abhijithumbe Oct 7, 2020
e9d8746
[JBPM-9334] Process execution stuck at ExecWorkItemHandler node if co…
abhijithumbe Oct 7, 2020
7035dd6
[JBPM-9334] Process execution stuck at ExecWorkItemHandler node if co…
abhijithumbe Oct 7, 2020
0a87bcb
[JBPM-9334] Process execution stuck at ExecWorkItemHandler node if co…
abhijithumbe Oct 7, 2020
e4f580d
[JBPM-9334] Process execution stuck at ExecWorkItemHandler node if co…
abhijithumbe Oct 7, 2020
3cd9f2a
[JBPM-9334] Process execution stuck at ExecWorkItemHandler node if co…
abhijithumbe Oct 7, 2020
74d3b61
[JBPM-9334] Process execution stuck at ExecWorkItemHandler node if co…
abhijithumbe Oct 7, 2020
0218a2c
[JBPM-9334] Process execution stuck at ExecWorkItemHandler node if co…
abhijithumbe Oct 7, 2020
a8c0e74
[JBPM-9334] Process execution stuck at ExecWorkItemHandler node if co…
abhijithumbe Oct 7, 2020
65804ab
[JBPM-9334] Process execution stuck at ExecWorkItemHandler node if co…
abhijithumbe Oct 7, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -16,12 +16,19 @@
package org.jbpm.process.workitem.exec;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.time.Duration;
import java.time.OffsetDateTime;
import java.time.Period;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.ExecuteException;
import org.apache.commons.exec.ExecuteWatchdog;
import org.apache.commons.exec.PumpStreamHandler;
import org.jbpm.process.workitem.core.AbstractLogOrThrowWorkItemHandler;
import org.jbpm.process.workitem.core.util.RequiredParameterValidator;
Expand All @@ -34,32 +41,36 @@
import org.jbpm.process.workitem.core.util.service.WidService;
import org.kie.api.runtime.process.WorkItem;
import org.kie.api.runtime.process.WorkItemManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Wid(widfile = "ExecDefinitions.wid", name = "Exec",
displayName = "Exec",
defaultHandler = "mvel: new org.jbpm.process.workitem.exec.ExecWorkItemHandler()",
documentation = "${artifactId}/index.html",
category = "${artifactId}",
icon = "Exec.png",
parameters = {
@WidParameter(name = "Command", required = true),
@WidParameter(name = "Arguments", runtimeType = "java.util.List")
},
results = {
displayName = "Exec",
defaultHandler = "mvel: new org.jbpm.process.workitem.exec.ExecWorkItemHandler()",
documentation = "${artifactId}/index.html",
category = "${artifactId}",
icon = "Exec.png",
parameters = {
@WidParameter(name = "Command", required = true),
@WidParameter(name = "Arguments", runtimeType = "java.util.List"),
@WidParameter(name = "TimeoutInMillis", runtimeType = "java.lang.String")
},
results = {
@WidResult(name = "Output")
},
mavenDepends = {
@WidMavenDepends(group = "${groupId}", artifact = "${artifactId}", version = "${version}")
},
serviceInfo = @WidService(category = "${name}", description = "${description}",
keywords = "execute,comand",
action = @WidAction(title = "Execute a command"),
authinfo = @WidAuth
))
},
mavenDepends = {
@WidMavenDepends(group = "${groupId}", artifact = "${artifactId}", version = "${version}")
},
serviceInfo = @WidService(category = "${name}", description = "${description}",
keywords = "execute,comand",
action = @WidAction(title = "Execute a command"),
authinfo = @WidAuth))
public class ExecWorkItemHandler extends AbstractLogOrThrowWorkItemHandler {

private static final Logger logger = LoggerFactory.getLogger(ExecWorkItemHandler.class);
public static final String RESULT = "Output";
private String parsedCommandStr = "";
private long defaultTimeout = 4000L;

public void executeWorkItem(WorkItem workItem,
WorkItemManager manager) {
Expand All @@ -71,27 +82,17 @@ public void executeWorkItem(WorkItem workItem,

String command = (String) workItem.getParameter("Command");
List<String> arguments = (List<String>) workItem.getParameter("Arguments");
String commandExecutionTimeout = (String) workItem.getParameter("TimeoutInMillis");

Map<String, Object> results = new HashMap<>();

CommandLine commandLine = CommandLine.parse(command);
if (arguments != null && arguments.size() > 0) {
commandLine.addArguments(arguments.toArray(new String[0]),
true);
if (commandExecutionTimeout != null) {
this.setDefaultTimeout(parseTimeout(commandExecutionTimeout));
}

parsedCommandStr = commandLine.toString();

DefaultExecutor executor = new DefaultExecutor();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream);
executor.setStreamHandler(streamHandler);
executor.execute(commandLine);
String executionResult = executeCommand(command, arguments, defaultTimeout);

Map<String, Object> results = new HashMap<>();
results.put(RESULT,
outputStream.toString());

outputStream.close();
executionResult);

manager.completeWorkItem(workItem.getId(),
results);
Expand All @@ -100,11 +101,72 @@ public void executeWorkItem(WorkItem workItem,
}
}

protected long parseTimeout(String durationStr) {
try {
if (durationStr.startsWith("PT")) { // ISO-8601 PTnHnMn.nS
return Duration.parse(durationStr).toMillis();
} else if (!durationStr.contains("T")) { // ISO-8601 PnYnMnWnD
Period period = Period.parse(durationStr);
OffsetDateTime now = OffsetDateTime.now();
return Duration.between(now, now.plus(period)).toMillis();
} else { // ISO-8601 PnYnMnWnDTnHnMn.nS
String[] elements = durationStr.split("T");
Period period = Period.parse(elements[0]);
Duration duration = Duration.parse("PT" + elements[1]);
OffsetDateTime now = OffsetDateTime.now();

return Duration.between(now, now.plus(period).plus(duration)).toMillis();
}
} catch (Exception e) {
logger.error("Exception occured while parsing provided timeout" + durationStr + ".Default timeout of" +
defaultTimeout + "ms will be used for command execution");
return defaultTimeout;
}
}

protected String executeCommand(String command, List<String> arguments, long timeout) throws IOException {

CommandLine commandLine = CommandLine.parse(command);
if (arguments != null && arguments.size() > 0) {
commandLine.addArguments(arguments.toArray(new String[0]), true);
}

parsedCommandStr = commandLine.toString();

ExecuteWatchdog watchdog = new ExecuteWatchdog(timeout);
DefaultExecutor executor = new DefaultExecutor();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream);
executor.setStreamHandler(streamHandler);
executor.setWatchdog(watchdog);
try {
executor.execute(commandLine);
} catch (ExecuteException e) {
if (watchdog.killedProcess()) {
logger.error("A timeout occured after " + timeout + "ms while executing a command " +
parsedCommandStr.replace(",", ""));
throw new RuntimeException("A timeout occured after " + timeout + "ms while executing a command " +
parsedCommandStr.replace(",", ""),e);

} else {
logger.error(parsedCommandStr.replace(",", "") + " command exception failed");
throw new RuntimeException(parsedCommandStr.replace(",", "") + " command exception failed",e);

}
}
return outputStream.toString();

}

public void abortWorkItem(WorkItem workItem,
WorkItemManager manager) {
// Do nothing, this work item cannot be aborted
}

public void setDefaultTimeout(long defaultTimeout) {
this.defaultTimeout = defaultTimeout;
}

public String getParsedCommandStr() {
return parsedCommandStr;
}
Expand Down
Expand Up @@ -86,6 +86,34 @@ public void testExecCommandWithArguments() throws Exception {
assertTrue(result.contains("java version") || result.contains("jdk version"));
}

@Test(timeout = 6000)
public void testExecCommandWithTimeout() throws Exception {

TestWorkItemManager manager = new TestWorkItemManager();
WorkItemImpl workItem = new WorkItemImpl();
workItem.setParameter("Command",
"ping");
List<String> argumentList = new ArrayList<>();
argumentList.add("127.0.0.1");
workItem.setParameter("Arguments",
argumentList);
workItem.setParameter("TimeoutInMillis",
"PT5S");
ExecWorkItemHandler handler = new ExecWorkItemHandler();
handler.setLogThrownException(true);

handler.executeWorkItem(workItem,
manager);

assertNotNull(manager.getResults());
assertEquals(0,
manager.getResults().size());

assertEquals("[ping, 127.0.0.1]",
handler.getParsedCommandStr());

}

@Test
public void testExecCommandInvalidParam() throws Exception {
TestWorkItemManager manager = new TestWorkItemManager();
Expand Down