Search in sources :

Example 16 with ScriptHandler

use of org.ow2.proactive.scripting.ScriptHandler 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 17 with ScriptHandler

use of org.ow2.proactive.scripting.ScriptHandler 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 18 with ScriptHandler

use of org.ow2.proactive.scripting.ScriptHandler 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 19 with ScriptHandler

use of org.ow2.proactive.scripting.ScriptHandler 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 20 with ScriptHandler

use of org.ow2.proactive.scripting.ScriptHandler in project scheduling by ow2-proactive.

the class ForkEnvironmentScriptExecutor method executeForkEnvironmentScript.

/**
 * Executes a fork environment script.
 *
 * @param context    The task context to execute the script in.
 * @param outputSink Standard output sink.
 * @param errorSink  Error output sink.
 * @return Returns a ScriptResult.
 * @throws Exception
 */
public ScriptResult executeForkEnvironmentScript(TaskContext context, PrintStream outputSink, PrintStream errorSink) throws Exception {
    VariablesMap variables = new VariablesMap();
    variables.setInheritedMap(taskContextVariableExtractor.getAllNonTaskVariables(context));
    variables.setScopeMap(taskContextVariableExtractor.getScopeVariables(context));
    Map<String, String> thirdPartyCredentials = forkedTaskVariablesManager.extractThirdPartyCredentials(context);
    ScriptHandler scriptHandler = ScriptLoader.createLocalHandler();
    Script<?> script = context.getInitializer().getForkEnvironment().getEnvScript();
    SchedulerNodeClient schedulerNodeClient = forkedTaskVariablesManager.createSchedulerNodeClient(context);
    RemoteSpace userSpaceClient = forkedTaskVariablesManager.createDataSpaceNodeClient(context, schedulerNodeClient, IDataSpaceClient.Dataspace.USER);
    RemoteSpace globalSpaceClient = forkedTaskVariablesManager.createDataSpaceNodeClient(context, schedulerNodeClient, IDataSpaceClient.Dataspace.GLOBAL);
    forkedTaskVariablesManager.addBindingsToScriptHandler(scriptHandler, context, variables, thirdPartyCredentials, schedulerNodeClient, userSpaceClient, globalSpaceClient, Collections.<String, String>emptyMap());
    forkedTaskVariablesManager.replaceScriptParameters(script, thirdPartyCredentials, variables, errorSink);
    ScriptResult scriptResult = scriptHandler.handle(script, outputSink, errorSink);
    if (scriptResult.errorOccured()) {
        throw new Exception("Failed to execute fork environment script", scriptResult.getException());
    }
    return scriptResult;
}
Also used : ScriptResult(org.ow2.proactive.scripting.ScriptResult) RemoteSpace(org.ow2.proactive.scheduler.common.task.dataspaces.RemoteSpace) SchedulerNodeClient(org.ow2.proactive.scheduler.task.client.SchedulerNodeClient) VariablesMap(org.ow2.proactive.scheduler.task.utils.VariablesMap) ScriptHandler(org.ow2.proactive.scripting.ScriptHandler)

Aggregations

ScriptHandler (org.ow2.proactive.scripting.ScriptHandler)19 VariablesMap (org.ow2.proactive.scheduler.task.utils.VariablesMap)16 Test (org.junit.Test)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 HashMap (java.util.HashMap)6 Serializable (java.io.Serializable)4 SchedulerNodeClient (org.ow2.proactive.scheduler.task.client.SchedulerNodeClient)4 File (java.io.File)3 RemoteSpace (org.ow2.proactive.scheduler.common.task.dataspaces.RemoteSpace)3 Decrypter (org.ow2.proactive.scheduler.task.utils.Decrypter)3 InvalidScriptException (org.ow2.proactive.scripting.InvalidScriptException)3 ScriptResult (org.ow2.proactive.scripting.ScriptResult)3 TaskResultImpl (org.ow2.proactive.scheduler.task.TaskResultImpl)2 Stopwatch (com.google.common.base.Stopwatch)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 PrintStream (java.io.PrintStream)1