Skip to content

Commit

Permalink
Kill process when removing a workflow run with status "RUNNING"
Browse files Browse the repository at this point in the history
  • Loading branch information
lowery committed Nov 16, 2017
1 parent 0cdf866 commit ce37158
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
12 changes: 12 additions & 0 deletions app/dao/WorkflowDao.java
Expand Up @@ -21,6 +21,7 @@
import models.db.user.User;
import models.db.workflow.*;

import java.io.IOException;
import java.util.Date;
import java.util.List;

Expand Down Expand Up @@ -107,6 +108,17 @@ public void removeWorkflowRun(long workflowRunId) {
WorkflowRun run = WorkflowRun.find.byId(workflowRunId);

if (run != null) {
// if status is running then kill the process
try {
Runtime rt = Runtime.getRuntime();
if (System.getProperty("os.name").toLowerCase().indexOf("windows") > -1)
rt.exec("taskkill " + run.getPid());
else
rt.exec("kill -9 " + run.getPid());
} catch (IOException e) {
e.printStackTrace();
}

run.delete();

WorkflowResult result = run.getResult();
Expand Down
28 changes: 27 additions & 1 deletion app/util/WorkflowRunner.java
Expand Up @@ -19,11 +19,13 @@
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import models.db.workflow.Status;
import models.db.workflow.WorkflowRun;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.lang.reflect.Field;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
Expand All @@ -34,7 +36,7 @@ public class WorkflowRunner {
private static String JAVA_BIN = System.getProperty("java.home") + "/bin/java";
private static String KURATOR_JAR = System.getenv("KURATOR_JAR");

public RunResult run(RunOptions options) throws IOException, InterruptedException {
public RunResult run(RunOptions options, WorkflowRun run) throws IOException, InterruptedException {
if (KURATOR_JAR == null) {
// Try a JVM property
String prop = System.getProperty("kurator.jar");
Expand Down Expand Up @@ -82,6 +84,14 @@ public RunResult run(RunOptions options) throws IOException, InterruptedExceptio

// Start the workflow run as a process and get the input and output streams
Process process = builder.start();

// Store the process id to the database
long pid = getPid(process);

System.out.println("PID: " + pid);
run.setPid(pid);
run.save();

OutputStream stdin = process.getOutputStream();
InputStream stdout = process.getInputStream();

Expand Down Expand Up @@ -115,4 +125,20 @@ public RunResult run(RunOptions options) throws IOException, InterruptedExceptio
System.out.println("Workflow run status: " + result.getStatus());
return result;
}

public static synchronized long getPid(Process p) {
long pid = -1;

try {
if (p.getClass().getName().equals("java.lang.UNIXProcess")) {
Field f = p.getClass().getDeclaredField("pid");
f.setAccessible(true);
pid = f.getLong(p);
f.setAccessible(false);
}
} catch (Exception e) {
pid = -1;
}
return pid;
}
}

0 comments on commit ce37158

Please sign in to comment.