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