Search in sources :

Example 41 with ForkEnvironment

use of org.ow2.proactive.scheduler.common.task.ForkEnvironment in project scheduling by ow2-proactive.

the class TaskLauncherTest method nativeTask_WorkingDir_WithVariableReplacement.

@Test
public void nativeTask_WorkingDir_WithVariableReplacement() throws Throwable {
    String tempFolder = tmpFolder.newFolder().getCanonicalPath();
    ScriptExecutableContainer executableContainer = new ScriptExecutableContainer(new TaskScript(new SimpleScript(pwdCommand(), "native")));
    TaskLauncherInitializer initializer = new TaskLauncherInitializer();
    initializer.setJobVariables(singletonMap("folder", new JobVariable("folder", tempFolder)));
    initializer.setForkEnvironment(new ForkEnvironment("$folder"));
    initializer.setTaskId(TaskIdImpl.createTaskId(JobIdImpl.makeJobId("1000"), "job", 1000L));
    TaskResult taskResult = runTaskLauncher(createLauncherWithInjectedMocks(initializer, new TestTaskLauncherFactory()), executableContainer);
    assertThat(taskResult.getOutput().getAllLogs(false).contains(String.format("%s%n", tempFolder)), is(true));
}
Also used : TaskScript(org.ow2.proactive.scripting.TaskScript) SimpleScript(org.ow2.proactive.scripting.SimpleScript) ScriptExecutableContainer(org.ow2.proactive.scheduler.task.containers.ScriptExecutableContainer) TaskResult(org.ow2.proactive.scheduler.common.task.TaskResult) JobVariable(org.ow2.proactive.scheduler.common.job.JobVariable) ForkEnvironment(org.ow2.proactive.scheduler.common.task.ForkEnvironment) Test(org.junit.Test)

Example 42 with ForkEnvironment

use of org.ow2.proactive.scheduler.common.task.ForkEnvironment in project scheduling by ow2-proactive.

the class TaskLauncherTest method nativeTask_WorkingDir.

@Test
public void nativeTask_WorkingDir() throws Throwable {
    String tempFolder = tmpFolder.newFolder().getCanonicalPath();
    ScriptExecutableContainer executableContainer = new ScriptExecutableContainer(new TaskScript(new SimpleScript(pwdCommand(), "native")));
    TaskLauncherInitializer initializer = new TaskLauncherInitializer();
    initializer.setForkEnvironment(new ForkEnvironment(tempFolder));
    initializer.setTaskId(TaskIdImpl.createTaskId(JobIdImpl.makeJobId("1000"), "job", 1000L));
    TaskResult taskResult = runTaskLauncher(createLauncherWithInjectedMocks(initializer, new TestTaskLauncherFactory()), executableContainer);
    assertThat(taskResult.getOutput().getAllLogs(false).contains(String.format("%s%n", tempFolder)), is(true));
}
Also used : TaskScript(org.ow2.proactive.scripting.TaskScript) SimpleScript(org.ow2.proactive.scripting.SimpleScript) ScriptExecutableContainer(org.ow2.proactive.scheduler.task.containers.ScriptExecutableContainer) TaskResult(org.ow2.proactive.scheduler.common.task.TaskResult) ForkEnvironment(org.ow2.proactive.scheduler.common.task.ForkEnvironment) Test(org.junit.Test)

Example 43 with ForkEnvironment

use of org.ow2.proactive.scheduler.common.task.ForkEnvironment in project scheduling by ow2-proactive.

the class ForkedProcessBuilderCreatorTest method createForkEnvironment.

private ForkEnvironment createForkEnvironment() {
    ForkEnvironment forkEnv = new ForkEnvironment();
    forkEnv.addJVMArgument(forkEnvJvmArguments[0]);
    forkEnv.addJVMArgument(forkEnvJvmArguments[1]);
    return forkEnv;
}
Also used : ForkEnvironment(org.ow2.proactive.scheduler.common.task.ForkEnvironment)

