Search in sources :

Example 56 with ScriptExecutableContainer

use of org.ow2.proactive.scheduler.task.containers.ScriptExecutableContainer in project scheduling by ow2-proactive.

the class TaskContextVariableExtractorTest method testExtractTaskResultVariablesGetScopeVariables.

@Test
public void testExtractTaskResultVariablesGetScopeVariables() throws Exception {
    ScriptExecutableContainer scriptContainer = new ScriptExecutableContainer(new TaskScript(new SimpleScript("print('hello'); result='hello'", "javascript")));
    TaskLauncherInitializer taskLauncherInitializer = getTaskLauncherInitializerWithWorkflowVariables();
    TaskContext taskContext = new TaskContext(scriptContainer, taskLauncherInitializer, null, new NodeDataSpacesURIs(null, null, null, null, null, null), null, new NodeInfo(null, null, null, null));
    Map<String, Serializable> scopeVariables = new TaskContextVariableExtractor().getScopeVariables(taskContext);
    Assert.assertThat(scopeVariables.toString(), CoreMatchers.containsString("{TestVariable2=valueForTest2}"));
}
Also used : Serializable(java.io.Serializable) TaskScript(org.ow2.proactive.scripting.TaskScript) SimpleScript(org.ow2.proactive.scripting.SimpleScript) ScriptExecutableContainer(org.ow2.proactive.scheduler.task.containers.ScriptExecutableContainer) TaskLauncherInitializer(org.ow2.proactive.scheduler.task.TaskLauncherInitializer) Test(org.junit.Test)

Example 57 with ScriptExecutableContainer

use of org.ow2.proactive.scheduler.task.containers.ScriptExecutableContainer in project scheduling by ow2-proactive.

the class TaskContextVariableExtractorTest method testExtractTaskLauncherInitializerVariablesFromTaskContext.

@Test
public void testExtractTaskLauncherInitializerVariablesFromTaskContext() throws Exception {
    ScriptExecutableContainer scriptContainer = new ScriptExecutableContainer(new TaskScript(new SimpleScript("print('hello'); result='hello'", "javascript")));
    TaskContext taskContext = new TaskContext(scriptContainer, createTaskLauncherInitializer(), null, new NodeDataSpacesURIs(null, null, null, null, null, null), null, new NodeInfo(null, null, null, null));
    Map<String, Serializable> contextVariables = new TaskContextVariableExtractor().getAllVariables(taskContext);
    validateExtractedVariablesFromTaskLauncherInitializer(contextVariables);
}
Also used : Serializable(java.io.Serializable) TaskScript(org.ow2.proactive.scripting.TaskScript) SimpleScript(org.ow2.proactive.scripting.SimpleScript) ScriptExecutableContainer(org.ow2.proactive.scheduler.task.containers.ScriptExecutableContainer) Test(org.junit.Test)

Example 58 with ScriptExecutableContainer

use of org.ow2.proactive.scheduler.task.containers.ScriptExecutableContainer in project scheduling by ow2-proactive.

the class TaskContextVariableExtractorTest method testExtractTaskResultVariablesFromPreviousTaskResultsInsideTheTaskContext.

@Test
public void testExtractTaskResultVariablesFromPreviousTaskResultsInsideTheTaskContext() throws Exception {
    ScriptExecutableContainer scriptContainer = new ScriptExecutableContainer(new TaskScript(new SimpleScript("print('hello'); result='hello'", "javascript")));
    TaskLauncherInitializer taskLauncherInitializer = getTaskLauncherInitializerWithWorkflowVariables();
    Map<String, byte[]> taskResultVariables = new HashMap<>();
    // The task result variables are expected to be converted to byte streams.
    taskResultVariables.put(taskResultPropagatedVariables1Key, AllObjects2BytesConverterHandler.convertObject2Byte(taskResultPropagatedVariables1Key, taskResultPropagatedVariables1Value));
    taskResultVariables.put(SchedulerVars.PA_TASK_ID.name(), AllObjects2BytesConverterHandler.convertObject2Byte(SchedulerVars.PA_TASK_ID.name(), "" + previousTaskIdValue));
    TaskResultImpl taskResult = new TaskResultImpl(taskLauncherInitializer.getTaskId(), new Exception("Exception"));
    taskResult.setPropagatedVariables(taskResultVariables);
    TaskResult[] taskResultArray = { taskResult };
    TaskContext taskContext = new TaskContext(scriptContainer, taskLauncherInitializer, taskResultArray, new NodeDataSpacesURIs(null, null, null, null, null, null), null, new NodeInfo(null, null, null, null));
    Map<String, Serializable> contextVariables = new TaskContextVariableExtractor().getAllVariables(taskContext);
    assertThat((String) contextVariables.get(taskResultPropagatedVariables1Key), is(taskResultPropagatedVariables1Value));
    // Makes sure the previous task id has been overwritten by the current task id
    assertThat((String) contextVariables.get(SchedulerVars.PA_TASK_ID.name()), is("" + taskIdValue));
    contextVariables = new TaskContextVariableExtractor().getAllNonTaskVariables(taskContext);
    assertThat((String) contextVariables.get(taskResultPropagatedVariables1Key), is(taskResultPropagatedVariables1Value));
    // Makes sure the previous task id has been overwritten by the current task id
    assertThat((String) contextVariables.get(SchedulerVars.PA_TASK_ID.name()), is("" + taskIdValue));
}
Also used : Serializable(java.io.Serializable) TaskScript(org.ow2.proactive.scripting.TaskScript) TaskResultImpl(org.ow2.proactive.scheduler.task.TaskResultImpl) HashMap(java.util.HashMap) SimpleScript(org.ow2.proactive.scripting.SimpleScript) ScriptExecutableContainer(org.ow2.proactive.scheduler.task.containers.ScriptExecutableContainer) TaskLauncherInitializer(org.ow2.proactive.scheduler.task.TaskLauncherInitializer) TaskResult(org.ow2.proactive.scheduler.common.task.TaskResult) Test(org.junit.Test)

