Search in sources :

Example 1 with ProcessStreamsReader

use of org.ow2.proactive.scheduler.task.utils.ProcessStreamsReader 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();
        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) {
            process.destroy();
        }
        if (taskProcessTreeKiller != null) {
            taskProcessTreeKiller.kill();
        }
        if (processStreamsReader != null) {
            processStreamsReader.close();
        }
    }
}
Also used : TaskId(org.ow2.proactive.scheduler.common.task.TaskId) TaskContext(org.ow2.proactive.scheduler.task.context.TaskContext) TaskResultImpl(org.ow2.proactive.scheduler.task.TaskResultImpl) IOException(java.io.IOException) OSProcessBuilder(org.objectweb.proactive.extensions.processbuilder.OSProcessBuilder) ProcessStreamsReader(org.ow2.proactive.scheduler.task.utils.ProcessStreamsReader) CookieBasedProcessTreeKiller(org.ow2.proactive.utils.CookieBasedProcessTreeKiller) File(java.io.File)

Aggregations

File (java.io.File)1 IOException (java.io.IOException)1 OSProcessBuilder (org.objectweb.proactive.extensions.processbuilder.OSProcessBuilder)1 TaskId (org.ow2.proactive.scheduler.common.task.TaskId)1 TaskResultImpl (org.ow2.proactive.scheduler.task.TaskResultImpl)1 TaskContext (org.ow2.proactive.scheduler.task.context.TaskContext)1 ProcessStreamsReader (org.ow2.proactive.scheduler.task.utils.ProcessStreamsReader)1 CookieBasedProcessTreeKiller (org.ow2.proactive.utils.CookieBasedProcessTreeKiller)1