Search in sources :

Example 51 with TaskContext

use of org.ow2.proactive.scheduler.task.context.TaskContext in project scheduling by ow2-proactive.

the class InProcessTaskExecutor method execute.

/**
 * Executes a task.
 *
 * @param taskContext           Task context to execute
 * @param output                Standard output sink.
 * @param error                 Error sink.
 * @param scriptHandler
 * @param thirdPartyCredentials
 * @param variables             Environment variables.
 * @return The result of the executed script.
 * @throws Throwable
 */
private Serializable execute(TaskContext taskContext, PrintStream output, PrintStream error, ScriptHandler scriptHandler, Map<String, String> thirdPartyCredentials, VariablesMap variables) throws Throwable {
    if (taskContext.getPreScript() != null) {
        Script<?> script = taskContext.getPreScript();
        forkedTaskVariablesManager.replaceScriptParameters(script, thirdPartyCredentials, variables, error);
        ScriptResult preScriptResult = scriptHandler.handle(script, output, error);
        if (preScriptResult.errorOccured()) {
            throw new TaskException("Failed to execute pre script: " + preScriptResult.getException().getMessage(), preScriptResult.getException());
        }
    }
    Script<Serializable> script = ((ScriptExecutableContainer) taskContext.getExecutableContainer()).getScript();
    forkedTaskVariablesManager.replaceScriptParameters(script, thirdPartyCredentials, variables, error);
    ScriptResult<Serializable> scriptResult = scriptHandler.handle(script, output, error);
    if (scriptResult.errorOccured()) {
        throw new TaskException("Failed to execute task: " + scriptResult.getException().getMessage(), scriptResult.getException());
    }
    if (taskContext.getPostScript() != null) {
        forkedTaskVariablesManager.replaceScriptParameters(taskContext.getPostScript(), thirdPartyCredentials, variables, error);
        ScriptResult postScriptResult = scriptHandler.handle(taskContext.getPostScript(), output, error);
        if (postScriptResult.errorOccured()) {
            throw new TaskException("Failed to execute post script: " + postScriptResult.getException().getMessage(), postScriptResult.getException());
        }
    }
    return scriptResult.getResult();
}
Also used : ScriptResult(org.ow2.proactive.scripting.ScriptResult) Serializable(java.io.Serializable) TaskException(org.ow2.proactive.scheduler.task.exceptions.TaskException) ScriptExecutableContainer(org.ow2.proactive.scheduler.task.containers.ScriptExecutableContainer)

Example 52 with TaskContext

use of org.ow2.proactive.scheduler.task.context.TaskContext in project scheduling by ow2-proactive.

the class ExecuteForkedTaskInsideNewJvm method fromForkedJVM.

private void fromForkedJVM(String contextPath) {
    try {
        TaskContext container = deserializeContext(contextPath);
        TaskResultImpl result = new InProcessTaskExecutor().execute(container, System.out, System.err);
        serializeTaskResult(result, contextPath);
    } catch (Throwable throwable) {
        throwable.printStackTrace(System.err);
        try {
            serializeTaskResult(throwable, contextPath);
        } catch (Throwable couldNotSerializeException) {
            System.err.println("Could not serialize exception as task result:");
            couldNotSerializeException.printStackTrace(System.err);
        }
        System.exit(1);
    }
}
Also used : TaskContext(org.ow2.proactive.scheduler.task.context.TaskContext) TaskResultImpl(org.ow2.proactive.scheduler.task.TaskResultImpl) InProcessTaskExecutor(org.ow2.proactive.scheduler.task.executors.InProcessTaskExecutor)

Example 53 with TaskContext

use of org.ow2.proactive.scheduler.task.context.TaskContext 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 54 with TaskContext

use of org.ow2.proactive.scheduler.task.context.TaskContext in project scheduling by ow2-proactive.

the class ForkedTaskExecutorTest method failToFindJava.

@Test
public void failToFindJava() throws Throwable {
    System.setProperty("java.home", "does not exist");
    TestTaskOutput taskOutput = new TestTaskOutput();
    ForkedTaskExecutor taskExecutor = new ForkedTaskExecutor(tmpFolder.newFolder());
    TaskLauncherInitializer initializer = new TaskLauncherInitializer();
    initializer.setTaskId((TaskIdImpl.createTaskId(JobIdImpl.makeJobId("1000"), "job", 1000L)));
    TaskResultImpl result = taskExecutor.execute(new TaskContext(new ScriptExecutableContainer(new TaskScript(new SimpleScript("print('hello'); result='hello'", "javascript"))), initializer, null, new NodeDataSpacesURIs("", "", "", "", "", ""), "", ""), taskOutput.outputStream, taskOutput.error);
    assertNotNull(result.getException());
}
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) NodeDataSpacesURIs(org.ow2.proactive.scheduler.task.context.NodeDataSpacesURIs) TestTaskOutput(org.ow2.proactive.scheduler.task.TestTaskOutput) TaskLauncherInitializer(org.ow2.proactive.scheduler.task.TaskLauncherInitializer) Test(org.junit.Test)

