Permalink
Browse files

Kill process when removing a workflow run with status "RUNNING"

  • Loading branch information...
lowery committed Nov 16, 2017
1 parent 0cdf866 commit ce371582dee8b947491aa11bd3f19b16d933f1ac
Showing with 39 additions and 1 deletion.
  1. +12 −0 app/dao/WorkflowDao.java
  2. +27 −1 app/util/WorkflowRunner.java
@@ -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;

@@ -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();
@@ -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.*;
@@ -34,7 +36,7 @@
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");
@@ -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();

@@ -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.