Search in sources :

Example 41 with TaskScript

use of org.ow2.proactive.scripting.TaskScript 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);
        }
    }
}
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) TaskScript(org.ow2.proactive.scripting.TaskScript) JobCreationException(org.ow2.proactive.scheduler.common.exception.JobCreationException) JavaTask(org.ow2.proactive.scheduler.common.task.JavaTask) NativeTask(org.ow2.proactive.scheduler.common.task.NativeTask) XMLStreamException(javax.xml.stream.XMLStreamException) JobValidationException(org.ow2.proactive.scheduler.common.exception.JobValidationException) FileNotFoundException(java.io.FileNotFoundException) JobCreationException(org.ow2.proactive.scheduler.common.exception.JobCreationException) VerifierConfigurationException(org.iso_relax.verifier.VerifierConfigurationException) ParallelEnvironment(org.ow2.proactive.scheduler.common.task.ParallelEnvironment) ScriptTask(org.ow2.proactive.scheduler.common.task.ScriptTask) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 42 with TaskScript

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

the class Job2XMLTransformerTest method argumentsInScript.

@Test
public void argumentsInScript() throws Exception {
    File xmlFile = tmpFolder.newFile();
    TaskFlowJob job = new TaskFlowJob();
    job.setName("simpleJob");
    String[] params = { "param1", "param2" };
    SimpleScript script = new SimpleScript("\nprint('arguments[0]='+arguments[0])\n", "javascript", params);
    ScriptTask task = new ScriptTask();
    task.setName("testTask");
    task.setScript(new TaskScript(script));
    job.addTask(task);
    new Job2XMLTransformer().job2xmlFile(job, xmlFile);
    TaskFlowJob recreatedJob = (TaskFlowJob) (JobFactory.getFactory().createJob(xmlFile.getAbsolutePath()));
    Assert.assertEquals("param1", ((ScriptTask) recreatedJob.getTask("testTask")).getScript().getParameters()[0].toString());
    Assert.assertEquals("param2", ((ScriptTask) recreatedJob.getTask("testTask")).getScript().getParameters()[1].toString());
}
Also used : ScriptTask(org.ow2.proactive.scheduler.common.task.ScriptTask) TaskScript(org.ow2.proactive.scripting.TaskScript) TaskFlowJob(org.ow2.proactive.scheduler.common.job.TaskFlowJob) SimpleScript(org.ow2.proactive.scripting.SimpleScript) File(java.io.File) Test(org.junit.Test)

Example 43 with TaskScript

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

the class InProcessTaskExecutorTest method testPaUserVariableAvailabilityFromScriptEngine.

@Test
public void testPaUserVariableAvailabilityFromScriptEngine() throws Throwable {
    TestTaskOutput taskOutput = new TestTaskOutput();
    String jobOwner = "JohnDoe";
    TaskLauncherInitializer initializer = new TaskLauncherInitializer();
    initializer.setJobOwner(jobOwner);
    initializer.setTaskId(TaskIdImpl.createTaskId(JobIdImpl.makeJobId("1000"), "job", 1000L));
    new InProcessTaskExecutor().execute(new TaskContext(new ScriptExecutableContainer(new TaskScript(new SimpleScript("print variables.get('PA_USER')", "python"))), initializer, null, new NodeDataSpacesURIs("", "", "", "", "", ""), "", ""), taskOutput.outputStream, taskOutput.error);
    assertEquals(jobOwner, taskOutput.output().trim());
}
Also used : TaskContext(org.ow2.proactive.scheduler.task.context.TaskContext) TaskScript(org.ow2.proactive.scripting.TaskScript) InProcessTaskExecutor(org.ow2.proactive.scheduler.task.executors.InProcessTaskExecutor) SimpleScript(org.ow2.proactive.scripting.SimpleScript) ScriptExecutableContainer(org.ow2.proactive.scheduler.task.containers.ScriptExecutableContainer) NodeDataSpacesURIs(org.ow2.proactive.scheduler.task.context.NodeDataSpacesURIs) Test(org.junit.Test)

Example 44 with TaskScript

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

the class KillTaskLauncherTest method finished_but_terminate_not_called_back.