Example 44 with ForkEnvironment

use of org.ow2.proactive.scheduler.common.task.ForkEnvironment in project scheduling by ow2-proactive.

the class ForkedJvmTaskExecutionCommandCreator method createForkedJvmTaskExecutionCommand.

/**
 * Creates a command to start a task inside a java virtual machine.
 *
 * @param taskContext                   TaskContext object describing the task.
 * @param forkEnvironmentScriptResult   Result from a running fork environment script. If it is
 *                                      of instance {@link ForkEnvironmentScriptResult}, the script return
 *                                      variables will be used for the construction of the command.
 * @param serializedContextAbsolutePath The serialized TaskContext object which will be read by the virtual
 *                                      machine to run the task.
 * @return A List, empty if the TaskContext is null, otherwise filled with a command.
 * @throws Exception If the {@link TaskContextVariableExtractor} could not extract all variables from the
 *                   TaskContext.
 */
public List<String> createForkedJvmTaskExecutionCommand(TaskContext taskContext, ScriptResult forkEnvironmentScriptResult, String serializedContextAbsolutePath) throws Exception {
    if (taskContext == null) {
        return new ArrayList<>(0);
    }
    Map<String, Serializable> variables = taskContextVariableExtractor.getAllVariables(taskContext);
    String javaHome = System.getProperty("java.home");
    ArrayList<String> jvmArguments = new ArrayList<>(1);
    ForkEnvironment forkEnvironment = null;
    if (taskContext.getInitializer() != null) {
        forkEnvironment = taskContext.getInitializer().getForkEnvironment();
    }
    // set the task fork property so that script engines have a mean to know
    // if they are running in a forked task or not
    jvmArguments.add(PASchedulerProperties.TASK_FORK.getCmdLine() + "true");
    configureLogging(jvmArguments, variables);
    StringBuilder classpath = new StringBuilder("." + File.pathSeparatorChar);
    if (!System.getProperty("java.class.path", "").contains("node.jar")) {
        // in case the class path of the node is not built with the node.jar, we
        // build the classpath with wildcards to avoid command too long errors on windows
        classpath.append(getStandardClassPathEntries(variables));
    }
    for (String classpathEntry : OneJar.getClasspath()) {
        classpath.append(File.pathSeparatorChar).append(classpathEntry);
    }
    if (forkEnvironment != null) {
        for (String jvmArgument : forkEnvironment.getJVMArguments()) {
            jvmArguments.add(VariableSubstitutor.filterAndUpdate(jvmArgument, variables));
        }
        for (String classpathEntry : forkEnvironment.getAdditionalClasspath()) {
            classpath.append(File.pathSeparatorChar).append(VariableSubstitutor.filterAndUpdate(classpathEntry, variables));
        }
        if (!Strings.isNullOrEmpty(forkEnvironment.getJavaHome())) {
            javaHome = VariableSubstitutor.filterAndUpdate(forkEnvironment.getJavaHome(), variables);
        }
    }
    List<String> prefixes = javaPrefixCommandExtractor.extractJavaPrefixCommandToCommandListFromScriptResult(forkEnvironmentScriptResult);
    List<String> javaCommand = new ArrayList<>(prefixes.size() + 3 + jvmArguments.size() + 2);
    javaCommand.addAll(prefixes);
    javaCommand.add(javaHome + javaHomePostfixJavaExecutable);
    javaCommand.add("-cp");
    javaCommand.add(classpath.toString());
    javaCommand.addAll(jvmArguments);
    javaCommand.add(ExecuteForkedTaskInsideNewJvm.class.getName());
    javaCommand.add(serializedContextAbsolutePath);
    return javaCommand;
}
Also used : Serializable(java.io.Serializable) ArrayList(java.util.ArrayList) ForkEnvironment(org.ow2.proactive.scheduler.common.task.ForkEnvironment)

Example 45 with ForkEnvironment

