Search in sources :

Example 6 with Script

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

the class FlowScript method getResult.

@Override
protected ScriptResult<FlowAction> getResult(Object evalResult, Bindings bindings) {
    try {
        FlowAction act = new FlowAction();
        /*
             * no action defined
             */
        if (this.actionType == null || this.actionType.equals(FlowActionType.CONTINUE.toString())) {
            act.setType(FlowActionType.CONTINUE);
        } else /*
             * loop
             */
        if (this.actionType.equals(FlowActionType.LOOP.toString())) {
            if (this.target == null) {
                String msg = "LOOP control flow action requires a target";
                logger.error(msg);
                return new ScriptResult<FlowAction>(new Exception(msg));
            } else {
                if (bindings.containsKey(loopVariable)) {
                    Boolean enabled;
                    String loopValue = bindings.get(loopVariable).toString();
                    if ("true".equalsIgnoreCase(loopValue)) {
                        enabled = Boolean.TRUE;
                    } else if ("false".equalsIgnoreCase(loopValue)) {
                        enabled = Boolean.FALSE;
                    } else {
                        try {
                            (new Predictor(loopValue)).nextMatchingDate();
                            enabled = Boolean.TRUE;
                            act.setCronExpr(loopValue);
                        } catch (InvalidPatternException e) {
                            enabled = Boolean.FALSE;
                        }
                    }
                    if (enabled) {
                        act.setType(FlowActionType.LOOP);
                        act.setTarget(this.target);
                    } else {
                        act.setType(FlowActionType.CONTINUE);
                    }
                } else {
                    String msg = "Script environment for LOOP action needs to define variable " + loopVariable;
                    logger.error(msg);
                    return new ScriptResult<FlowAction>(new Exception(msg));
                }
            }
        } else /*
             * replicate
             */
        if (this.actionType.equals(FlowActionType.REPLICATE.toString())) {
            if (bindings.containsKey(replicateRunsVariable)) {
                act.setType(FlowActionType.REPLICATE);
                int args = 1;
                Object o = bindings.get(replicateRunsVariable);
                try {
                    args = Integer.parseInt("" + o);
                } catch (NumberFormatException e) {
                    try {
                        args = (int) Math.floor(Double.parseDouble("" + o));
                    } catch (Exception e2) {
                        String msg = "REPLICATE action: could not parse value for variable " + replicateRunsVariable;
                        logger.error(msg);
                        return new ScriptResult<FlowAction>(new Exception(msg, e2));
                    }
                }
                if (args < 0) {
                    String msg = "REPLICATE action: value of variable " + replicateRunsVariable + " cannot be negative";
                    logger.error(msg);
                    return new ScriptResult<FlowAction>(new Exception(msg));
                }
                act.setDupNumber(args);
            } else {
                String msg = "Script environment for REPLICATE action needs to define variable " + replicateRunsVariable;
                logger.error(msg);
                return new ScriptResult<FlowAction>(new Exception(msg));
            }
        } else /*
             * if
             */
        if (this.actionType.equals(FlowActionType.IF.toString())) {
            if (this.target == null) {
                String msg = "IF action requires a target ";
                logger.error(msg);
                return new ScriptResult<FlowAction>(new Exception(msg));
            } else if (this.targetElse == null) {
                String msg = "IF action requires an ELSE target ";
                logger.error(msg);
                return new ScriptResult<FlowAction>(new Exception(msg));
            } else {
                act.setType(FlowActionType.IF);
                if (bindings.containsKey(branchSelectionVariable)) {
                    String val = new String((String) bindings.get(branchSelectionVariable));
                    if (val.toLowerCase().equals(ifBranchSelectedVariable)) {
                        act.setTarget(this.target);
                        act.setTargetElse(this.targetElse);
                    } else if (val.toLowerCase().equals(elseBranchSelectedVariable)) {
                        act.setTarget(this.targetElse);
                        act.setTargetElse(this.target);
                    } else {
                        String msg = "IF action: value for " + branchSelectionVariable + " needs to be one of " + ifBranchSelectedVariable + " or " + elseBranchSelectedVariable;
                        logger.error(msg);
                        return new ScriptResult<FlowAction>(new Exception(msg));
                    }
                } else {
                    String msg = "Environment for IF action needs to define variable " + branchSelectionVariable;
                    logger.error(msg);
                    return new ScriptResult<FlowAction>(new Exception(msg));
                }
                if (this.targetContinuation != null) {
                    act.setTargetContinuation(this.targetContinuation);
                }
            }
        } else /*
             * unknown action
             */
        {
            String msg = actionType + " action type unknown";
            logger.error(msg);
            return new ScriptResult<FlowAction>(new Exception(msg));
        }
        return new ScriptResult<FlowAction>(act);
    } catch (Throwable th) {
        return new ScriptResult<FlowAction>(th);
    }
}
Also used : ScriptResult(org.ow2.proactive.scripting.ScriptResult) InvalidPatternException(it.sauronsoftware.cron4j.InvalidPatternException) Predictor(it.sauronsoftware.cron4j.Predictor) InvalidScriptException(org.ow2.proactive.scripting.InvalidScriptException) InvalidPatternException(it.sauronsoftware.cron4j.InvalidPatternException)

