use of org.ow2.proactive.utils.CookieBasedProcessTreeKiller in project scheduling by ow2-proactive.
the class ForkedTaskExecutor method execute.
@Override
public TaskResultImpl execute(TaskContext context, PrintStream outputSink, PrintStream errorSink) {
CookieBasedProcessTreeKiller taskProcessTreeKiller = null;
Process process = null;
ProcessStreamsReader processStreamsReader = null;
File serializedContext = null;
try {
if (!workingDir.exists()) {
FileUtils.forceMkdir(workingDir);
}
serializedContext = taskContextSerializer.serializeContext(context, workingDir);
OSProcessBuilder processBuilder = forkedJvmProcessBuilderCreator.createForkedProcessBuilder(context, serializedContext, outputSink, errorSink, workingDir);
TaskId taskId = context.getTaskId();
if (context.getInitializer().getGenericInformation() == null || !"true".equalsIgnoreCase(context.getInitializer().getGenericInformation().get(SchedulerConstants.DISABLE_PROCESS_TREE_KILLER_GENERIC_INFO))) {
String cookieNameSuffix = "Job" + taskId.getJobId().value() + "Task" + taskId.value();
taskProcessTreeKiller = CookieBasedProcessTreeKiller.createProcessChildrenKiller(cookieNameSuffix, processBuilder.environment());
}
process = processBuilder.start();
processStreamsReader = new ProcessStreamsReader(taskId.toString(), process, outputSink, errorSink);
int exitCode = process.waitFor();
if (exitCode != 0) {
try {
Object error = deserializeTaskResult(serializedContext);
if (error instanceof TaskContext) {
return createTaskResult(context, new IOException("Forked JVM process returned with exit code " + exitCode + ", see task logs for more information"));
} else {
Throwable exception = (Throwable) error;
return createTaskResult(context, exception);
}
} catch (Throwable cannotDeserializeResult) {
return createTaskResult(context, cannotDeserializeResult);
}
}
return (TaskResultImpl) deserializeTaskResult(serializedContext);
} catch (Throwable throwable) {
return createTaskResult(context, throwable);
} finally {
FileUtils.deleteQuietly(serializedContext);
if (process != null) {
logger.info("killing forked JVM process");
process.destroy();
try {
// wait for twice the time of the cleanup process
process.waitFor((new CleanupTimeoutGetter()).getCleanupTimeSeconds(), TimeUnit.SECONDS);
logger.info("JVM process killed");
} catch (Exception e) {
logger.info("Exception while waiting task to finish " + e);
}
}
if (taskProcessTreeKiller != null) {
taskProcessTreeKiller.kill();
}
if (processStreamsReader != null) {
processStreamsReader.close();
}
}
}
use of org.ow2.proactive.utils.CookieBasedProcessTreeKiller in project scheduling by ow2-proactive.
the class JavaSpawnExecutable method execute.
@Override
public Serializable execute(TaskResult... results) throws Throwable {
CookieBasedProcessTreeKiller processTreeKiller = null;
Process process = null;
org.apache.log4j.Logger.getLogger(ProcessTree.class).setLevel(Level.DEBUG);
try {
ProcessBuilder builder = new ProcessBuilder(getNativeExecLauncher(false));
builder.directory(getExecutablePath(launchersDir).getParentFile().getCanonicalFile());
Map<String, String> environment = builder.environment();
String cookieSuffix = "JavaExecutable_Job" + getVariables().get(SchedulerVars.PA_JOB_ID) + "Task" + getVariables().get(SchedulerVars.PA_TASK_ID);
// In non-fork mode the child process must be managed by a process tree killer
if (!forkMode) {
processTreeKiller = CookieBasedProcessTreeKiller.createProcessChildrenKiller(cookieSuffix, environment);
}
process = builder.start();
// redirect streams
redirectStream(process.getInputStream(), getOut());
redirectStream(process.getErrorStream(), getErr());
process.waitFor();
// we sleep 2 sec
Thread.sleep(sleep * 1000);
} catch (InterruptedException e) {
getOut().println("Process Interrupted");
} finally {
if (processTreeKiller != null) {
processTreeKiller.kill();
}
}
return true;
}
Aggregations