Search in sources :

Example 11 with TaskContextVariableExtractor

use of org.ow2.proactive.scheduler.task.context.TaskContextVariableExtractor 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)

Aggregations

Test (org.junit.Test)9 Serializable (java.io.Serializable)8 ScriptExecutableContainer (org.ow2.proactive.scheduler.task.containers.ScriptExecutableContainer)8 SimpleScript (org.ow2.proactive.scripting.SimpleScript)8 TaskScript (org.ow2.proactive.scripting.TaskScript)8 TaskLauncherInitializer (org.ow2.proactive.scheduler.task.TaskLauncherInitializer)7 HashMap (java.util.HashMap)3 TaskResultImpl (org.ow2.proactive.scheduler.task.TaskResultImpl)3 TaskResult (org.ow2.proactive.scheduler.common.task.TaskResult)2 TaskContext (org.ow2.proactive.scheduler.task.context.TaskContext)2 TaskContextVariableExtractor (org.ow2.proactive.scheduler.task.context.TaskContextVariableExtractor)2 PrintStream (java.io.PrintStream)1 ArrayList (java.util.ArrayList)1 ForkEnvironment (org.ow2.proactive.scheduler.common.task.ForkEnvironment)1 ForkedJvmTaskExecutionCommandCreator (org.ow2.proactive.scheduler.task.executors.forked.env.ForkedJvmTaskExecutionCommandCreator)1 ForkEnvironmentScriptResult (org.ow2.proactive.scripting.ForkEnvironmentScriptResult)1 ScriptResult (org.ow2.proactive.scripting.ScriptResult)1