use of org.ow2.proactive.scheduler.common.task.NativeTask in project scheduling by ow2-proactive.
the class FlatJobFactory method createNativeJobFromCommandsFile.
/**
* Create a job from a String representing file path, this text file contains native commands to launch
* Every line of the text file is taken and considered as a native command from which a native task is built,
* except lines beginning with {@link FlatJobFactory#JOB_DEFAULT_NAME_PREFIX} and empty lines.
* So job in result is made of several native tasks without dependencies.
*
* @param commandFilePath a string representing a text file containing native commands.
* @param jobName A String representing a name to give to the job. If null, default job name is made of
* {@link FlatJobFactory#JOB_DEFAULT_NAME_PREFIX} + userName parameter.
* @param selectionScriptPath a Path to a file containing a selection script, or null if
* no script is needed.
* @param userName name of connected user that asked job creation, null otherwise. This parameter
* is only used for default job's name creation.
* @return a job object representing created job and ready-to-schedule job.
* @throws JobCreationException with a relevant error message if an error occurs.
*/
public Job createNativeJobFromCommandsFile(String commandFilePath, String jobName, String selectionScriptPath, String userName) throws JobCreationException {
if (jobName == null) {
jobName = JOB_DEFAULT_NAME_PREFIX + userName;
}
Job nativeJob = new TaskFlowJob();
nativeJob.setName(jobName);
logger.debug("Job : " + nativeJob.getName());
try {
File commandFile = new File(commandFilePath);
if (!commandFile.isFile()) {
throw new JobCreationException("Error occured during Job creation, " + "check that file " + commandFilePath + " exists and is a readable file");
}
String commandLine;
int task_number = 0;
ArrayList<String> commandList = new ArrayList<>();
try (BufferedReader reader = new BufferedReader(new FileReader(commandFile))) {
while ((commandLine = reader.readLine()) != null) {
commandLine = commandLine.trim();
if (!commandLine.startsWith(CMD_FILE_COMMENT_CHAR, 0) && !"".equals(commandLine)) {
commandList.add(commandLine);
}
}
}
if (commandList.size() == 0) {
throw new JobCreationException("Error occured during Job creation, " + "No any valid command line has been built from" + commandFilePath + "");
}
// compute padding for task number
int numberOfDigit = Integer.toString(commandList.size()).length();
NumberFormat nf = NumberFormat.getInstance();
nf.setMaximumIntegerDigits(numberOfDigit);
nf.setMinimumIntegerDigits(numberOfDigit);
for (String command : commandList) {
NativeTask t = createNativeTaskFromCommandString(command, "task_" + (nf.format(++task_number)), selectionScriptPath);
t.setPreciousResult(true);
((TaskFlowJob) nativeJob).addTask(t);
logger.debug("-> Task Name = " + t.getName());
logger.debug("-> command = " + t.getCommandLine() + "\n");
}
} catch (Exception e) {
throw new JobCreationException(e);
}
return nativeJob;
}
use of org.ow2.proactive.scheduler.common.task.NativeTask in project scheduling by ow2-proactive.
the class FlatJobFactory method createNativeTaskFromCommandString.
/**
* Creates a native task from a string representing a native command to execute.
* @param command a String representing a native command.
* @param taskName an eventual name for the task.
* @param selectionScriptPath path to an existing file containing a selection script code.
* @return a NativeTask object that can be put in a Job Object.
* @throws InvalidScriptException if an error occurs in definition of selection script
* from file path specified.
*/
private NativeTask createNativeTaskFromCommandString(String command, String taskName, String selectionScriptPath) throws InvalidScriptException {
NativeTask desc = new NativeTask();
desc.setCommandLine(Tools.parseCommandLine(command));
desc.setName(taskName);
if (selectionScriptPath != null) {
SelectionScript script = new SelectionScript(new SimpleScript(new File(selectionScriptPath), null), true);
desc.addSelectionScript(script);
}
return desc;
}
use of org.ow2.proactive.scheduler.common.task.NativeTask in project scheduling by ow2-proactive.
the class JobComparator method isTaskEqual.
private boolean isTaskEqual(Task t1, Task t2) throws IOException, ClassNotFoundException {
if ((t1 == null) && (t2 == null))
return true;
if ((t1 == null) ^ (t2 == null)) {
stack.push("One of 2 tasks is null");
return false;
}
if (!isEqualCommonAttribute(t1, t2))
return false;
if (!t1.getName().equals(t2.getName())) {
stack.push("name");
return false;
}
if (!isEqualString(t1.getDescription(), t2.getDescription())) {
stack.push("description");
return false;
}
if (t1.getWallTime() != t2.getWallTime()) {
stack.push("walltime");
return false;
}
// ****** task dependencies ****
stack.push("task dependenices");
List<Task> dep1 = t1.getDependencesList();
List<Task> dep2 = t2.getDependencesList();
if (dep1 == null ^ dep2 == null) {
stack.push("one dependency list is empty");
return false;
}
if (dep1 != null) {
if (dep1.size() != dep2.size()) {
stack.push("sizes don't match");
return false;
}
// we only compare the names in the 2 dependencies lists
int dep1Size = dep1.size();
Set<String> names1 = new HashSet<String>(dep1Size);
Set<String> names2 = new HashSet<String>(dep1Size);
for (int k = 0; k < dep1Size; k++) {
names1.add(dep1.get(k).getName());
names2.add(dep2.get(k).getName());
}
if (!CollectionUtils.isEqualCollection(names1, names2)) {
return false;
}
}
// task dependencies
stack.pop();
// **** parallel environment ****
stack.push("parallel environment");
if (!isEqualParallelEnvironment(t1.getParallelEnvironment(), t2.getParallelEnvironment()))
return false;
// parallel env
stack.pop();
// input files
stack.push("input files");
if (!isEqualInputFiles(t1.getInputFilesList(), t2.getInputFilesList()))
return false;
stack.pop();
stack.push("output files");
if (!isEqualOutputFiles(t1.getOutputFilesList(), t2.getOutputFilesList()))
return false;
stack.pop();
// scripts
stack.push("pre script");
if (!isEqualScript(t1.getPreScript(), t2.getPreScript()))
return false;
stack.pop();
stack.push("post script");
if (!isEqualScript(t1.getPostScript(), t2.getPostScript()))
return false;
stack.pop();
stack.push("cleaning script");
if (!isEqualScript(t1.getCleaningScript(), t2.getCleaningScript()))
return false;
stack.pop();
stack.push("selection scripts");
List<SelectionScript> ss1 = t1.getSelectionScripts();
List<SelectionScript> ss2 = t2.getSelectionScripts();
if ((ss1 == null) ^ (ss2 == null)) {
stack.push("One of two lists of selection scripts is null");
return false;
}
if (ss1 != null) {
if (t1.getSelectionScripts().size() != t2.getSelectionScripts().size()) {
stack.push("lists size don't match");
return false;
}
for (int k = 0; k < t1.getSelectionScripts().size(); k++) {
if (!isEqualScript(t1.getSelectionScripts().get(k), t2.getSelectionScripts().get(k))) {
return false;
}
}
}
// select scripts
stack.pop();
// flow control
if (t1.getFlowBlock() != t2.getFlowBlock()) {
stack.push("flow block");
return false;
}
stack.push("flow control");
if (!isEqualFlowControl(t1.getFlowScript(), t2.getFlowScript()))
return false;
stack.pop();
// ***** task executable *****
if (!isEqualClass(t1.getClass(), t2.getClass())) {
stack.push("Executable types don't match");
return false;
}
if (t1 instanceof JavaTask) {
JavaTask jt1 = (JavaTask) t1;
JavaTask jt2 = (JavaTask) t2;
stack.push("arguments");
if (!isEqualMap(jt1.getArguments(), jt2.getArguments()))
return false;
stack.pop();
stack.push("executable class");
if (!isEqualString(jt1.getExecutableClassName(), jt2.getExecutableClassName()))
return false;
stack.pop();
stack.push("forked environemnt");
if (!isEqualForkedEnvironment(jt1.getForkEnvironment(), jt2.getForkEnvironment()))
return false;
stack.pop();
}
if (t1 instanceof NativeTask) {
NativeTask nt1 = (NativeTask) t1;
NativeTask nt2 = (NativeTask) t2;
String[] cl1 = nt1.getCommandLine();
String[] cl2 = nt2.getCommandLine();
if (cl1 == null ^ cl2 == null) {
return false;
} else if (cl1 != null) {
if (!CollectionUtils.isEqualCollection(Arrays.asList(cl1), Arrays.asList(cl2))) {
return false;
}
}
}
if (t1 instanceof ScriptTask) {
ScriptTask st1 = (ScriptTask) t1;
ScriptTask st2 = (ScriptTask) t2;
if (!isEqualScript(st1.getScript(), st2.getScript()))
return false;
}
return true;
}
use of org.ow2.proactive.scheduler.common.task.NativeTask in project scheduling by ow2-proactive.
the class StaxJobFactory method displayJobInfo.
private void displayJobInfo(Job job) {
if (logger.isDebugEnabled()) {
logger.debug("type: " + job.getType());
logger.debug("name: " + job.getName());
logger.debug("description: " + job.getDescription());
logger.debug("projectName: " + job.getProjectName());
logger.debug("variables: " + job.getVariables());
logger.debug("priority: " + job.getPriority());
logger.debug("onTaskError: " + job.getOnTaskErrorProperty().getValue().toString());
logger.debug("restartTaskOnError: " + job.getRestartTaskOnError());
logger.debug("maxNumberOfExecution: " + job.getMaxNumberOfExecution());
logger.debug("inputSpace: " + job.getInputSpace());
logger.debug("outputSpace: " + job.getOutputSpace());
logger.debug("genericInformation: " + job.getGenericInformation());
logger.debug("TASKS ------------------------------------------------");
ArrayList<Task> tasks = new ArrayList<>();
switch(job.getType()) {
case TASKSFLOW:
tasks.addAll(((TaskFlowJob) job).getTasks());
break;
default:
break;
}
for (Task t : tasks) {
logger.debug("name: " + t.getName());
logger.debug("description: " + t.getDescription());
logger.debug("parallel: " + t.isParallel());
logger.debug("nbNodes: " + (t.getParallelEnvironment() == null ? "1" : t.getParallelEnvironment().getNodesNumber()));
logger.debug("onTaskError: " + t.getOnTaskErrorProperty().getValue().toString());
logger.debug("preciousResult: " + t.isPreciousResult());
logger.debug("preciousLogs: " + t.isPreciousLogs());
logger.debug("restartTaskOnError: " + t.getRestartTaskOnError());
logger.debug("maxNumberOfExecution: " + t.getMaxNumberOfExecution());
logger.debug("walltime: " + t.getWallTime());
logger.debug("selectionScripts: " + t.getSelectionScripts());
logger.debug("preScript: " + t.getPreScript());
logger.debug("postScript: " + t.getPostScript());
logger.debug("cleaningScript: " + t.getCleaningScript());
try {
logger.debug("inputFileList: length=" + t.getInputFilesList().size());
} catch (NullPointerException ignored) {
}
try {
logger.debug("outputFileList: length=" + t.getOutputFilesList().size());
} catch (NullPointerException ignored) {
}
if (t.getDependencesList() != null) {
String dep = "dependence: ";
for (Task tdep : t.getDependencesList()) {
dep += tdep.getName() + " ";
}
logger.debug(dep);
} else {
logger.debug("dependence: null");
}
logger.debug("genericInformation: " + t.getGenericInformation());
logger.debug("variables: " + t.getVariables());
if (t instanceof JavaTask) {
logger.debug("class: " + ((JavaTask) t).getExecutableClassName());
try {
logger.debug("args: " + ((JavaTask) t).getArguments());
} catch (Exception e) {
logger.debug("Cannot get args: " + e.getMessage(), e);
}
logger.debug("fork: " + ((JavaTask) t).isFork());
} else if (t instanceof NativeTask) {
logger.debug("commandLine: " + Arrays.toString(((NativeTask) t).getCommandLine()));
} else if (t instanceof ScriptTask) {
logger.debug("script: " + ((ScriptTask) t).getScript());
}
ForkEnvironment forkEnvironment = t.getForkEnvironment();
if (forkEnvironment != null) {
logger.debug("javaHome: " + forkEnvironment.getJavaHome());
logger.debug("systemEnvironment: " + forkEnvironment.getSystemEnvironment());
logger.debug("jvmArguments: " + forkEnvironment.getJVMArguments());
logger.debug("classpath: " + forkEnvironment.getAdditionalClasspath());
logger.debug("envScript: " + forkEnvironment.getEnvScript());
}
logger.debug("--------------------------------------------------");
}
}
}
use of org.ow2.proactive.scheduler.common.task.NativeTask 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);
}
}
}
Aggregations