Search in sources :

Example 56 with Script

use of org.ow2.proactive.scripting.Script in project scheduling by ow2-proactive.

the class ProbabilisticSelectionManagerTest method testVariableBindings.

@Test
public void testVariableBindings() throws Exception {
    SelectionScript script = new SelectionScript("variables.get(\"TOTO\")", "groovy", false);
    ManagerObjects managerObjects = new ManagerObjects(1).invoke();
    SelectionManager selectionManager = managerObjects.getSelectionManager();
    ArrayList<RMNode> freeNodes = managerObjects.getFreeNodes();
    Map<String, Serializable> bindings = Collections.singletonMap("TOTO", (Serializable) "value");
    selectionManager.processScriptResult(script, Collections.singletonMap("TOTO", (Serializable) "value"), new ScriptResult<>(true), freeNodes.get(0));
    Assert.assertTrue(selectionManager.isPassed(script, Collections.singletonMap("TOTO", (Serializable) "value"), freeNodes.get(0)));
    Assert.assertFalse(selectionManager.isPassed(script, Collections.singletonMap("TOTO", (Serializable) "differentValue"), freeNodes.get(0)));
    Assert.assertFalse(selectionManager.isPassed(script, Collections.<String, Serializable>emptyMap(), freeNodes.get(0)));
    selectionManager.processScriptResult(script, Collections.singletonMap("TOTO", (Serializable) "differentValue"), new ScriptResult<>(true), freeNodes.get(0));
    Assert.assertTrue(selectionManager.isPassed(script, Collections.singletonMap("TOTO", (Serializable) "differentValue"), freeNodes.get(0)));
    Assert.assertTrue(selectionManager.isPassed(script, Collections.singletonMap("TOTO", (Serializable) "value"), freeNodes.get(0)));
}
Also used : SelectionManager(org.ow2.proactive.resourcemanager.selection.SelectionManager) SelectionScript(org.ow2.proactive.scripting.SelectionScript) RMNode(org.ow2.proactive.resourcemanager.rmnode.RMNode) Serializable(java.io.Serializable) SelectionManagerTest(org.ow2.proactive.resourcemanager.selection.SelectionManagerTest) Test(org.junit.Test)

Example 57 with Script

use of org.ow2.proactive.scripting.Script 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;
}
Also used : TaskFlowJob(org.ow2.proactive.scheduler.common.job.TaskFlowJob) JobCreationException(org.ow2.proactive.scheduler.common.exception.JobCreationException) NativeTask(org.ow2.proactive.scheduler.common.task.NativeTask) Job(org.ow2.proactive.scheduler.common.job.Job) TaskFlowJob(org.ow2.proactive.scheduler.common.job.TaskFlowJob) JobCreationException(org.ow2.proactive.scheduler.common.exception.JobCreationException) InvalidScriptException(org.ow2.proactive.scripting.InvalidScriptException)

Example 58 with Script

use of org.ow2.proactive.scripting.Script 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;
}
Also used : TaskFlowJob(org.ow2.proactive.scheduler.common.job.TaskFlowJob) ArrayList(java.util.ArrayList) JobCreationException(org.ow2.proactive.scheduler.common.exception.JobCreationException) NativeTask(org.ow2.proactive.scheduler.common.task.NativeTask) JobCreationException(org.ow2.proactive.scheduler.common.exception.JobCreationException) InvalidScriptException(org.ow2.proactive.scripting.InvalidScriptException) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) Job(org.ow2.proactive.scheduler.common.job.Job) TaskFlowJob(org.ow2.proactive.scheduler.common.job.TaskFlowJob) File(java.io.File) NumberFormat(java.text.NumberFormat)

Example 59 with Script

use of org.ow2.proactive.scripting.Script 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;
}
Also used : SelectionScript(org.ow2.proactive.scripting.SelectionScript) SimpleScript(org.ow2.proactive.scripting.SimpleScript) NativeTask(org.ow2.proactive.scheduler.common.task.NativeTask) File(java.io.File)

Example 60 with Script

use of org.ow2.proactive.scripting.Script 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;
}
Also used : Task(org.ow2.proactive.scheduler.common.task.Task) JavaTask(org.ow2.proactive.scheduler.common.task.JavaTask) NativeTask(org.ow2.proactive.scheduler.common.task.NativeTask) ScriptTask(org.ow2.proactive.scheduler.common.task.ScriptTask) SelectionScript(org.ow2.proactive.scripting.SelectionScript) ScriptTask(org.ow2.proactive.scheduler.common.task.ScriptTask) JavaTask(org.ow2.proactive.scheduler.common.task.JavaTask) NativeTask(org.ow2.proactive.scheduler.common.task.NativeTask) HashSet(java.util.HashSet)

Aggregations

Test (org.junit.Test)38 SelectionScript (org.ow2.proactive.scripting.SelectionScript)32 File (java.io.File)31 SimpleScript (org.ow2.proactive.scripting.SimpleScript)28 TaskFlowJob (org.ow2.proactive.scheduler.common.job.TaskFlowJob)25 ArrayList (java.util.ArrayList)16 JavaTask (org.ow2.proactive.scheduler.common.task.JavaTask)15 Script (org.ow2.proactive.scripting.Script)15 ScriptResult (org.ow2.proactive.scripting.ScriptResult)15 FlowScript (org.ow2.proactive.scheduler.common.task.flow.FlowScript)13 HashMap (java.util.HashMap)12 RMNode (org.ow2.proactive.resourcemanager.rmnode.RMNode)11 IOException (java.io.IOException)10 JobId (org.ow2.proactive.scheduler.common.job.JobId)10 NodeSet (org.ow2.proactive.utils.NodeSet)10 RMFunctionalTest (functionaltests.utils.RMFunctionalTest)9 ResourceManager (org.ow2.proactive.resourcemanager.frontend.ResourceManager)9 Job (org.ow2.proactive.scheduler.common.job.Job)8 ForkEnvironment (org.ow2.proactive.scheduler.common.task.ForkEnvironment)8 Serializable (java.io.Serializable)7