Example 7 with Script

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

the class ForkedProcessBuilderCreator method executeForkEnvironmentScriptAndExtractVariables.

private ScriptResult executeForkEnvironmentScriptAndExtractVariables(TaskContext context, PrintStream outputSink, PrintStream errorSink, OSProcessBuilder processBuilder) throws Exception {
    ScriptResult forkEnvironmentScriptResult = null;
    ForkEnvironment forkEnvironment = context.getInitializer().getForkEnvironment();
    if (forkEnvironment != null) {
        if (forkEnvironment.getEnvScript() != null) {
            if (!context.getInitializer().isAuthorizedForkEnvironmentScript()) {
                throw new SecurityException("Unauthorized fork environment script: " + System.getProperty("line.separator") + forkEnvironment.getEnvScript().fetchScript());
            }
            forkEnvironmentScriptResult = forkEnvironmentScriptExecutor.executeForkEnvironmentScript(context, outputSink, errorSink);
        }
        try {
            processBuilder.environment().putAll(// by existing environment variables, variables and credentials
            taskContextVariableExtractor.extractVariablesThirdPartyCredentialsAndSystemEnvironmentVariables(context));
        } catch (IllegalArgumentException processEnvironmentReadOnly) {
            throw new IllegalStateException("Cannot use runAsMe mode and set system environment properties", processEnvironmentReadOnly);
        }
    }
    return forkEnvironmentScriptResult;
}
Also used : ScriptResult(org.ow2.proactive.scripting.ScriptResult) ForkEnvironment(org.ow2.proactive.scheduler.common.task.ForkEnvironment)

Example 8 with Script

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

the class InProcessTaskExecutorTest method storePreScriptAbsolute.

@Test
public void storePreScriptAbsolute() throws Throwable {
    TestTaskOutput taskOutput = new TestTaskOutput();
    TaskLauncherInitializer initializer = new TaskLauncherInitializer();
    initializer.setPreScript(new SimpleScript("println('pre')", "groovy"));
    initializer.setPostScript(new SimpleScript("println('post')", "groovy"));
    Map<String, String> genericInfo = new HashMap<>();
    File file = File.createTempFile("test", ".py");
    genericInfo.put("PRE_SCRIPT_AS_FILE", file.getAbsolutePath());
    file.delete();
    initializer.setGenericInformation(genericInfo);
    initializer.setTaskId(TaskIdImpl.createTaskId(JobIdImpl.makeJobId("1000"), "job", 1000L));
    TaskResultImpl result = new InProcessTaskExecutor().execute(new TaskContext(new ScriptExecutableContainer(new TaskScript(new SimpleScript("println('hello'); java.lang.Thread.sleep(5); result='hello'", "groovy"))), initializer, null, new NodeDataSpacesURIs("", "", "", "", "", ""), "", new NodeInfo("", "", "", "")), taskOutput.outputStream, taskOutput.error);
    // Make sure that the execution of the pre-script is skipped
    assertEquals(String.format("hello%npost%n"), taskOutput.output());
    assertEquals("hello", result.value());
    // Make sure that the pre-script is stored in a file
    assertTrue(file.exists());
    String content = new String(Files.readAllBytes(Paths.get(file.getAbsolutePath())));
    assertEquals("println('pre')", content);
    file.delete();
}
Also used : TaskContext(org.ow2.proactive.scheduler.task.context.TaskContext) TaskScript(org.ow2.proactive.scripting.TaskScript) InProcessTaskExecutor(org.ow2.proactive.scheduler.task.executors.InProcessTaskExecutor) HashMap(java.util.HashMap) SimpleScript(org.ow2.proactive.scripting.SimpleScript) ScriptExecutableContainer(org.ow2.proactive.scheduler.task.containers.ScriptExecutableContainer) NodeDataSpacesURIs(org.ow2.proactive.scheduler.task.context.NodeDataSpacesURIs) NodeInfo(org.ow2.proactive.scheduler.task.context.NodeInfo) File(java.io.File) Test(org.junit.Test)

Example 9 with Script

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

the class InProcessTaskExecutorTest method storePreScriptWithoutExtension.

