Search in sources :

Example 11 with FlowScript

use of org.ow2.proactive.scheduler.common.task.flow.FlowScript in project scheduling by ow2-proactive.

the class StaxJobFactory method createControlFlowScript.

private FlowScript createControlFlowScript(XMLStreamReader cursorTask, Task tmpTask, Map<String, String> variables) throws JobCreationException {
    String type = null;
    String target = null;
    String targetElse = null;
    String targetJoin = null;
    int event = -1;
    for (int i = 0; i < cursorTask.getAttributeCount(); i++) {
        String attrName = cursorTask.getAttributeLocalName(i);
        if (XMLAttributes.FLOW_BLOCK.matches(attrName)) {
            tmpTask.setFlowBlock(FlowBlock.parse(replace(cursorTask.getAttributeValue(i), variables)));
        }
    }
    // <control>  =>  <if> | <loop> | <replicate>
    try {
        while (cursorTask.hasNext()) {
            event = cursorTask.next();
            if (event == XMLEvent.START_ELEMENT) {
                break;
            } else if (event == XMLEvent.END_ELEMENT && XMLTags.FLOW.matches(cursorTask.getLocalName())) {
                return null;
            }
        }
    } catch (Exception e) {
        throw new JobCreationException(XMLTags.FLOW.getXMLName(), null, e);
    }
    if (event != XMLEvent.START_ELEMENT) {
        throw new JobCreationException(XMLTags.FLOW.getXMLName(), null, null);
    }
    String tag = null;
    // REPLICATE: no attribute
    if (XMLTags.FLOW_REPLICATE.matches(cursorTask.getLocalName())) {
        type = FlowActionType.REPLICATE.toString();
        tag = XMLTags.FLOW_REPLICATE.getXMLName();
    } else // IF: attributes TARGET_IF and TARGET_ELSE and TARGET_JOIN
    if (XMLTags.FLOW_IF.matches(cursorTask.getLocalName())) {
        type = FlowActionType.IF.toString();
        tag = XMLTags.FLOW_IF.getXMLName();
        for (int i = 0; i < cursorTask.getAttributeCount(); i++) {
            String attrName = cursorTask.getAttributeLocalName(i);
            if (XMLAttributes.FLOW_TARGET.matches(attrName)) {
                target = cursorTask.getAttributeValue(i);
            } else if (XMLAttributes.FLOW_ELSE.matches(attrName)) {
                targetElse = cursorTask.getAttributeValue(i);
            } else if (XMLAttributes.FLOW_CONTINUATION.matches(attrName)) {
                targetJoin = cursorTask.getAttributeValue(i);
            }
        }
    } else // LOOP: attribute TARGET
    if (XMLTags.FLOW_LOOP.matches(cursorTask.getLocalName())) {
        type = FlowActionType.LOOP.toString();
        tag = XMLTags.FLOW_LOOP.getXMLName();
        for (int i = 0; i < cursorTask.getAttributeCount(); i++) {
            String attrName = cursorTask.getAttributeLocalName(i);
            if (XMLAttributes.FLOW_TARGET.matches(attrName)) {
                target = cursorTask.getAttributeValue(i);
            }
        }
    }
    FlowScript sc = null;
    Script<?> internalScript;
    try {
        internalScript = createScript(cursorTask, ScriptType.FLOW, variables);
        switch(FlowActionType.parse(type)) {
            case IF:
                sc = FlowScript.createIfFlowScript(internalScript, target, targetElse, targetJoin);
                break;
            case REPLICATE:
                sc = FlowScript.createReplicateFlowScript(internalScript);
                break;
            case LOOP:
                sc = FlowScript.createLoopFlowScript(internalScript, target);
                break;
            default:
                break;
        }
    } catch (Exception e) {
        throw new JobCreationException(tag, null, e);
    }
    // </script>  -->  </if> | </replicate> | </loop>
    try {
        while (cursorTask.hasNext()) {
            event = cursorTask.next();
            if (event == XMLEvent.END_ELEMENT) {
                break;
            }
        }
    } catch (XMLStreamException e) {
        throw new JobCreationException(tag, null, e);
    }
    if (event != XMLEvent.END_ELEMENT) {
        throw new JobCreationException(tag, null, null);
    }
    return sc;
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) JobCreationException(org.ow2.proactive.scheduler.common.exception.JobCreationException) 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) FlowScript(org.ow2.proactive.scheduler.common.task.flow.FlowScript)