Example 55 with TaskContext

use of org.ow2.proactive.scheduler.task.context.TaskContext in project scheduling by ow2-proactive.

the class ForkedTaskExecutorTest method runAsMe_userDoesNotExist.

@Test
public void runAsMe_userDoesNotExist() throws Throwable {
    TestTaskOutput taskOutput = new TestTaskOutput();
    Decrypter decrypter = createCredentials("somebody_that_does_not_exists");
    ForkedTaskExecutor taskExecutor = new ForkedTaskExecutor(tmpFolder.newFolder());
    TaskLauncherInitializer initializer = new TaskLauncherInitializer();
    initializer.setTaskId((TaskIdImpl.createTaskId(JobIdImpl.makeJobId("1000"), "job", 1000L)));
    ScriptExecutableContainer container = new ScriptExecutableContainer(new TaskScript(new SimpleScript("print('hello'); result='hello'", "javascript")));
    container.setRunAsUser(true);
    TaskContext taskContext = new TaskContext(container, initializer, null, new NodeDataSpacesURIs("", "", "", "", "", ""), "", "", decrypter);
    TaskResultImpl result = taskExecutor.execute(taskContext, taskOutput.outputStream, taskOutput.error);
    assertNotNull(result.getException());
}
Also used : TaskScript(org.ow2.proactive.scripting.TaskScript) TaskContext(org.ow2.proactive.scheduler.task.context.TaskContext) TaskResultImpl(org.ow2.proactive.scheduler.task.TaskResultImpl) ForkedTaskExecutor(org.ow2.proactive.scheduler.task.executors.ForkedTaskExecutor) SimpleScript(org.ow2.proactive.scripting.SimpleScript) Decrypter(org.ow2.proactive.scheduler.task.utils.Decrypter) ScriptExecutableContainer(org.ow2.proactive.scheduler.task.containers.ScriptExecutableContainer) NodeDataSpacesURIs(org.ow2.proactive.scheduler.task.context.NodeDataSpacesURIs) TestTaskOutput(org.ow2.proactive.scheduler.task.TestTaskOutput) TaskLauncherInitializer(org.ow2.proactive.scheduler.task.TaskLauncherInitializer) Test(org.junit.Test)

Aggregations

ScriptExecutableContainer (org.ow2.proactive.scheduler.task.containers.ScriptExecutableContainer)52 Test (org.junit.Test)51 TaskContext (org.ow2.proactive.scheduler.task.context.TaskContext)51 NodeDataSpacesURIs (org.ow2.proactive.scheduler.task.context.NodeDataSpacesURIs)43 SimpleScript (org.ow2.proactive.scripting.SimpleScript)40 TaskScript (org.ow2.proactive.scripting.TaskScript)40 TaskLauncherInitializer (org.ow2.proactive.scheduler.task.TaskLauncherInitializer)30 InProcessTaskExecutor (org.ow2.proactive.scheduler.task.executors.InProcessTaskExecutor)20 ForkEnvironment (org.ow2.proactive.scheduler.common.task.ForkEnvironment)17 Serializable (java.io.Serializable)16 TaskResultImpl (org.ow2.proactive.scheduler.task.TaskResultImpl)14 ScriptHandler (org.ow2.proactive.scripting.ScriptHandler)13 VariablesMap (org.ow2.proactive.scheduler.task.utils.VariablesMap)12 JobIdImpl (org.ow2.proactive.scheduler.job.JobIdImpl)10 HashMap (java.util.HashMap)9 TestTaskOutput (org.ow2.proactive.scheduler.task.TestTaskOutput)9 ForkedTaskExecutor (org.ow2.proactive.scheduler.task.executors.ForkedTaskExecutor)9 File (java.io.File)7 Decrypter (org.ow2.proactive.scheduler.task.utils.Decrypter)6 TaskResult (org.ow2.proactive.scheduler.common.task.TaskResult)5