@Test
public void storePreScriptWithoutExtension() throws Throwable {
    TestTaskOutput taskOutput = new TestTaskOutput();
    TaskLauncherInitializer initializer = new TaskLauncherInitializer();
    initializer.setPreScript(new SimpleScript("println('pre')", "groovy"));
    initializer.setPostScript(new SimpleScript("println('post')", "groovy"));
    Map<String, String> genericInfo = new HashMap<>();
    File file = File.createTempFile("test", "");
    genericInfo.put("PRE_SCRIPT_AS_FILE", file.getAbsolutePath());
    file.delete();
    initializer.setGenericInformation(genericInfo);
    initializer.setTaskId(TaskIdImpl.createTaskId(JobIdImpl.makeJobId("1000"), "job", 1000L));
    TaskResultImpl result = new InProcessTaskExecutor().execute(new TaskContext(new ScriptExecutableContainer(new TaskScript(new SimpleScript("println('hello'); java.lang.Thread.sleep(5); result='hello'", "groovy"))), initializer, null, new NodeDataSpacesURIs("", "", "", "", "", ""), "", new NodeInfo("", "", "", "")), taskOutput.outputStream, taskOutput.error);
    // Make sure that the execution of the pre-script is skipped
    assertEquals(String.format("hello%npost%n"), taskOutput.output());
    assertEquals("hello", result.value());
    // Make sure that the pre-script is stored in a file
    file = new File(file.getAbsolutePath() + ".groovy");
    assertTrue(file.exists());
    String content = new String(Files.readAllBytes(Paths.get(file.getAbsolutePath())));
    assertEquals("println('pre')", content);
    file.delete();
}
Also used : TaskContext(org.ow2.proactive.scheduler.task.context.TaskContext) TaskScript(org.ow2.proactive.scripting.TaskScript) InProcessTaskExecutor(org.ow2.proactive.scheduler.task.executors.InProcessTaskExecutor) HashMap(java.util.HashMap) SimpleScript(org.ow2.proactive.scripting.SimpleScript) ScriptExecutableContainer(org.ow2.proactive.scheduler.task.containers.ScriptExecutableContainer) NodeDataSpacesURIs(org.ow2.proactive.scheduler.task.context.NodeDataSpacesURIs) NodeInfo(org.ow2.proactive.scheduler.task.context.NodeInfo) File(java.io.File) Test(org.junit.Test)

Example 10 with Script

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

the class InProcessTaskExecutorTest method contextVariables_index.

@Test
public void contextVariables_index() throws Throwable {
    TestTaskOutput taskOutput = new TestTaskOutput();
    TaskLauncherInitializer initializer = new TaskLauncherInitializer();
    initializer.setReplicationIndex(7);
    initializer.setIterationIndex(6);
    String script = "result = variables.get('PA_TASK_ITERATION') * variables.get('PA_TASK_REPLICATION')";
    initializer.setTaskId(TaskIdImpl.createTaskId(new JobIdImpl(1000, "job"), "task", 42L));
    TaskResultImpl result = new InProcessTaskExecutor().execute(new TaskContext(new ScriptExecutableContainer(new TaskScript(new SimpleScript(script, "groovy"))), initializer, null, new NodeDataSpacesURIs("", "", "", "", "", ""), "", new NodeInfo("", "", "", "")), taskOutput.outputStream, taskOutput.error);
    assertEquals(42, result.value());
}
Also used : TaskContext(org.ow2.proactive.scheduler.task.context.TaskContext) TaskScript(org.ow2.proactive.scripting.TaskScript) InProcessTaskExecutor(org.ow2.proactive.scheduler.task.executors.InProcessTaskExecutor) NodeInfo(org.ow2.proactive.scheduler.task.context.NodeInfo) SimpleScript(org.ow2.proactive.scripting.SimpleScript) ScriptExecutableContainer(org.ow2.proactive.scheduler.task.containers.ScriptExecutableContainer) JobIdImpl(org.ow2.proactive.scheduler.job.JobIdImpl) NodeDataSpacesURIs(org.ow2.proactive.scheduler.task.context.NodeDataSpacesURIs) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)46 SelectionScript (org.ow2.proactive.scripting.SelectionScript)40 File (java.io.File)38 SimpleScript (org.ow2.proactive.scripting.SimpleScript)33 TaskFlowJob (org.ow2.proactive.scheduler.common.job.TaskFlowJob)27 ScriptResult (org.ow2.proactive.scripting.ScriptResult)21 IOException (java.io.IOException)18 HashMap (java.util.HashMap)18 JavaTask (org.ow2.proactive.scheduler.common.task.JavaTask)16 Script (org.ow2.proactive.scripting.Script)16 JobId (org.ow2.proactive.scheduler.common.job.JobId)13 FlowScript (org.ow2.proactive.scheduler.common.task.flow.FlowScript)13 ArrayList (java.util.ArrayList)12 RMNode (org.ow2.proactive.resourcemanager.rmnode.RMNode)12 Serializable (java.io.Serializable)11 TaskScript (org.ow2.proactive.scripting.TaskScript)11 NodeSet (org.ow2.proactive.utils.NodeSet)11 RMFunctionalTest (functionaltests.utils.RMFunctionalTest)10 ResourceManager (org.ow2.proactive.resourcemanager.frontend.ResourceManager)10 ForkEnvironment (org.ow2.proactive.scheduler.common.task.ForkEnvironment)9