Example 12 with FlowScript

use of org.ow2.proactive.scheduler.common.task.flow.FlowScript in project scheduling by ow2-proactive.

the class FlowScript method createContinueFlowScript.

public static FlowScript createContinueFlowScript() throws InvalidScriptException {
    FlowScript fs = new FlowScript(new SimpleScript("", "javascript"));
    fs.setActionType(FlowActionType.CONTINUE);
    return fs;
}
Also used : SimpleScript(org.ow2.proactive.scripting.SimpleScript)

Example 13 with FlowScript

use of org.ow2.proactive.scheduler.common.task.flow.FlowScript in project scheduling by ow2-proactive.

the class TestWorkflowFailedScript method testIf.

/**
 * Creates  a job with 3 tasks : A B C
 * Workflow : A  -> if (B) else (C)
 * Flowscript on A throws an Exception
 * B is supposed to be executed as cancelJobOnError==false
 * C is supposed to be skipped as it is the default ELSE branch
 * A is supposed to provide a task result value containing an exception
 */
private void testIf() throws Throwable {
    TaskFlowJob job = new TaskFlowJob();
    job.setName(this.getClass().getSimpleName());
    job.setMaxNumberOfExecution(1);
    job.setOnTaskError(OnTaskError.CONTINUE_JOB_EXECUTION);
    JavaTask A = new JavaTask();
    A.setOnTaskError(OnTaskError.NONE);
    A.setMaxNumberOfExecution(1);
    A.setName("A");
    A.setExecutableClassName("org.ow2.proactive.scheduler.examples.EmptyTask");
    FlowScript ifScript = FlowScript.createIfFlowScript(ifScriptContent, "B", "C", null);
    A.setFlowScript(ifScript);
    job.addTask(A);
    JavaTask B = new JavaTask();
    B.setName("B");
    B.setExecutableClassName("org.ow2.proactive.scheduler.examples.EmptyTask");
    job.addTask(B);
    JavaTask C = new JavaTask();
    C.setName("C");
    C.setExecutableClassName("org.ow2.proactive.scheduler.examples.EmptyTask");
    job.addTask(C);
    JobId id = schedulerHelper.submitJob(job);
    schedulerHelper.waitForEventJobFinished(id);
    JobResult res = schedulerHelper.getJobResult(id);
    Map<String, TaskResult> results = res.getAllResults();
    // tasks A and B should produce a result, C should be SKIPPED
    Assert.assertTrue("Expected 2 results, got " + results.size(), results.size() == 2);
    for (Entry<String, TaskResult> result : results.entrySet()) {
        if (result.getKey().equals("A")) {
            // A should produce an exception
            Assert.assertTrue("Task " + result.getKey() + " should have had an exception!", result.getValue().hadException());
        } else {
            // task should be B and not C
            Assert.assertTrue("Expected result for task B, got task " + result.getKey(), result.getKey().equals("B"));
            // Task B should run without exception
            Assert.assertFalse("Task " + result.getKey() + " had an exception!", result.getValue().hadException());
        }
    }
}
Also used : JobResult(org.ow2.proactive.scheduler.common.job.JobResult) TaskFlowJob(org.ow2.proactive.scheduler.common.job.TaskFlowJob) TaskResult(org.ow2.proactive.scheduler.common.task.TaskResult) JavaTask(org.ow2.proactive.scheduler.common.task.JavaTask) JobId(org.ow2.proactive.scheduler.common.job.JobId) FlowScript(org.ow2.proactive.scheduler.common.task.flow.FlowScript)

Example 14 with FlowScript

use of org.ow2.proactive.scheduler.common.task.flow.FlowScript in project scheduling by ow2-proactive.

