use of org.ow2.proactive.scripting.Script in project scheduling by ow2-proactive.
the class TestTaskAttributes method testScripts.
@Test
public void testScripts() throws Exception {
TaskFlowJob jobDef = new TaskFlowJob();
JavaTask task1 = createDefaultTask("task1");
task1.addSelectionScript(new SelectionScript("selection1", "js", new String[] { "param1", "param2" }, true));
task1.addSelectionScript(new SelectionScript("selection2", "js", new String[] { "param3" }, false));
task1.addSelectionScript(new SelectionScript("selection3", "js"));
task1.setCleaningScript(new SimpleScript("cleanscript", "js", new String[] { "p1", "p2" }));
task1.setPreScript(new SimpleScript("prescript", "js", new String[] { "p1", "p2" }));
task1.setPostScript(new SimpleScript("postscript", "js", new String[] { "p1", "p2" }));
task1.setFlowScript(FlowScript.createContinueFlowScript());
jobDef.addTask(task1);
InternalJob job = defaultSubmitJobAndLoadInternal(true, jobDef);
InternalTask task = job.getTask("task1");
Assert.assertEquals("cleanscript", task.getCleaningScript().getScript());
Assert.assertArrayEquals(new String[] { "p1", "p2" }, task.getCleaningScript().getParameters());
Assert.assertEquals("prescript", task.getPreScript().getScript());
Assert.assertArrayEquals(new String[] { "p1", "p2" }, task.getPreScript().getParameters());
Assert.assertEquals("postscript", task.getPostScript().getScript());
Assert.assertArrayEquals(new String[] { "p1", "p2" }, task.getPostScript().getParameters());
Assert.assertEquals(FlowActionType.CONTINUE.toString(), task.getFlowScript().getActionType());
Assert.assertEquals(3, task.getSelectionScripts().size());
Set<String> scripts = new HashSet<>();
for (SelectionScript script : task.getSelectionScripts()) {
scripts.add(script.getScript());
if (script.getScript().equals("selection1")) {
Assert.assertArrayEquals(new String[] { "param1", "param2" }, script.getParameters());
}
if (script.getScript().equals("selection2")) {
Assert.assertArrayEquals(new String[] { "param3" }, script.getParameters());
}
if (script.getScript().equals("selection3")) {
Assert.assertArrayEquals(new String[] {}, script.getParameters());
}
}
Set<String> expected = new HashSet<>();
expected.add("selection1");
expected.add("selection2");
expected.add("selection3");
Assert.assertEquals(expected, scripts);
}
use of org.ow2.proactive.scripting.Script in project scheduling by ow2-proactive.
the class SchedulingMethodImpl method getNextcompatibleTasks.
/**
* Extract the next compatible tasks from the eligible task list,
*
* Two tasks are compatible according to several criteria such as the same list of selection script,
* the same node tokens, or node exclusions.
* The check of compliance is currently done by the {@link SchedulingTaskComparator} class.<br>
* This method has two side effects : extracted tasks are removed from the bagOfTasks and put in the toFill list
*
* @param bagOfTasks the list of tasks form which to extract tasks
* @param toFill the list that will contains the task to schedule at the end. This list must not be null but must be empty.<br>
* this list will be filled with the first compatible tasks
*
* @return the number of nodes needed to start every task present in the 'toFill' argument at the end of the method.
*/
protected int getNextcompatibleTasks(Map<JobId, JobDescriptor> jobsMap, LinkedList<EligibleTaskDescriptor> bagOfTasks, LinkedList<EligibleTaskDescriptor> toFill) {
if (toFill == null || bagOfTasks == null) {
throw new IllegalArgumentException("The two given lists must not be null !");
}
int totalNeededNodes = 0;
if (PASchedulerProperties.JETTY_STARTED.isSet() && !PASchedulerProperties.JETTY_STARTED.getValueAsBoolean()) {
Iterator<EligibleTaskDescriptor> it = bagOfTasks.iterator();
EligibleTaskDescriptor etd;
while (it.hasNext()) {
etd = it.next();
if (checkEligibleTaskDescriptorScript.isTaskContainsAPIBinding(etd)) {
// skip task here
it.remove();
}
}
}
if (!bagOfTasks.isEmpty()) {
EligibleTaskDescriptor etd = bagOfTasks.removeFirst();
((EligibleTaskDescriptorImpl) etd).addAttempt();
InternalJob currentJob = ((JobDescriptorImpl) jobsMap.get(etd.getJobId())).getInternal();
InternalTask internalTask = currentJob.getIHMTasks().get(etd.getTaskId());
internalTask.updateVariables(schedulingService);
int neededNodes = internalTask.getNumberOfNodesNeeded();
SchedulingTaskComparator referent = new SchedulingTaskComparator(internalTask, currentJob);
boolean firstLoop = true;
do {
if (!firstLoop) {
// if bagOfTasks is not empty
if (!bagOfTasks.isEmpty()) {
etd = bagOfTasks.removeFirst();
((EligibleTaskDescriptorImpl) etd).addAttempt();
currentJob = ((JobDescriptorImpl) jobsMap.get(etd.getJobId())).getInternal();
internalTask = currentJob.getIHMTasks().get(etd.getTaskId());
internalTask.updateVariables(schedulingService);
neededNodes = internalTask.getNumberOfNodesNeeded();
}
} else {
firstLoop = false;
}
// check if the task is compatible with the other previous one
if (referent.equals(new SchedulingTaskComparator(internalTask, currentJob))) {
tlogger.debug(internalTask.getId(), "scheduling");
totalNeededNodes += neededNodes;
toFill.add(etd);
} else {
bagOfTasks.addFirst(etd);
break;
}
} while (!bagOfTasks.isEmpty());
}
return totalNeededNodes;
}
use of org.ow2.proactive.scripting.Script in project scheduling by ow2-proactive.
the class SchedulingTaskComparator method computeHashForSelectionScripts.
private void computeHashForSelectionScripts(InternalTask task, InternalJob job) {
List<SelectionScript> scriptList = SchedulingMethodImpl.resolveScriptVariables(task.getSelectionScripts(), task.getRuntimeVariables());
for (SelectionScript script : scriptList) {
SelectionScript modifiedScript = script;
try {
Map<String, Serializable> bindings = SchedulingMethodImpl.createBindingsForSelectionScripts(job, task, null);
modifiedScript = SchedulingMethodImpl.replaceBindingsInsideScript(script, bindings);
} catch (Exception e) {
logger.error("Error while replacing selection script bindings for task " + task.getId(), e);
}
try {
digests.add(new String(modifiedScript.digest()));
} catch (NoSuchAlgorithmException e) {
logger.error("Error while computing selection script digest for task " + task.getId(), e);
}
}
}
use of org.ow2.proactive.scripting.Script in project scheduling by ow2-proactive.
the class TerminationData method terminateRunningTask.
private void terminateRunningTask(SchedulingService service, TaskTerminationData taskToTerminate, RunningTaskData taskData) {
Map<String, String> genericInformation = new HashMap<>();
VariablesMap variables = null;
if (taskToTerminate.internalJob != null) {
genericInformation = taskData.getTask().getRuntimeGenericInformation();
}
try {
variables = getStringSerializableMap(service, taskToTerminate);
} catch (Exception e) {
logger.error("Exception occurred, fail to get variables into the cleaning script: ", e);
}
try {
if (taskToTerminate.terminationStatus == ABORTED) {
taskData.getLauncher().kill();
}
} catch (Throwable t) {
logger.info("Cannot terminate task launcher for task '" + taskData.getTask().getId() + "'", t);
try {
logger.info("Task launcher that cannot be terminated is identified by " + taskData.getLauncher().toString());
} catch (Throwable ignore) {
logger.info("Getting information about Task launcher failed (remote object not accessible?)");
}
}
try {
logger.debug("Releasing nodes for task '" + taskData.getTask().getId() + "'");
RMProxiesManager proxiesManager = service.getInfrastructure().getRMProxiesManager();
proxiesManager.getUserRMProxy(taskData.getUser(), taskData.getCredentials()).releaseNodes(taskData.getNodes(), getCleaningScript(taskToTerminate, taskData), variables, genericInformation, taskToTerminate.taskData.getTask().getId(), service.addThirdPartyCredentials(taskData.getCredentials()), new SynchronizationWrapper(taskToTerminate.internalJob.getOwner(), taskData.getTask().getId(), taskToTerminate.internalJob.getSynchronizationAPI()), new SignalApiImpl(taskToTerminate.internalJob.getOwner(), taskData.getTask().getId(), taskToTerminate.internalJob.getSynchronizationAPI()));
} catch (Throwable t) {
logger.info("Failed to release nodes for task '" + taskData.getTask().getId() + "'", t);
}
}
use of org.ow2.proactive.scripting.Script in project scheduling by ow2-proactive.
the class ForkedTaskVariablesManagerTest method testScriptParametersAreReplaced.
@Test
public void testScriptParametersAreReplaced() throws InvalidScriptException {
ForkedTaskVariablesManager forkedTaskVariablesManager = new ForkedTaskVariablesManager();
// Create a variable $[something] inside a python script
Serializable[] parameters = new Serializable[] { "$" + testVariable1Key };
Script script = new SimpleScript("print 'hello'", "python", parameters);
// Create a hash map with key as variable name and value as variable value.
Map<String, Serializable> variables = new HashMap<>();
variables.put(testVariable1Key, testVariable1Value);
VariablesMap variablesMap = new VariablesMap();
variablesMap.setInheritedMap(variables);
// Replace that variable inside the script parameters with its value in the hash map
forkedTaskVariablesManager.replaceScriptParameters(script, new HashMap<String, String>(), variablesMap, System.out);
assertThat((String) parameters[0], is(testVariable1Value));
}
Aggregations