use of org.ow2.proactive.scheduler.common.task.NativeTask in project scheduling by ow2-proactive.
the class TestJobNativeSubmission method testJobNativeSubmission.
@Test
public void testJobNativeSubmission() throws Throwable {
// test submission and event reception
TaskFlowJob job = new TaskFlowJob();
NativeTask successfulTask = new NativeTask();
successfulTask.setName("successfulTask");
if (OperatingSystem.getOperatingSystem() == OperatingSystem.windows) {
successfulTask.setCommandLine("cmd", "/C", "ping 127.0.0.1 -n 10", ">", "NUL");
} else {
successfulTask.setCommandLine("ping", "-c", "5", "127.0.0.1");
}
job.addTask(successfulTask);
NativeTask invalidCommandTask = new NativeTask();
invalidCommandTask.setName("invalidCommandTask");
invalidCommandTask.addDependence(successfulTask);
invalidCommandTask.setCommandLine("invalid_command");
job.addTask(invalidCommandTask);
// SCHEDULING-1987
NativeTask taskReadingInput = new NativeTask();
taskReadingInput.setName("taskReadingInput");
if (OperatingSystem.getOperatingSystem() == OperatingSystem.windows) {
// wait for y/n
taskReadingInput.setCommandLine("choice");
} else {
// cat hangs for user's input
taskReadingInput.setCommandLine("cat");
}
job.addTask(taskReadingInput);
JobId id = schedulerHelper.submitJob(job);
log("Job submitted, id " + id.toString());
log("Waiting for jobSubmitted Event");
JobState receivedState = schedulerHelper.waitForEventJobSubmitted(id);
assertEquals(receivedState.getId(), id);
log("Waiting for job running");
JobInfo jInfo = schedulerHelper.waitForEventJobRunning(id);
assertEquals(jInfo.getJobId(), id);
assertEquals(JobStatus.RUNNING, jInfo.getStatus());
schedulerHelper.waitForEventTaskRunning(id, successfulTask.getName());
TaskInfo tInfo = schedulerHelper.waitForEventTaskFinished(id, successfulTask.getName());
assertEquals(TaskStatus.FINISHED, tInfo.getStatus());
schedulerHelper.waitForEventTaskRunning(id, invalidCommandTask.getName());
tInfo = schedulerHelper.waitForEventTaskFinished(id, invalidCommandTask.getName());
assertEquals(TaskStatus.FAULTY, tInfo.getStatus());
TaskInfo taskReadingInputInfo = schedulerHelper.waitForEventTaskFinished(id, taskReadingInput.getName());
if (OperatingSystem.getOperatingSystem() == OperatingSystem.windows) {
// choice fails when input is closed
assertEquals(TaskStatus.FAULTY, taskReadingInputInfo.getStatus());
} else {
assertEquals(TaskStatus.FINISHED, taskReadingInputInfo.getStatus());
}
schedulerHelper.waitForEventJobFinished(id);
// remove job
schedulerHelper.removeJob(id);
schedulerHelper.waitForEventJobRemoved(id);
}
use of org.ow2.proactive.scheduler.common.task.NativeTask in project scheduling by ow2-proactive.
the class TestNativeTaskPaths method testNativeTaskPaths.
@Test
public void testNativeTaskPaths() throws Throwable {
File in = File.createTempFile("input", "space");
in.delete();
in.mkdir();
String inPath = in.getAbsolutePath();
File out = File.createTempFile("output", "space");
out.delete();
out.mkdir();
File outc = new File(out, OutVarsFileC);
File outd = new File(out, OutVarsFileD);
if (outc.exists()) {
outc.delete();
}
if (outd.exists()) {
outd.delete();
}
File scriptTestEnv = null;
if (OperatingSystem.getOperatingSystem() == OperatingSystem.unix) {
scriptTestEnv = new File(inPath + File.separator + scriptCLinux);
scriptTestEnv.createNewFile();
PrintWriter out3 = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(scriptTestEnv))));
out3.print(scriptCLinuxContent);
out3.close();
} else {
scriptTestEnv = new File(inPath + File.separator + scriptCWindows);
scriptTestEnv.createNewFile();
PrintWriter out3 = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(scriptTestEnv))));
out3.print(scriptCWindowsContent);
out3.close();
}
TaskFlowJob job = new TaskFlowJob();
job.setName(this.getClass().getSimpleName());
job.setInputSpace(in.toURI().toURL().toString());
job.setOutputSpace(out.toURI().toURL().toString());
// // testing paths pattern
// NativeTask C = new NativeTask();
// C.setName("C");
// C.addOutputFiles(OutVarsFileC, OutputAccessMode.TransferToOutputSpace);
// switch (OperatingSystem.getOperatingSystem()) {
// case windows:
// C.setCommandLine(new String[] { "cmd", "/C",
// "echo \"$JAVA\" \"$PROACTIVE_HOME\" > $LOCALSPACE\\" + OutVarsFileC });
// break;
// case unix:
// C.setCommandLine(new String[] { "/bin/bash", "-c",
// "echo \\\"$JAVA\\\" \\\"$PROACTIVE_HOME\\\" > $LOCALSPACE/" + OutVarsFileC });
// break;
// default:
// throw new IllegalStateException("Unsupported operating system");
// }
// job.addTask(C);
// testing $USERSPACE environment variable
NativeTask D = new NativeTask();
D.setName("D");
if (OperatingSystem.getOperatingSystem() == OperatingSystem.unix) {
D.addInputFiles(scriptCLinux, InputAccessMode.TransferFromInputSpace);
} else {
D.addInputFiles(scriptCWindows, InputAccessMode.TransferFromInputSpace);
}
D.addOutputFiles(OutVarsFileD, OutputAccessMode.TransferToOutputSpace);
switch(OperatingSystem.getOperatingSystem()) {
case windows:
D.setCommandLine(new String[] { "cmd", "/C", scriptCWindows });
break;
case unix:
D.setCommandLine(new String[] { "/bin/bash", "-c", "chmod u+x $localspace/" + scriptCLinux + "; $localspace/" + scriptCLinux });
break;
default:
throw new IllegalStateException("Unsupported operating system");
}
D.setForkEnvironment(new ForkEnvironment("$LOCALSPACE"));
job.addTask(D);
Scheduler sched = schedulerHelper.getSchedulerInterface();
JobId id = sched.submit(job);
schedulerHelper.waitForEventJobFinished(id);
String contentExpected = "foo";
JobResult jr = schedulerHelper.getJobResult(id);
Assert.assertFalse(jr.hadException());
logger.info("Expected : '" + contentExpected + "'");
// logger.info(jr.getAllResults().get("C").getOutput().getAllLogs(true));
// String receivedc = IOUtils.toString(outc.toURI()).trim();
// logger.info("Received C : '" + receivedc + "'");
// Assert.assertEquals(contentExpected.toLowerCase(), receivedc.toLowerCase());
logger.info(jr.getAllResults().get("D").getOutput().getAllLogs(true));
String receivedd = IOUtils.toString(outd.toURI()).trim();
logger.info("Received D : '" + receivedd + "'");
Assert.assertEquals(contentExpected.toLowerCase(), receivedd.toLowerCase());
}
use of org.ow2.proactive.scheduler.common.task.NativeTask in project scheduling by ow2-proactive.
the class TestPropagatedVariables method createTaskFlowJob.
private TaskFlowJob createTaskFlowJob() throws UserException {
TaskFlowJob flowJob = new TaskFlowJob();
JavaTask taskA = new JavaTask();
taskA.setName("Task_A");
HashMap<String, String> setA = new HashMap<>();
setA.put("Task_A_Var", "Task_A_Val");
taskA.addArgument("set", setA);
taskA.setExecutableClassName(PropagateVariablesExec.class.getName());
flowJob.addTask(taskA);
JavaTask taskB = new JavaTask();
taskB.setName("Task_B");
HashMap<String, String> setB = new HashMap<>();
setB.put("Task_B_Var", "Task_B_Val");
taskB.addArgument("set", setB);
taskB.setExecutableClassName(PropagateVariablesExec.class.getName());
flowJob.addTask(taskB);
JavaTask taskC = new JavaTask();
taskC.setName("Task_C");
taskC.addDependence(taskA);
taskC.addDependence(taskB);
HashMap<String, String> checkC = new HashMap<>();
checkC.put("Task_A_Var", "Task_A_Val");
checkC.put("Task_B_Var", "Task_B_Val");
taskC.addArgument("check", checkC);
taskC.setExecutableClassName(PropagateVariablesExec.class.getName());
flowJob.addTask(taskC);
if (OperatingSystem.unix == OperatingSystem.getOperatingSystem()) {
NativeTask taskD = new NativeTask();
taskD.setName("TaskD");
taskD.setCommandLine("/bin/bash", "-c", "echo $variables_Task_A_Var; test \"$variables_Task_A_Var\" == \"Task_A_Val\"");
taskD.addDependence(taskC);
flowJob.addTask(taskD);
}
return flowJob;
}
use of org.ow2.proactive.scheduler.common.task.NativeTask in project scheduling by ow2-proactive.
the class TestPreciousLogs method testPreciousLogs.
private void testPreciousLogs(boolean createJavaTask, boolean forkEnv) throws Exception {
TaskFlowJob job = new TaskFlowJob();
job.setName(this.getClass().getSimpleName());
Map<String, List<String>> expectedOutput = new LinkedHashMap<>();
for (int i = 0; i < 3; i++) {
String forkOutput = "forkOutput-" + i;
String preOutput = "preOutput-" + i;
String postOutput = "postOutput-" + i;
List<String> expectedTaskOutput = new ArrayList<>();
expectedTaskOutput.add(TASK_OUTPUT);
expectedTaskOutput.add(preOutput);
expectedTaskOutput.add(postOutput);
Task task;
if (createJavaTask) {
JavaTask javaTask = new JavaTask();
javaTask.setExecutableClassName(TestJavaTask.class.getName());
if (forkEnv) {
ForkEnvironment env = new ForkEnvironment();
env.setEnvScript(createScript(forkOutput));
javaTask.setForkEnvironment(env);
expectedTaskOutput.add(forkOutput);
}
task = javaTask;
} else {
NativeTask nativeTask = new NativeTask();
File script = new File(getClass().getResource("/functionaltests/executables/test_echo_task.sh").getFile());
if (!script.exists()) {
Assert.fail("Can't find script " + script.getAbsolutePath());
}
nativeTask.setCommandLine(script.getAbsolutePath());
task = nativeTask;
}
task.setMaxNumberOfExecution(1);
task.setOnTaskError(OnTaskError.CANCEL_JOB);
task.setPreciousLogs(true);
task.setName("Task-" + i);
task.setPreScript(createScript(preOutput));
task.setPostScript(createScript(postOutput));
expectedOutput.put(task.getName(), expectedTaskOutput);
job.addTask(task);
}
JobId jobId = schedulerHelper.testJobSubmission(job);
Scheduler scheduler = schedulerHelper.getSchedulerInterface();
String userURI = scheduler.getUserSpaceURIs().get(0);
String userPath = new File(new URI(userURI)).getAbsolutePath();
JobResult jobResult = scheduler.getJobResult(jobId);
Map<String, TaskResult> results = jobResult.getAllResults();
for (String taskName : expectedOutput.keySet()) {
File taskLog = new File(userPath + "/" + jobId.value(), String.format("TaskLogs-%s-%s.log", jobId.value(), results.get(taskName).getTaskId().value()));
if (!taskLog.exists()) {
Assert.fail("Task log file " + taskLog.getAbsolutePath() + " doesn't exist");
}
String output = new String(FileToBytesConverter.convertFileToByteArray(taskLog));
System.out.println("Log file for " + taskName + ":");
System.out.println(output);
for (String expectedLine : expectedOutput.get(taskName)) {
Assert.assertTrue("Output doesn't contain line " + expectedLine, output.contains(expectedLine));
}
}
}
use of org.ow2.proactive.scheduler.common.task.NativeTask in project scheduling by ow2-proactive.
the class FlatJobFactory method createNativeJobFromCommand.
/**
* Creates a job from a String representing a native command to launch. So job in result is made
* of one native task.
*
* @param command a string representing an executable command to launch.
* @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 just 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 createNativeJobFromCommand(String command, String jobName, String selectionScriptPath, String userName) throws JobCreationException {
if (command == null || "".equalsIgnoreCase(command)) {
throw new JobCreationException("Error, command cannot be null");
}
if (jobName == null) {
jobName = JOB_DEFAULT_NAME_PREFIX + userName;
}
Job nativeJob = new TaskFlowJob();
nativeJob.setName(jobName);
logger.debug("Job : " + nativeJob.getName());
try {
NativeTask t = createNativeTaskFromCommandString(command, "task1", 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;
}
Aggregations