Search in sources :

Example 26 with VariablesMap

use of org.ow2.proactive.scheduler.task.utils.VariablesMap in project scheduling by ow2-proactive.

the class ForkedTaskVariablesManagerTest method initializeForkedTaskVariableManager.

private Map<String, Object> initializeForkedTaskVariableManager(ScriptHandler scriptHandler, TaskContext taskContext, VariablesMap variables, Map<String, String> credentials, Map<String, String> resultMetadata) throws IllegalAccessException, NoSuchFieldException {
    // Create class
    ForkedTaskVariablesManager forkedTaskVariablesManager = new ForkedTaskVariablesManager();
    // Replace additionalBindings to hold a reference to it
    Map<String, Object> scriptHandlerBindings = new HashMap<>();
    setPrivateField(ScriptHandler.class.getDeclaredField("additionalBindings"), scriptHandler, scriptHandlerBindings);
    SchedulerNodeClient schedulerNodeClient = forkedTaskVariablesManager.createSchedulerNodeClient(taskContext);
    // Execute method which adds bindings
    forkedTaskVariablesManager.addBindingsToScriptHandler(scriptHandler, taskContext, variables, credentials, schedulerNodeClient, forkedTaskVariablesManager.createDataSpaceNodeClient(taskContext, schedulerNodeClient, IDataSpaceClient.Dataspace.USER), forkedTaskVariablesManager.createDataSpaceNodeClient(taskContext, schedulerNodeClient, IDataSpaceClient.Dataspace.GLOBAL), resultMetadata);
    return scriptHandlerBindings;
}
Also used : HashMap(java.util.HashMap) SchedulerNodeClient(org.ow2.proactive.scheduler.task.client.SchedulerNodeClient) ScriptHandler(org.ow2.proactive.scripting.ScriptHandler)

Example 27 with VariablesMap

use of org.ow2.proactive.scheduler.task.utils.VariablesMap in project scheduling by ow2-proactive.

the class ForkedTaskVariablesManagerTest method testAddBindingsToScriptHandlerContainsForkEnvironment.

@Test
public void testAddBindingsToScriptHandlerContainsForkEnvironment() throws InvalidScriptException, NodeException, NoSuchFieldException, IllegalAccessException {
    TaskContext taskContext = createTaskContext(null);
    ForkEnvironment forkEnvironment = new ForkEnvironment();
    taskContext.getInitializer().setForkEnvironment(forkEnvironment);
    // Expect taskResultArray to be inside the map
    validateThatScriptHandlerBindingsContain(new ScriptHandler(), taskContext, new VariablesMap(), new HashMap<String, String>(), new HashMap<String, String>(), SchedulerConstants.FORK_ENVIRONMENT_BINDING_NAME, forkEnvironment);
}
Also used : TaskContext(org.ow2.proactive.scheduler.task.context.TaskContext) VariablesMap(org.ow2.proactive.scheduler.task.utils.VariablesMap) ForkEnvironment(org.ow2.proactive.scheduler.common.task.ForkEnvironment) ScriptHandler(org.ow2.proactive.scripting.ScriptHandler) Test(org.junit.Test)

Example 28 with VariablesMap

use of org.ow2.proactive.scheduler.task.utils.VariablesMap in project scheduling by ow2-proactive.

the class ForkedTaskVariablesManagerTest method testAddBindingsToScriptHandlerContainsUserAndGlobalSpaceApiVariable.

@Test
public void testAddBindingsToScriptHandlerContainsUserAndGlobalSpaceApiVariable() throws InvalidScriptException, NodeException, NoSuchFieldException, IllegalAccessException, KeyException, NoSuchAlgorithmException {
    ScriptExecutableContainer scriptContainer = createScriptContainer();
    TaskLauncherInitializer taskLauncherInitializer = new TaskLauncherInitializer();
    taskLauncherInitializer.setForkEnvironment(new ForkEnvironment());
    taskLauncherInitializer.setSchedulerRestUrl("http://localhost:8080/rest");
    Decrypter decrypter = createCredentials(testUser, testPass);
    TaskContext taskContext = new TaskContext(scriptContainer, taskLauncherInitializer, null, new NodeDataSpacesURIs(null, null, null, null, null, null), null, null, decrypter);
    // variable should belong to the expected class
    validateThatScriptHandlerBindingsInstanceOf(new ScriptHandler(), taskContext, new VariablesMap(), new HashMap<String, String>(), new HashMap<String, String>(), SchedulerConstants.DS_USER_API_BINDING_NAME, DataSpaceNodeClient.class);
    // variable should belong to the expected class
    validateThatScriptHandlerBindingsInstanceOf(new ScriptHandler(), taskContext, new VariablesMap(), new HashMap<String, String>(), new HashMap<String, String>(), SchedulerConstants.DS_GLOBAL_API_BINDING_NAME, DataSpaceNodeClient.class);
}
Also used : TaskContext(org.ow2.proactive.scheduler.task.context.TaskContext) ScriptExecutableContainer(org.ow2.proactive.scheduler.task.containers.ScriptExecutableContainer) Decrypter(org.ow2.proactive.scheduler.task.utils.Decrypter) VariablesMap(org.ow2.proactive.scheduler.task.utils.VariablesMap) ForkEnvironment(org.ow2.proactive.scheduler.common.task.ForkEnvironment) NodeDataSpacesURIs(org.ow2.proactive.scheduler.task.context.NodeDataSpacesURIs) TaskLauncherInitializer(org.ow2.proactive.scheduler.task.TaskLauncherInitializer) ScriptHandler(org.ow2.proactive.scripting.ScriptHandler) Test(org.junit.Test)