the class CheckEligibleTaskDescriptorScriptTest method testOnlyCleanScriptContainsAPIBinding.

@Test
public void testOnlyCleanScriptContainsAPIBinding() throws InvalidScriptException {
    Script s = scriptWithApiBindingClient();
    Script s2 = scriptWithoutApiBinding();
    FlowScript fs = flowScriptWithoutApiBinding();
    Mockito.when(it.getPreScript()).thenReturn(s2);
    Mockito.when(it.getPostScript()).thenReturn(s2);
    Mockito.when(it.getCleaningScript()).thenReturn(s);
    Mockito.when(sec.getScript()).thenReturn(s2);
    Mockito.when(fe.getEnvScript()).thenReturn(s2);
    Mockito.when(it.getFlowScript()).thenReturn(fs);
    assertTrue(new CheckEligibleTaskDescriptorScript().isTaskContainsAPIBinding(etd));
}
Also used : SimpleScript(org.ow2.proactive.scripting.SimpleScript) FlowScript(org.ow2.proactive.scheduler.common.task.flow.FlowScript) Script(org.ow2.proactive.scripting.Script) FlowScript(org.ow2.proactive.scheduler.common.task.flow.FlowScript) Test(org.junit.Test)

Example 15 with FlowScript

use of org.ow2.proactive.scheduler.common.task.flow.FlowScript in project scheduling by ow2-proactive.

the class CheckEligibleTaskDescriptorScriptTest method testOnlyFlowScriptContainsAPIBinding.

@Test
public void testOnlyFlowScriptContainsAPIBinding() throws InvalidScriptException {
    Script s = scriptWithoutApiBinding();
    FlowScript fs = flowScriptWithApiBindingUser();
    Mockito.when(it.getPreScript()).thenReturn(s);
    Mockito.when(it.getPostScript()).thenReturn(s);
    Mockito.when(it.getCleaningScript()).thenReturn(s);
    Mockito.when(fe.getEnvScript()).thenReturn(s);
    Mockito.when(sec.getScript()).thenReturn(s);
    Mockito.when(it.getFlowScript()).thenReturn(fs);
    assertTrue(new CheckEligibleTaskDescriptorScript().isTaskContainsAPIBinding(etd));
}
Also used : SimpleScript(org.ow2.proactive.scripting.SimpleScript) FlowScript(org.ow2.proactive.scheduler.common.task.flow.FlowScript) Script(org.ow2.proactive.scripting.Script) FlowScript(org.ow2.proactive.scheduler.common.task.flow.FlowScript) Test(org.junit.Test)

Aggregations

FlowScript (org.ow2.proactive.scheduler.common.task.flow.FlowScript)18 SimpleScript (org.ow2.proactive.scripting.SimpleScript)11 Test (org.junit.Test)9 Script (org.ow2.proactive.scripting.Script)9 TaskFlowJob (org.ow2.proactive.scheduler.common.job.TaskFlowJob)4 JavaTask (org.ow2.proactive.scheduler.common.task.JavaTask)4 InternalScriptTask (org.ow2.proactive.scheduler.task.internal.InternalScriptTask)2 InternalTask (org.ow2.proactive.scheduler.task.internal.InternalTask)2 EmptyExecutable (functionaltests.executables.EmptyExecutable)1 InvalidPatternException (it.sauronsoftware.cron4j.InvalidPatternException)1 Predictor (it.sauronsoftware.cron4j.Predictor)1 FileNotFoundException (java.io.FileNotFoundException)1 Date (java.util.Date)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 VerifierConfigurationException (org.iso_relax.verifier.VerifierConfigurationException)1 JobCreationException (org.ow2.proactive.scheduler.common.exception.JobCreationException)1 JobValidationException (org.ow2.proactive.scheduler.common.exception.JobValidationException)1 JobId (org.ow2.proactive.scheduler.common.job.JobId)1 JobResult (org.ow2.proactive.scheduler.common.job.JobResult)1 ForkEnvironment (org.ow2.proactive.scheduler.common.task.ForkEnvironment)1