use of org.ow2.proactive.scheduler.common.task.ScriptTask in project scheduling by ow2-proactive.
the class Attribute method createTaskElement.
/**
* Creates the task element, corressponding to <define name="task">
*/
private Element createTaskElement(Document doc, Task task) {
Element taskE = doc.createElementNS(Schemas.SCHEMA_LATEST.getNamespace(), XMLTags.TASK.getXMLName());
if (task.getOnTaskErrorProperty().isSet()) {
setAttribute(taskE, XMLAttributes.COMMON_ON_TASK_ERROR, task.getOnTaskErrorProperty().getValue().toString(), true);
}
if (task.getMaxNumberOfExecutionProperty().isSet()) {
setAttribute(taskE, XMLAttributes.COMMON_MAX_NUMBER_OF_EXECUTION, Integer.toString(task.getMaxNumberOfExecution()));
}
setAttribute(taskE, XMLAttributes.COMMON_NAME, task.getName(), true);
if (task.getRestartTaskOnErrorProperty().isSet()) {
setAttribute(taskE, XMLAttributes.COMMON_RESTART_TASK_ON_ERROR, task.getRestartTaskOnError().toString());
}
if (task.getTaskRetryDelayProperty().isSet()) {
setAttribute(taskE, XMLAttributes.COMMON_TASK_RETRY_DELAY, formatDate(task.getTaskRetryDelay()));
}
// *** task attributes ***
if (task.getWallTime() != 0) {
setAttribute(taskE, XMLAttributes.TASK_WALLTIME, formatDate(task.getWallTime()));
}
if (task.isRunAsMe()) {
setAttribute(taskE, XMLAttributes.TASK_RUN_AS_ME, "true");
}
if (task.isFork() != null && task.isFork()) {
setAttribute(taskE, XMLAttributes.TASK_FORK, "true");
}
if (task.isPreciousResult()) {
setAttribute(taskE, XMLAttributes.TASK_PRECIOUS_RESULT, "true");
}
if (task.isPreciousLogs()) {
setAttribute(taskE, XMLAttributes.TASK_PRECIOUS_LOGS, "true");
}
// <ref name="taskDescription"/>
if (task.getDescription() != null) {
Element descrNode = createCDataElement(doc, XMLTags.COMMON_DESCRIPTION.getXMLName(), task.getDescription());
taskE.appendChild(descrNode);
}
// <ref name="variables"/>
if (task.getVariables() != null && !task.getVariables().isEmpty()) {
Element variablesE = createTaskVariablesElement(doc, task.getVariables());
taskE.appendChild(variablesE);
}
// <ref name="genericInformation"/>
if ((task.getGenericInformation() != null) && (task.getGenericInformation().size() > 0)) {
Element genericInfoE = createGenericInformation(doc, task.getGenericInformation());
taskE.appendChild(genericInfoE);
}
// <ref name="depends"/>
List<Task> dependencies = task.getDependencesList();
if ((dependencies != null) && (dependencies.size() > 0)) {
Element dependsE = doc.createElementNS(Schemas.SCHEMA_LATEST.getNamespace(), XMLTags.TASK_DEPENDENCES.getXMLName());
for (Task dep : dependencies) {
Element dependsTask = doc.createElementNS(Schemas.SCHEMA_LATEST.getNamespace(), XMLTags.TASK_DEPENDENCES_TASK.getXMLName());
setAttribute(dependsTask, XMLAttributes.TASK_DEPENDS_REF, dep.getName(), true);
dependsE.appendChild(dependsTask);
}
taskE.appendChild(dependsE);
}
// if has dependencies
// <ref name="inputFiles"/>
List<InputSelector> inputFiles = task.getInputFilesList();
if (inputFiles != null) {
Element inputFilesE = doc.createElementNS(Schemas.SCHEMA_LATEST.getNamespace(), XMLTags.DS_INPUT_FILES.getXMLName());
for (InputSelector inputSelector : inputFiles) {
FileSelector fs = inputSelector.getInputFiles();
Element filesE = doc.createElementNS(Schemas.SCHEMA_LATEST.getNamespace(), XMLTags.DS_FILES.getXMLName());
// pattern
if (!fs.getIncludes().isEmpty())
setAttribute(filesE, XMLAttributes.DS_INCLUDES, fs.getIncludes().iterator().next(), true);
if (!fs.getExcludes().isEmpty())
setAttribute(filesE, XMLAttributes.DS_EXCLUDES, fs.getExcludes().iterator().next(), true);
if (inputSelector.getMode() != null) {
setAttribute(filesE, XMLAttributes.DS_ACCESS_MODE, inputSelector.getMode().toString(), true);
}
inputFilesE.appendChild(filesE);
}
taskE.appendChild(inputFilesE);
}
// <ref name="parallel"/>
Element parallelEnvE = createParallelEnvironment(doc, task);
if (parallelEnvE != null)
taskE.appendChild(parallelEnvE);
// <ref name="selection"/>
List<SelectionScript> selectionScripts = task.getSelectionScripts();
if (selectionScripts != null && selectionScripts.size() > 0) {
Element selectionE = doc.createElementNS(Schemas.SCHEMA_LATEST.getNamespace(), XMLTags.SCRIPT_SELECTION.getXMLName());
for (SelectionScript selectionScript : selectionScripts) {
Element scriptE = createScriptElement(doc, selectionScript);
selectionE.appendChild(scriptE);
}
taskE.appendChild(selectionE);
}
// <ref name="forkEnvironment"/>
if (task.getForkEnvironment() != null) {
Element forkEnvE = createForkEnvironmentElement(doc, task.getForkEnvironment());
taskE.appendChild(forkEnvE);
}
// <ref name="pre"/>
Script preScript = task.getPreScript();
if (preScript != null) {
Element preE = doc.createElementNS(Schemas.SCHEMA_LATEST.getNamespace(), XMLTags.SCRIPT_PRE.getXMLName());
Element scriptE = createScriptElement(doc, preScript);
preE.appendChild(scriptE);
taskE.appendChild(preE);
}
// <ref name="executable"/>
Element executableE = null;
if (task instanceof JavaTask) {
executableE = createJavaExecutableElement(doc, (JavaTask) task);
} else if (task instanceof NativeTask) {
executableE = createNativeExecutableElement(doc, (NativeTask) task);
} else if (task instanceof ScriptTask) {
executableE = createScriptExecutableElement(doc, (ScriptTask) task);
}
taskE.appendChild(executableE);
// <ref name="flow"/>
Element controlFlowE = createFlowControlElement(doc, task);
if (controlFlowE != null)
taskE.appendChild(controlFlowE);
// <ref name="post"/>
Script postScript = task.getPostScript();
if (postScript != null) {
Element postE = doc.createElementNS(Schemas.SCHEMA_LATEST.getNamespace(), XMLTags.SCRIPT_POST.getXMLName());
Element scriptE = createScriptElement(doc, postScript);
postE.appendChild(scriptE);
taskE.appendChild(postE);
}
// <ref name="cleaning"/>
Script cleanScript = task.getCleaningScript();
if (cleanScript != null) {
Element cleanE = doc.createElementNS(Schemas.SCHEMA_LATEST.getNamespace(), XMLTags.SCRIPT_CLEANING.getXMLName());
Element scriptE = createScriptElement(doc, cleanScript);
cleanE.appendChild(scriptE);
taskE.appendChild(cleanE);
}
// <ref name="outputFiles"/>
List<OutputSelector> outputFiles = task.getOutputFilesList();
if (outputFiles != null) {
Element outputFilesE = doc.createElementNS(Schemas.SCHEMA_LATEST.getNamespace(), XMLTags.DS_OUTPUT_FILES.getXMLName());
for (OutputSelector outputSelector : outputFiles) {
FileSelector fs = outputSelector.getOutputFiles();
Element filesE = doc.createElementNS(Schemas.SCHEMA_LATEST.getNamespace(), XMLTags.DS_FILES.getXMLName());
// pattern
if (!fs.getIncludes().isEmpty())
setAttribute(filesE, XMLAttributes.DS_INCLUDES, fs.getIncludes().iterator().next(), true);
if (!fs.getExcludes().isEmpty())
setAttribute(filesE, XMLAttributes.DS_EXCLUDES, fs.getExcludes().iterator().next(), true);
if (outputSelector.getMode() != null) {
setAttribute(filesE, XMLAttributes.DS_ACCESS_MODE, outputSelector.getMode().toString(), true);
}
outputFilesE.appendChild(filesE);
}
taskE.appendChild(outputFilesE);
}
return taskE;
}
use of org.ow2.proactive.scheduler.common.task.ScriptTask 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, GlobalVariablesData globalVariablesData) throws JobCreationException {
int i = 0;
XMLTags currentTag = null;
String current = null;
String taskName = null;
try {
Task toReturn = null;
Task tmpTask = new Task() {
};
// To allow variable replacements on the task attributes, store them until task variables are parsed
Map<String, String> delayedTaskAttributes = new LinkedHashMap<>();
int attrLen = cursorTask.getAttributeCount();
for (; i < attrLen; i++) {
String attributeName = cursorTask.getAttributeLocalName(i);
String attributeValue = cursorTask.getAttributeValue(i);
delayedTaskAttributes.put(attributeName, attributeValue);
if (XMLAttributes.COMMON_NAME.matches(attributeName)) {
// it is currently not possible to use variables in task names (due to the complexity of such feature)
taskName = attributeValue;
tmpTask.setName(taskName);
}
}
// in the XML schema, description is defined after variables for tasks
String delayedDescription = null;
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)) {
// Resolve task generic infos using job variables and job generic infos
Map<String, String> jobVariablesWithGenericInfos = tmpTask.getVariablesOverriden(job);
if (job.getGenericInformation() != null)
jobVariablesWithGenericInfos.putAll(job.getGenericInformation());
Map<String, String> unresolvedGenericInformationDefinedInWorkflow = getUnresolvedGenericInformations(cursorTask, true, globalVariablesData);
tmpTask.setUnresolvedGenericInformation(unresolvedGenericInformationDefinedInWorkflow);
tmpTask.setGenericInformation(getResolvedGenericInformations(unresolvedGenericInformationDefinedInWorkflow, jobVariablesWithGenericInfos));
} else if (XMLTags.VARIABLES.matches(current)) {
// Add resolved task variables using both job and task variables
// the final value of the variable can either be overwritten by a value of the job submission variables map or
// use in a pattern such value
Map<String, TaskVariable> unresolvedTaskVariablesMap = createUnresolvedTaskVariables(cursorTask);
tmpTask.getUnresolvedVariables().putAll(unresolvedTaskVariablesMap);
Map<String, TaskVariable> taskVariablesMap = replaceVariablesInTaskVariablesMap(unresolvedTaskVariablesMap, getInitialTaskVariablesOverriden(job, unresolvedTaskVariablesMap));
tmpTask.setVariables(taskVariablesMap);
} else if (XMLTags.COMMON_DESCRIPTION.matches(current)) {
delayedDescription = getDescription(cursorTask, Collections.EMPTY_MAP);
} 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;
default:
}
}
// fill the task attributes using variable resolution
handleTaskAttributes(tmpTask, job, delayedTaskAttributes);
tmpTask.setDescription(replace(delayedDescription, tmpTask.getVariablesOverriden(job)));
// check whether task properties has conflicts between "runAsMe" and "fork"
if (tmpTask.isRunAsMe() && Boolean.FALSE.equals(tmpTask.isFork())) {
throw new JobCreationException(String.format("The task contains conflicting properties between 'runAsMe=%s' and 'fork=%s', because 'runAsMe=true' implies 'fork=true'.", tmpTask.isRunAsMe(), tmpTask.isFork()));
}
// 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.scheduler.common.task.ScriptTask in project scheduling by ow2-proactive.
the class TestUnauthorizedScripts method createJob.
public Job createJob(String forkScriptContent, String cleanScriptContent) throws InvalidScriptException, UserException {
TaskFlowJob job = new TaskFlowJob();
job.setName(this.getClass().getSimpleName() + "_forkAndClean");
ScriptTask taskWithFork = new ScriptTask();
taskWithFork.setScript(new TaskScript(new SimpleScript("println 'Hello'", "groovy")));
ForkEnvironment forkEnvironment = new ForkEnvironment();
forkEnvironment.setEnvScript(new SimpleScript(forkScriptContent, "groovy"));
taskWithFork.setForkEnvironment(forkEnvironment);
ScriptTask taskWithClean = new ScriptTask();
taskWithClean.setScript(new TaskScript(new SimpleScript("println 'Hello'", "groovy")));
taskWithClean.setCleaningScript(new SimpleScript(cleanScriptContent, "groovy"));
job.addTask(taskWithFork);
job.addTask(taskWithClean);
return job;
}
use of org.ow2.proactive.scheduler.common.task.ScriptTask in project scheduling by ow2-proactive.
the class TestScriptTask method test_getTaskResult_nullReturningScriptTask_shouldSucceed.
/**
* SCHEDULING-2199 NPE is thrown when retrieving the result of a ScriptTask,
* if the result is 'null'.
*/
private void test_getTaskResult_nullReturningScriptTask_shouldSucceed() throws Throwable {
TaskFlowJob job = (TaskFlowJob) StaxJobFactory.getFactory().createJob(new File(job_null_returning_script_task.toURI()).getAbsolutePath());
JobId id = schedulerHelper.submitJob(job);
schedulerHelper.waitForEventJobFinished(id);
TaskResult taskResult = schedulerHelper.getTaskResult(id, "task");
assertNull(taskResult.value());
}
use of org.ow2.proactive.scheduler.common.task.ScriptTask in project scheduling by ow2-proactive.
the class TestUnauthorizedScripts method createJobSelection.
public Job createJobSelection(String selectionScriptContent) throws InvalidScriptException, UserException {
TaskFlowJob job = new TaskFlowJob();
job.setName(this.getClass().getSimpleName() + "_selection");
ScriptTask taskWithSelection = new ScriptTask();
taskWithSelection.setScript(new TaskScript(new SimpleScript("println 'Hello'", "groovy")));
taskWithSelection.addSelectionScript(new SelectionScript(new SimpleScript(selectionScriptContent, "groovy"), true));
job.addTask(taskWithSelection);
return job;
}
Aggregations