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;
}
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);
}
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);
}
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();
}
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;
}
Aggregations