Example 29 with VariablesMap

use of org.ow2.proactive.scheduler.task.utils.VariablesMap 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 30 with VariablesMap

use of org.ow2.proactive.scheduler.task.utils.VariablesMap in project scheduling by ow2-proactive.

the class JavaClassScriptEngine method eval.

@Override
public Object eval(String userExecutableClassName, ScriptContext context) throws ScriptException {
    try {
        JavaExecutable javaExecutable = getExecutable(userExecutableClassName);
        JavaStandaloneExecutableInitializer execInitializer = new JavaStandaloneExecutableInitializer();
        PrintStream output = new PrintStream(new WriterOutputStream(context.getWriter()), true);
        execInitializer.setOutputSink(output);
        PrintStream error = new PrintStream(new WriterOutputStream(context.getErrorWriter()), true);
        execInitializer.setErrorSink(error);
        Map<String, byte[]> propagatedVariables = null;
        if (context.getAttribute(SchedulerConstants.VARIABLES_BINDING_NAME) != null) {
            propagatedVariables = SerializationUtil.serializeVariableMap(((VariablesMap) context.getAttribute(SchedulerConstants.VARIABLES_BINDING_NAME)).getPropagatedVariables());
            execInitializer.setPropagatedVariables(propagatedVariables);
        } else {
            execInitializer.setPropagatedVariables(Collections.<String, byte[]>emptyMap());
        }
        if (context.getAttribute(Script.ARGUMENTS_NAME) != null) {
            execInitializer.setSerializedArguments((Map<String, byte[]>) ((Serializable[]) context.getAttribute(Script.ARGUMENTS_NAME))[0]);
        } else {
            execInitializer.setSerializedArguments(Collections.<String, byte[]>emptyMap());
        }
        if (context.getAttribute(SchedulerConstants.CREDENTIALS_VARIABLE) != null) {
            execInitializer.setThirdPartyCredentials((Map<String, String>) context.getAttribute(SchedulerConstants.CREDENTIALS_VARIABLE));
        } else {
            execInitializer.setThirdPartyCredentials(Collections.<String, String>emptyMap());
        }
        if (context.getAttribute(SchedulerConstants.MULTI_NODE_TASK_NODESURL_BINDING_NAME) != null) {
            List<String> nodesURLs = (List<String>) context.getAttribute(SchedulerConstants.MULTI_NODE_TASK_NODESURL_BINDING_NAME);
            execInitializer.setNodesURL(nodesURLs);
        } else {
            execInitializer.setNodesURL(Collections.<String>emptyList());
        }
        javaExecutable.internalInit(execInitializer, context);
        Serializable execute = javaExecutable.execute((TaskResult[]) context.getAttribute(SchedulerConstants.RESULTS_VARIABLE));
        if (propagatedVariables != null) {
            ((Map<String, Serializable>) context.getAttribute(SchedulerConstants.VARIABLES_BINDING_NAME)).putAll(javaExecutable.getVariables());
        }
        output.close();
        error.close();
        return execute;
    } catch (Throwable e) {
        throw new ScriptException(new TaskException(getStackTraceAsString(e), e));
    }
}
Also used : PrintStream(java.io.PrintStream) Serializable(java.io.Serializable) Throwables.getStackTraceAsString(com.google.common.base.Throwables.getStackTraceAsString) WriterOutputStream(org.apache.commons.io.output.WriterOutputStream) JavaExecutable(org.ow2.proactive.scheduler.common.task.executable.JavaExecutable) ScriptException(javax.script.ScriptException) TaskException(org.ow2.proactive.scheduler.task.exceptions.TaskException) TaskResult(org.ow2.proactive.scheduler.common.task.TaskResult) VariablesMap(org.ow2.proactive.scheduler.task.utils.VariablesMap) List(java.util.List) JavaStandaloneExecutableInitializer(org.ow2.proactive.scheduler.common.task.executable.internal.JavaStandaloneExecutableInitializer) Map(java.util.Map) VariablesMap(org.ow2.proactive.scheduler.task.utils.VariablesMap)

Aggregations

VariablesMap (org.ow2.proactive.scheduler.task.utils.VariablesMap)22 Test (org.junit.Test)20 ScriptHandler (org.ow2.proactive.scripting.ScriptHandler)17 HashMap (java.util.HashMap)13 TaskContext (org.ow2.proactive.scheduler.task.context.TaskContext)10 ForkEnvironment (org.ow2.proactive.scheduler.common.task.ForkEnvironment)9 ScriptExecutableContainer (org.ow2.proactive.scheduler.task.containers.ScriptExecutableContainer)9 TaskLauncherInitializer (org.ow2.proactive.scheduler.task.TaskLauncherInitializer)8 NodeDataSpacesURIs (org.ow2.proactive.scheduler.task.context.NodeDataSpacesURIs)8 Serializable (java.io.Serializable)7 TaskFlowJob (org.ow2.proactive.scheduler.common.job.TaskFlowJob)4 SchedulerNodeClient (org.ow2.proactive.scheduler.task.client.SchedulerNodeClient)4 LinkedHashMap (java.util.LinkedHashMap)3 Map (java.util.Map)3 JobVariable (org.ow2.proactive.scheduler.common.job.JobVariable)3 RemoteSpace (org.ow2.proactive.scheduler.common.task.dataspaces.RemoteSpace)3 TaskResultImpl (org.ow2.proactive.scheduler.task.TaskResultImpl)3 File (java.io.File)2 FileNotFoundException (java.io.FileNotFoundException)2 LoginException (javax.security.auth.login.LoginException)2