Example 59 with ScriptExecutableContainer

use of org.ow2.proactive.scheduler.task.containers.ScriptExecutableContainer in project scheduling by ow2-proactive.

the class ForkedProcessBuilderCreatorTest method createTaskContext.

private TaskContext createTaskContext() throws InvalidScriptException, NodeException {
    ScriptExecutableContainer scriptContainer = new ScriptExecutableContainer(new TaskScript(new SimpleScript("print('hello'); result='hello'", "javascript")));
    TaskLauncherInitializer taskLauncherInitializer = getTaskLauncherInitializerWithWorkflowVariableAndForkEnvironment();
    TaskContext taskContext = new TaskContext(scriptContainer, taskLauncherInitializer, null, new NodeDataSpacesURIs(null, null, null, null, null, null), null, new NodeInfo(null, null, null, null));
    return taskContext;
}
Also used : TaskScript(org.ow2.proactive.scripting.TaskScript) TaskContext(org.ow2.proactive.scheduler.task.context.TaskContext) NodeInfo(org.ow2.proactive.scheduler.task.context.NodeInfo) SimpleScript(org.ow2.proactive.scripting.SimpleScript) ScriptExecutableContainer(org.ow2.proactive.scheduler.task.containers.ScriptExecutableContainer) NodeDataSpacesURIs(org.ow2.proactive.scheduler.task.context.NodeDataSpacesURIs) TaskLauncherInitializer(org.ow2.proactive.scheduler.task.TaskLauncherInitializer)

Example 60 with ScriptExecutableContainer

use of org.ow2.proactive.scheduler.task.containers.ScriptExecutableContainer 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);
        Map<String, String> genericInfo = taskContext.getInitializer().getGenericInformation();
        if (genericInfo != null && genericInfo.containsKey("PRE_SCRIPT_AS_FILE")) {
            String path = genericInfo.get("PRE_SCRIPT_AS_FILE");
            saveScriptAsFile(path, script, taskContext);
        } else {
            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);
        try {
            scriptHandler.addBinding(TaskScript.RESULT_VARIABLE, scriptResult.getResult());
        } catch (Throwable throwable) {
            scriptHandler.addBinding(TaskScript.RESULT_VARIABLE, throwable);
        }
        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)

Aggregations

ScriptExecutableContainer (org.ow2.proactive.scheduler.task.containers.ScriptExecutableContainer)84 Test (org.junit.Test)71 SimpleScript (org.ow2.proactive.scripting.SimpleScript)67 TaskScript (org.ow2.proactive.scripting.TaskScript)67 NodeDataSpacesURIs (org.ow2.proactive.scheduler.task.context.NodeDataSpacesURIs)46 NodeInfo (org.ow2.proactive.scheduler.task.context.NodeInfo)46 TaskContext (org.ow2.proactive.scheduler.task.context.TaskContext)46 TaskLauncherInitializer (org.ow2.proactive.scheduler.task.TaskLauncherInitializer)29 InProcessTaskExecutor (org.ow2.proactive.scheduler.task.executors.InProcessTaskExecutor)22 TaskResult (org.ow2.proactive.scheduler.common.task.TaskResult)18 ForkEnvironment (org.ow2.proactive.scheduler.common.task.ForkEnvironment)17 File (java.io.File)12 Serializable (java.io.Serializable)11 JobIdImpl (org.ow2.proactive.scheduler.job.JobIdImpl)11 TaskResultImpl (org.ow2.proactive.scheduler.task.TaskResultImpl)10 TestTaskOutput (org.ow2.proactive.scheduler.task.TestTaskOutput)9 ForkedTaskExecutor (org.ow2.proactive.scheduler.task.executors.ForkedTaskExecutor)9 Decrypter (org.ow2.proactive.scheduler.task.utils.Decrypter)9 HashMap (java.util.HashMap)8 VariablesMap (org.ow2.proactive.scheduler.task.utils.VariablesMap)8