Search in sources :

Example 1 with OSProcessBuilder

use of org.objectweb.proactive.extensions.processbuilder.OSProcessBuilder in project scheduling by ow2-proactive.

the class ForkedProcessBuilderCreatorTest method testEnvPrefixCommandIsAddedToCommandRunAsMeIsTrue.

@Test
public void testEnvPrefixCommandIsAddedToCommandRunAsMeIsTrue() throws Exception {
    ForkedProcessBuilderCreator forkedProcessBuilderCreator = new ForkedProcessBuilderCreator();
    setMocks(forkedProcessBuilderCreator);
    TaskContext taskContext = createTaskContext();
    taskContext.getExecutableContainer().setRunAsUser(true);
    OSProcessBuilder processBuilder = forkedProcessBuilderCreator.createForkedProcessBuilder(createTaskContext(), tmpFolder.newFolder(), System.out, System.out, tmpFolder.newFolder());
    assertThat(processBuilder.command(), hasItems(forkEnJavaCommandString));
}
Also used : OSProcessBuilder(org.objectweb.proactive.extensions.processbuilder.OSProcessBuilder) TaskContext(org.ow2.proactive.scheduler.task.context.TaskContext) Test(org.junit.Test)

Example 2 with OSProcessBuilder

use of org.objectweb.proactive.extensions.processbuilder.OSProcessBuilder 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();
        }
    }
}
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) CleanupTimeoutGetter(org.ow2.proactive.scheduler.task.utils.task.termination.CleanupTimeoutGetter) IOException(java.io.IOException) IOException(java.io.IOException) ForkedJvmProcessException(org.ow2.proactive.scheduler.task.exceptions.ForkedJvmProcessException) 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)

Example 3 with OSProcessBuilder

use of org.objectweb.proactive.extensions.processbuilder.OSProcessBuilder in project scheduling by ow2-proactive.

the class ForkedProcessBuilderCreatorTest method testForEnvPrefixCommandIsAddedToCommand.

@Test
public void testForEnvPrefixCommandIsAddedToCommand() throws Exception {
    ForkedProcessBuilderCreator forkedProcessBuilderCreator = new ForkedProcessBuilderCreator();
    setMocks(forkedProcessBuilderCreator);
    OSProcessBuilder processBuilder = forkedProcessBuilderCreator.createForkedProcessBuilder(createTaskContext(), tmpFolder.newFolder(), System.out, System.out, tmpFolder.newFolder());
    assertThat(processBuilder.command(), hasItems(forkEnJavaCommandString));
}
Also used : OSProcessBuilder(org.objectweb.proactive.extensions.processbuilder.OSProcessBuilder) Test(org.junit.Test)

Example 4 with OSProcessBuilder

use of org.objectweb.proactive.extensions.processbuilder.OSProcessBuilder in project scheduling by ow2-proactive.

the class ForkedProcessBuilderCreator method getOsProcessBuilder.

private OSProcessBuilder getOsProcessBuilder(TaskContext context, File workingDir, String nativeScriptPath) throws IOException, IllegalAccessException, KeyException {
    OSProcessBuilder processBuilder;
    if (context.isRunAsUser()) {
        addExecutionPermissionToScripts();
        forkerUtils.setSharedExecutablePermissions(workingDir);
        processBuilder = forkerUtils.getOSProcessBuilderFactory(nativeScriptPath).getBuilder(forkerUtils.checkConfigAndGetUser(context));
    } else {
        processBuilder = forkerUtils.getOSProcessBuilderFactory(nativeScriptPath).getBuilder();
    }
    return processBuilder;
}
Also used : OSProcessBuilder(org.objectweb.proactive.extensions.processbuilder.OSProcessBuilder)

Example 5 with OSProcessBuilder

use of org.objectweb.proactive.extensions.processbuilder.OSProcessBuilder in project scheduling by ow2-proactive.

the class ForkedProcessBuilderCreator method createForkedProcessBuilder.

/**
 * Creates a process builder for a given task context.
 *
 * @param context           The task context to execute.
 * @param serializedContext The task context saved to disk.
 * @param outputSink        Standard output sink.
 * @param errorSink         Error sink.
 * @param workingDir        The working directory to execute the process in.
 * @return Returns a process builder, ready to execute.
 * @throws Exception
 */
public OSProcessBuilder createForkedProcessBuilder(TaskContext context, File serializedContext, PrintStream outputSink, PrintStream errorSink, File workingDir) throws Exception {
    String nativeScriptPath = context.getSchedulerHome();
    OSProcessBuilder processBuilder = getOsProcessBuilder(context, workingDir, nativeScriptPath);
    ScriptResult forkEnvironmentScriptResult = executeForkEnvironmentScriptAndExtractVariables(context, outputSink, errorSink, processBuilder);
    processBuilder.command().addAll(forkedJvmTaskExecutionCommandCreator.createForkedJvmTaskExecutionCommand(context, forkEnvironmentScriptResult, serializedContext.getAbsolutePath()));
    processBuilder = processBuilder.directory(workingDir);
    return processBuilder;
}
Also used : ScriptResult(org.ow2.proactive.scripting.ScriptResult) OSProcessBuilder(org.objectweb.proactive.extensions.processbuilder.OSProcessBuilder)

Aggregations

OSProcessBuilder (org.objectweb.proactive.extensions.processbuilder.OSProcessBuilder)5 Test (org.junit.Test)2 TaskContext (org.ow2.proactive.scheduler.task.context.TaskContext)2 File (java.io.File)1 IOException (java.io.IOException)1 TaskId (org.ow2.proactive.scheduler.common.task.TaskId)1 TaskResultImpl (org.ow2.proactive.scheduler.task.TaskResultImpl)1 ForkedJvmProcessException (org.ow2.proactive.scheduler.task.exceptions.ForkedJvmProcessException)1 ProcessStreamsReader (org.ow2.proactive.scheduler.task.utils.ProcessStreamsReader)1 CleanupTimeoutGetter (org.ow2.proactive.scheduler.task.utils.task.termination.CleanupTimeoutGetter)1 ScriptResult (org.ow2.proactive.scripting.ScriptResult)1 CookieBasedProcessTreeKiller (org.ow2.proactive.utils.CookieBasedProcessTreeKiller)1