use of org.ow2.proactive.scheduler.common.task.ForkEnvironment in project scheduling by ow2-proactive.

the class ForkedTaskExecutorTest method forkEnvironment_failingEnvScript.

@Test
public void forkEnvironment_failingEnvScript() throws Exception {
    TestTaskOutput taskOutput = new TestTaskOutput();
    File workingDir = tmpFolder.newFolder();
    ForkedTaskExecutor taskExecutor = new ForkedTaskExecutor(workingDir);
    TaskLauncherInitializer initializer = new TaskLauncherInitializer();
    initializer.setTaskId((TaskIdImpl.createTaskId(JobIdImpl.makeJobId("1000"), "job", 1000L)));
    ForkEnvironment forkEnvironment = new ForkEnvironment();
    forkEnvironment.setEnvScript(new SimpleScript("should fail execution", "groovy"));
    initializer.setForkEnvironment(forkEnvironment);
    TaskResultImpl taskResult = taskExecutor.execute(new TaskContext(new ScriptExecutableContainer(new TaskScript(new SimpleScript("", "groovy"))), initializer, null, new NodeDataSpacesURIs("", "", "", "", "", ""), "", ""), taskOutput.outputStream, taskOutput.error);
    assertTrue(taskResult.hadException());
}
Also used : TaskContext(org.ow2.proactive.scheduler.task.context.TaskContext) TaskScript(org.ow2.proactive.scripting.TaskScript) TaskResultImpl(org.ow2.proactive.scheduler.task.TaskResultImpl) ForkedTaskExecutor(org.ow2.proactive.scheduler.task.executors.ForkedTaskExecutor) SimpleScript(org.ow2.proactive.scripting.SimpleScript) ScriptExecutableContainer(org.ow2.proactive.scheduler.task.containers.ScriptExecutableContainer) ForkEnvironment(org.ow2.proactive.scheduler.common.task.ForkEnvironment) NodeDataSpacesURIs(org.ow2.proactive.scheduler.task.context.NodeDataSpacesURIs) TestTaskOutput(org.ow2.proactive.scheduler.task.TestTaskOutput) File(java.io.File) TaskLauncherInitializer(org.ow2.proactive.scheduler.task.TaskLauncherInitializer) Test(org.junit.Test)

Aggregations

ForkEnvironment (org.ow2.proactive.scheduler.common.task.ForkEnvironment)54 Test (org.junit.Test)27 TaskFlowJob (org.ow2.proactive.scheduler.common.job.TaskFlowJob)25 JavaTask (org.ow2.proactive.scheduler.common.task.JavaTask)22 ScriptExecutableContainer (org.ow2.proactive.scheduler.task.containers.ScriptExecutableContainer)17 TaskContext (org.ow2.proactive.scheduler.task.context.TaskContext)15 TaskLauncherInitializer (org.ow2.proactive.scheduler.task.TaskLauncherInitializer)14 SimpleScript (org.ow2.proactive.scripting.SimpleScript)14 NodeDataSpacesURIs (org.ow2.proactive.scheduler.task.context.NodeDataSpacesURIs)13 VariablesMap (org.ow2.proactive.scheduler.task.utils.VariablesMap)9 ScriptHandler (org.ow2.proactive.scripting.ScriptHandler)9 File (java.io.File)8 NativeTask (org.ow2.proactive.scheduler.common.task.NativeTask)8 TaskScript (org.ow2.proactive.scripting.TaskScript)8 JobId (org.ow2.proactive.scheduler.common.job.JobId)7 JobCreationException (org.ow2.proactive.scheduler.common.exception.JobCreationException)5 JobResult (org.ow2.proactive.scheduler.common.job.JobResult)5 InternalJob (org.ow2.proactive.scheduler.job.InternalJob)5 InternalTask (org.ow2.proactive.scheduler.task.internal.InternalTask)5 FileNotFoundException (java.io.FileNotFoundException)4