use of org.ow2.proactive.scripting.TaskScript in project scheduling by ow2-proactive.
the class StaxJobFactory method createTask.
/**
* Fill the given task by the information that are at the given cursorTask.
* Leave the method with the cursor at the end of 'ELEMENT_TASK' tag.
*
* @param cursorTask the streamReader with the cursor on the 'ELEMENT_TASK' tag.
* @return The newly created task that can be any type.
*/
private Task createTask(XMLStreamReader cursorTask, Job job, Map<String, ArrayList<String>> dependencies) throws JobCreationException {
int i = 0;
XMLTags currentTag = null;
String current = null;
String taskName = null;
try {
Task toReturn = null;
Task tmpTask = new Task() {
};
// parse job attributes and fill the temporary one
int attrLen = cursorTask.getAttributeCount();
for (i = 0; i < attrLen; i++) {
String attributeName = cursorTask.getAttributeLocalName(i);
String attributeValue = cursorTask.getAttributeValue(i);
if (XMLAttributes.COMMON_NAME.matches(attributeName)) {
tmpTask.setName(attributeValue);
taskName = attributeValue;
} else if (XMLAttributes.TASK_NB_NODES.matches(attributeName)) {
int numberOfNodesNeeded = Integer.parseInt(replace(attributeValue, tmpTask.getVariablesOverriden(job)));
tmpTask.setParallelEnvironment(new ParallelEnvironment(numberOfNodesNeeded));
} else if (XMLAttributes.COMMON_CANCEL_JOB_ON_ERROR.matches(attributeName)) {
handleCancelJobOnErrorAttribute(tmpTask, attributeValue);
} else if (XMLAttributes.COMMON_ON_TASK_ERROR.matches(attributeName)) {
tmpTask.setOnTaskError(OnTaskError.getInstance(replace(attributeValue, tmpTask.getVariablesOverriden(job))));
} else if (XMLAttributes.COMMON_RESTART_TASK_ON_ERROR.matches(attributeName)) {
tmpTask.setRestartTaskOnError(RestartMode.getMode(replace(attributeValue, tmpTask.getVariablesOverriden(job))));
} else if (XMLAttributes.COMMON_MAX_NUMBER_OF_EXECUTION.matches(attributeName)) {
tmpTask.setMaxNumberOfExecution(Integer.parseInt(replace(attributeValue, tmpTask.getVariablesOverriden(job))));
} else if (XMLAttributes.TASK_PRECIOUS_RESULT.matches(attributeName)) {
tmpTask.setPreciousResult(Boolean.parseBoolean(replace(attributeValue, tmpTask.getVariablesOverriden(job))));
} else if (XMLAttributes.TASK_PRECIOUS_LOGS.matches(attributeName)) {
tmpTask.setPreciousLogs(Boolean.parseBoolean(replace(attributeValue, tmpTask.getVariablesOverriden(job))));
} else if (XMLAttributes.TASK_WALLTIME.matches(attributeName)) {
tmpTask.setWallTime(Tools.formatDate(replace(attributeValue, tmpTask.getVariablesOverriden(job))));
} else if (XMLAttributes.TASK_RUN_AS_ME.matches(attributeName)) {
tmpTask.setRunAsMe(Boolean.parseBoolean(replace(attributeValue, tmpTask.getVariablesOverriden(job))));
}
}
int eventType;
boolean shouldContinue = true;
while (shouldContinue && cursorTask.hasNext()) {
eventType = cursorTask.next();
switch(eventType) {
case XMLEvent.START_ELEMENT:
current = cursorTask.getLocalName();
currentTag = null;
if (XMLTags.COMMON_GENERIC_INFORMATION.matches(current)) {
tmpTask.setGenericInformation(getGenericInformation(cursorTask, tmpTask.getVariablesOverriden(job)));
} else if (XMLTags.VARIABLES.matches(current)) {
Map<String, TaskVariable> taskVariablesMap = createTaskVariables(cursorTask, tmpTask.getVariablesOverriden(job));
tmpTask.setVariables(taskVariablesMap);
} else if (XMLTags.COMMON_DESCRIPTION.matches(current)) {
tmpTask.setDescription(getDescription(cursorTask, tmpTask.getVariablesOverriden(job)));
} else if (XMLTags.DS_INPUT_FILES.matches(current)) {
setIOFIles(cursorTask, XMLTags.DS_INPUT_FILES.getXMLName(), tmpTask, tmpTask.getVariablesOverriden(job));
} else if (XMLTags.DS_OUTPUT_FILES.matches(current)) {
setIOFIles(cursorTask, XMLTags.DS_OUTPUT_FILES.getXMLName(), tmpTask, tmpTask.getVariablesOverriden(job));
} else if (XMLTags.PARALLEL_ENV.matches(current)) {
tmpTask.setParallelEnvironment(createParallelEnvironment(cursorTask, tmpTask.getVariablesOverriden(job)));
} else if (XMLTags.SCRIPT_SELECTION.matches(current)) {
tmpTask.setSelectionScripts(createSelectionScript(cursorTask, tmpTask.getVariablesOverriden(job)));
} else if (XMLTags.FORK_ENVIRONMENT.matches(current)) {
tmpTask.setForkEnvironment(createForkEnvironment(cursorTask, tmpTask.getVariablesOverriden(job)));
} else if (XMLTags.SCRIPT_PRE.matches(current)) {
tmpTask.setPreScript(createScript(cursorTask, tmpTask.getVariablesOverriden(job)));
} else if (XMLTags.SCRIPT_POST.matches(current)) {
tmpTask.setPostScript(createScript(cursorTask, tmpTask.getVariablesOverriden(job)));
} else if (XMLTags.SCRIPT_CLEANING.matches(current)) {
tmpTask.setCleaningScript(createScript(cursorTask, tmpTask.getVariablesOverriden(job)));
} else if (XMLTags.FLOW.matches(current)) {
tmpTask.setFlowScript(createControlFlowScript(cursorTask, tmpTask, tmpTask.getVariablesOverriden(job)));
} else if (XMLTags.TASK_DEPENDENCES.matches(current)) {
currentTag = XMLTags.TASK_DEPENDENCES;
dependencies.putAll(createDependences(cursorTask, tmpTask));
} else if (XMLTags.JAVA_EXECUTABLE.matches(current)) {
toReturn = new JavaTask();
setJavaExecutable((JavaTask) toReturn, cursorTask, tmpTask.getVariablesOverriden(job));
} else if (XMLTags.NATIVE_EXECUTABLE.matches(current)) {
toReturn = new NativeTask();
setNativeExecutable((NativeTask) toReturn, cursorTask);
} else if (XMLTags.SCRIPT_EXECUTABLE.matches(current)) {
toReturn = new ScriptTask();
((ScriptTask) toReturn).setScript(new TaskScript(createScript(cursorTask, tmpTask.getVariablesOverriden(job))));
}
break;
case XMLEvent.END_ELEMENT:
current = cursorTask.getLocalName();
if (XMLTags.TASK.matches(cursorTask.getLocalName())) {
shouldContinue = false;
}
break;
}
}
// fill the real task with common attribute if it is a new one
autoCopyfields(CommonAttribute.class, tmpTask, toReturn);
autoCopyfields(Task.class, tmpTask, toReturn);
if (toReturn != null) {
if (toReturn.getRestartTaskOnErrorProperty().isSet()) {
toReturn.setRestartTaskOnError(toReturn.getRestartTaskOnError());
}
if (toReturn.getMaxNumberOfExecutionProperty().isSet()) {
toReturn.setMaxNumberOfExecution(toReturn.getMaxNumberOfExecution());
}
}
return toReturn;
} catch (JobCreationException jce) {
jce.setTaskName(taskName);
if (currentTag != null) {
jce.pushTag(currentTag);
} else {
jce.pushTag(current);
}
throw jce;
} catch (Exception e) {
String attrtmp = null;
if (cursorTask.isStartElement() && cursorTask.getAttributeCount() > i) {
attrtmp = cursorTask.getAttributeLocalName(i);
}
if (currentTag != null) {
throw new JobCreationException(currentTag, attrtmp, e);
} else {
throw new JobCreationException(current, attrtmp, e);
}
}
}
use of org.ow2.proactive.scripting.TaskScript in project scheduling by ow2-proactive.
the class Job2XMLTransformerTest method argumentsInScript.
@Test
public void argumentsInScript() throws Exception {
File xmlFile = tmpFolder.newFile();
TaskFlowJob job = new TaskFlowJob();
job.setName("simpleJob");
String[] params = { "param1", "param2" };
SimpleScript script = new SimpleScript("\nprint('arguments[0]='+arguments[0])\n", "javascript", params);
ScriptTask task = new ScriptTask();
task.setName("testTask");
task.setScript(new TaskScript(script));
job.addTask(task);
new Job2XMLTransformer().job2xmlFile(job, xmlFile);
TaskFlowJob recreatedJob = (TaskFlowJob) (JobFactory.getFactory().createJob(xmlFile.getAbsolutePath()));
Assert.assertEquals("param1", ((ScriptTask) recreatedJob.getTask("testTask")).getScript().getParameters()[0].toString());
Assert.assertEquals("param2", ((ScriptTask) recreatedJob.getTask("testTask")).getScript().getParameters()[1].toString());
}
use of org.ow2.proactive.scripting.TaskScript in project scheduling by ow2-proactive.
the class InProcessTaskExecutorTest method testPaUserVariableAvailabilityFromScriptEngine.
@Test
public void testPaUserVariableAvailabilityFromScriptEngine() throws Throwable {
TestTaskOutput taskOutput = new TestTaskOutput();
String jobOwner = "JohnDoe";
TaskLauncherInitializer initializer = new TaskLauncherInitializer();
initializer.setJobOwner(jobOwner);
initializer.setTaskId(TaskIdImpl.createTaskId(JobIdImpl.makeJobId("1000"), "job", 1000L));
new InProcessTaskExecutor().execute(new TaskContext(new ScriptExecutableContainer(new TaskScript(new SimpleScript("print variables.get('PA_USER')", "python"))), initializer, null, new NodeDataSpacesURIs("", "", "", "", "", ""), "", ""), taskOutput.outputStream, taskOutput.error);
assertEquals(jobOwner, taskOutput.output().trim());
}
use of org.ow2.proactive.scripting.TaskScript in project scheduling by ow2-proactive.
the class KillTaskLauncherTest method finished_but_terminate_not_called_back.
@Test
@Repeat(value = repetitions, parallel = parallel, timeout = timeout)
public void finished_but_terminate_not_called_back() throws Throwable {
final ScriptExecutableContainer executableContainer = new ScriptExecutableContainer(new TaskScript(new SimpleScript("result='done'", "javascript")));
TaskLauncherInitializer initializer = new TaskLauncherInitializer();
initializer.setTaskId(TaskIdImpl.createTaskId(JobIdImpl.makeJobId("1000"), "job", 1000L));
final TaskLauncher taskLauncher = createLauncherWithInjectedMocks(initializer, new TestTaskLauncherFactory(new Semaphore(0)));
final TaskLauncher taskLauncherPA = PAActiveObject.turnActive(taskLauncher);
TaskResultWaiter taskResultWaiter = new TaskResultWaiter();
WaitForResultNotification waitForResultNotification = new WaitForResultNotification(taskResultWaiter);
waitForResultNotification = PAActiveObject.turnActive(waitForResultNotification);
taskLauncherPA.doTask(executableContainer, null, waitForResultNotification);
assertEquals("done", taskResultWaiter.getTaskResult().value());
assertTaskLauncherIsTerminated(taskLauncherPA);
PAActiveObject.terminateActiveObject(taskLauncherPA, true);
}
use of org.ow2.proactive.scripting.TaskScript in project scheduling by ow2-proactive.
the class KillTaskLauncherTest method kill_while_sleeping_in_task.
@Test
@Repeat(value = repetitions, parallel = parallel, timeout = timeout)
public void kill_while_sleeping_in_task() throws Exception {
final ScriptExecutableContainer executableContainer = new ScriptExecutableContainer(new TaskScript(new SimpleScript("java.lang.Thread.sleep(10000)", "javascript")));
TaskLauncherInitializer initializer = new TaskLauncherInitializer();
initializer.setTaskId(TaskIdImpl.createTaskId(JobIdImpl.makeJobId("1000"), "job", 1000L));
Semaphore taskRunning = new Semaphore(0);
final TaskLauncher taskLauncher = createLauncherWithInjectedMocks(initializer, new TestTaskLauncherFactory(taskRunning));
final TaskLauncher taskLauncherPA = PAActiveObject.turnActive(taskLauncher);
taskLauncherPA.doTask(executableContainer, null, null);
taskRunning.acquire();
taskLauncherPA.kill();
assertTaskLauncherIsTerminated(taskLauncherPA);
PAActiveObject.terminateActiveObject(taskLauncherPA, true);
}
Aggregations