@Test
@Repeat(value = repetitions, parallel = parallel, timeout = timeout)
public void finished_but_terminate_not_called_back() throws Throwable {
    final ScriptExecutableContainer executableContainer = new ScriptExecutableContainer(new TaskScript(new SimpleScript("result='done'", "javascript")));
    TaskLauncherInitializer initializer = new TaskLauncherInitializer();
    initializer.setTaskId(TaskIdImpl.createTaskId(JobIdImpl.makeJobId("1000"), "job", 1000L));
    final TaskLauncher taskLauncher = createLauncherWithInjectedMocks(initializer, new TestTaskLauncherFactory(new Semaphore(0)));
    final TaskLauncher taskLauncherPA = PAActiveObject.turnActive(taskLauncher);
    TaskResultWaiter taskResultWaiter = new TaskResultWaiter();
    WaitForResultNotification waitForResultNotification = new WaitForResultNotification(taskResultWaiter);
    waitForResultNotification = PAActiveObject.turnActive(waitForResultNotification);
    taskLauncherPA.doTask(executableContainer, null, waitForResultNotification);
    assertEquals("done", taskResultWaiter.getTaskResult().value());
    assertTaskLauncherIsTerminated(taskLauncherPA);
    PAActiveObject.terminateActiveObject(taskLauncherPA, true);
}
Also used : TaskScript(org.ow2.proactive.scripting.TaskScript) SimpleScript(org.ow2.proactive.scripting.SimpleScript) ScriptExecutableContainer(org.ow2.proactive.scheduler.task.containers.ScriptExecutableContainer) Semaphore(java.util.concurrent.Semaphore) Test(org.junit.Test) Repeat(org.ow2.proactive.utils.Repeat)

Example 45 with TaskScript

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

the class KillTaskLauncherTest method kill_while_sleeping_in_task.

@Test
@Repeat(value = repetitions, parallel = parallel, timeout = timeout)
public void kill_while_sleeping_in_task() throws Exception {
    final ScriptExecutableContainer executableContainer = new ScriptExecutableContainer(new TaskScript(new SimpleScript("java.lang.Thread.sleep(10000)", "javascript")));
    TaskLauncherInitializer initializer = new TaskLauncherInitializer();
    initializer.setTaskId(TaskIdImpl.createTaskId(JobIdImpl.makeJobId("1000"), "job", 1000L));
    Semaphore taskRunning = new Semaphore(0);
    final TaskLauncher taskLauncher = createLauncherWithInjectedMocks(initializer, new TestTaskLauncherFactory(taskRunning));
    final TaskLauncher taskLauncherPA = PAActiveObject.turnActive(taskLauncher);
    taskLauncherPA.doTask(executableContainer, null, null);
    taskRunning.acquire();
    taskLauncherPA.kill();
    assertTaskLauncherIsTerminated(taskLauncherPA);
    PAActiveObject.terminateActiveObject(taskLauncherPA, true);
}
Also used : TaskScript(org.ow2.proactive.scripting.TaskScript) SimpleScript(org.ow2.proactive.scripting.SimpleScript) ScriptExecutableContainer(org.ow2.proactive.scheduler.task.containers.ScriptExecutableContainer) Semaphore(java.util.concurrent.Semaphore) Test(org.junit.Test) Repeat(org.ow2.proactive.utils.Repeat)

Aggregations

TaskScript (org.ow2.proactive.scripting.TaskScript)78 SimpleScript (org.ow2.proactive.scripting.SimpleScript)76 ScriptExecutableContainer (org.ow2.proactive.scheduler.task.containers.ScriptExecutableContainer)64 Test (org.junit.Test)59 NodeDataSpacesURIs (org.ow2.proactive.scheduler.task.context.NodeDataSpacesURIs)31 TaskContext (org.ow2.proactive.scheduler.task.context.TaskContext)31 TaskResult (org.ow2.proactive.scheduler.common.task.TaskResult)19 TaskLauncherInitializer (org.ow2.proactive.scheduler.task.TaskLauncherInitializer)19 InProcessTaskExecutor (org.ow2.proactive.scheduler.task.executors.InProcessTaskExecutor)19 ScriptTask (org.ow2.proactive.scheduler.common.task.ScriptTask)12 TaskFlowJob (org.ow2.proactive.scheduler.common.job.TaskFlowJob)11 Serializable (java.io.Serializable)10 TaskResultImpl (org.ow2.proactive.scheduler.task.TaskResultImpl)10 File (java.io.File)9 JobIdImpl (org.ow2.proactive.scheduler.job.JobIdImpl)9 TestTaskOutput (org.ow2.proactive.scheduler.task.TestTaskOutput)9 ForkedTaskExecutor (org.ow2.proactive.scheduler.task.executors.ForkedTaskExecutor)9 ForkEnvironment (org.ow2.proactive.scheduler.common.task.ForkEnvironment)8 HashMap (java.util.HashMap)6 Semaphore (java.util.concurrent.Semaphore)5