Search in sources :

Example 6 with ForkEnvironment

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

the class StaxJobFactory method createForkEnvironment.

/**
 * Create the forkEnvironment of a java task
 * The cursor is currently at the beginning of the 'FORK_ENVIRONMENT' tag.
 *
 * @param cursorExec the streamReader with the cursor on the 'FORK_ENVIRONMENT' tag.
 * @return The created ForkEnvironment
 */
private ForkEnvironment createForkEnvironment(XMLStreamReader cursorExec, Map<String, String> variables) throws JobCreationException {
    ForkEnvironment forkEnv = new ForkEnvironment();
    int i = 0;
    String current = cursorExec.getLocalName();
    try {
        // parsing executable attributes
        int attrCount = cursorExec.getAttributeCount();
        for (i = 0; i < attrCount; i++) {
            String attrName = cursorExec.getAttributeLocalName(i);
            if (XMLAttributes.FORK_JAVA_HOME.matches(attrName)) {
                forkEnv.setJavaHome(replace(cursorExec.getAttributeValue(i), variables));
            }
            if (XMLAttributes.TASK_WORKDING_DIR.matches(attrName)) {
                forkEnv.setWorkingDir(replace(cursorExec.getAttributeValue(i), variables));
            }
        }
        // parsing executable tags
        int eventType;
        while (cursorExec.hasNext()) {
            eventType = cursorExec.next();
            switch(eventType) {
                case XMLEvent.START_ELEMENT:
                    current = cursorExec.getLocalName();
                    if (XMLTags.FORK_SYSTEM_PROPERTY.matches(current)) {
                        attrCount = cursorExec.getAttributeCount();
                        String name = null, value = null;
                        for (i = 0; i < attrCount; i++) {
                            String attrName = cursorExec.getAttributeLocalName(i);
                            if (XMLAttributes.COMMON_NAME.matches(attrName)) {
                                name = replace(cursorExec.getAttributeValue(i), variables);
                            }
                            if (XMLAttributes.COMMON_VALUE.matches(attrName)) {
                                value = replace(cursorExec.getAttributeValue(i), variables);
                            }
                        }
                        forkEnv.addSystemEnvironmentVariable(name, value);
                    } else if (XMLTags.FORK_JVM_ARG.matches(current)) {
                        forkEnv.addJVMArgument(replace(cursorExec.getAttributeValue(0), variables));
                    } else if (XMLTags.JOB_PATH_ELEMENT.matches(current)) {
                        forkEnv.addAdditionalClasspath(replace(cursorExec.getAttributeValue(0), variables));
                    } else if (XMLTags.SCRIPT_ENV.matches(current)) {
                        forkEnv.setEnvScript(new ForkEnvironmentScript(createScript(cursorExec, variables)));
                    }
                    break;
                case XMLEvent.END_ELEMENT:
                    if (XMLTags.FORK_ENVIRONMENT.matches(cursorExec.getLocalName())) {
                        return forkEnv;
                    }
                    break;
            }
        }
        return forkEnv;
    } catch (JobCreationException jce) {
        jce.pushTag(current);
        throw jce;
    } catch (Exception e) {
        String attrtmp = null;
        if (cursorExec.isStartElement() && cursorExec.getAttributeCount() > 0) {
            attrtmp = cursorExec.getAttributeLocalName(i);
        }
        throw new JobCreationException(current, attrtmp, e);
    }
}
Also used : JobCreationException(org.ow2.proactive.scheduler.common.exception.JobCreationException) ForkEnvironment(org.ow2.proactive.scheduler.common.task.ForkEnvironment) ForkEnvironmentScript(org.ow2.proactive.scripting.ForkEnvironmentScript) 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)

Example 7 with ForkEnvironment

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

the class StaxJobFactory method setNativeExecutable.

/**
 * Add the Native Executable to this native Task.
 * The cursor is currently at the beginning of the 'ELEMENT_NATIVE_EXECUTABLE' tag.
 *
 * @param nativeTask the task in which to add the Native Executable.
 * @param cursorExec the streamReader with the cursor on the 'ELEMENT_NATIVE_EXECUTABLE' tag.
 */
private void setNativeExecutable(NativeTask nativeTask, XMLStreamReader cursorExec) throws JobCreationException {
    int i = 0;
    String current = null;
    try {
        // one step ahead to go to the command (static or dynamic)
        while (cursorExec.next() != XMLEvent.START_ELEMENT) ;
        current = cursorExec.getLocalName();
        ArrayList<String> command = new ArrayList<>(0);
        if (XMLTags.NATIVE_TASK_STATIC_COMMAND.matches(cursorExec.getLocalName())) {
            String attr_ = null;
            String current_ = null;
            try {
                for (i = 0; i < cursorExec.getAttributeCount(); i++) {
                    String attrName = cursorExec.getAttributeLocalName(i);
                    attr_ = attrName;
                    if (XMLAttributes.TASK_COMMAND_VALUE.matches(attrName)) {
                        command.add((cursorExec.getAttributeValue(i)));
                    }
                    if (XMLAttributes.TASK_WORKDING_DIR.matches(attrName)) {
                        logger.warn(XMLAttributes.TASK_WORKDING_DIR.getXMLName() + " attribute no longer supported. Please use a forkEnvironment for defining a working directory.");
                    }
                }
                int eventType;
                while (cursorExec.hasNext()) {
                    eventType = cursorExec.next();
                    switch(eventType) {
                        case XMLEvent.START_ELEMENT:
                            current_ = cursorExec.getLocalName();
                            if (XMLTags.SCRIPT_ARGUMENT.matches(cursorExec.getLocalName())) {
                                command.add((cursorExec.getAttributeValue(0)));
                            }
                            break;
                        case XMLEvent.END_ELEMENT:
                            if (XMLTags.NATIVE_EXECUTABLE.matches(cursorExec.getLocalName())) {
                                nativeTask.setCommandLine(command.toArray(new String[command.size()]));
                                return;
                            }
                            break;
                    }
                }
            } catch (Exception e) {
                throw new JobCreationException(current_, attr_, e);
            }
        } else {
            throw new RuntimeException("Unknown command type: " + cursorExec.getLocalName());
        }
    } catch (JobCreationException jce) {
        jce.pushTag(current);
        throw jce;
    } catch (Exception e) {
        String temporaryAttribute = null;
        if (cursorExec.isStartElement() && cursorExec.getAttributeCount() > 0) {
            temporaryAttribute = cursorExec.getAttributeLocalName(i);
        }
        throw new JobCreationException(current, temporaryAttribute, e);
    }
}
Also used : ArrayList(java.util.ArrayList) 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)

Example 8 with ForkEnvironment

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

the class SchedulerClientTest method nodeClientJob.

protected Job nodeClientJob(String groovyScript, String forkScript) throws Exception {
    URL scriptURL = SchedulerClientTest.class.getResource(groovyScript);
    URL forkScriptURL = SchedulerClientTest.class.getResource(forkScript);
    TaskFlowJob job = new TaskFlowJob();
    job.setName("NodeClientJob");
    ScriptTask task = new ScriptTask();
    task.setName("NodeClientTask");
    ForkEnvironment forkEnvironment = new ForkEnvironment();
    forkEnvironment.setEnvScript(new SimpleScript(IOUtils.toString(forkScriptURL.toURI()), "groovy"));
    task.setForkEnvironment(forkEnvironment);
    task.setScript(new TaskScript(new SimpleScript(IOUtils.toString(scriptURL.toURI()), "groovy")));
    // add CleanScript to test external APIs
    task.setCleaningScript(new SimpleScript("" + "schedulerapi.connect();\n" + "print(\"SCHEDULERAPI_URI_LIST_NOT_NULL=\"+(schedulerapi.getGlobalSpaceURIs()!=null));\n" + "\n" + "userspaceapi.connect();\n" + "print(\"USERSPACE_FILE_LIST_NOT_NULL=\"+(userspaceapi.listFiles(\".\", \"*\")!=null));\n" + "\n" + "globalspaceapi.connect();\n" + "print(\"GLOBALSPACE_FILE_LIST_NOT_NULL=\"+(globalspaceapi.listFiles(\".\", \"*\")!=null));\n" + "print(\"TEST_CREDS=\"+(credentials.get(\"TEST_CREDS\")));\n", "js"));
    job.addTask(task);
    return job;
}
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) ForkEnvironment(org.ow2.proactive.scheduler.common.task.ForkEnvironment) URL(java.net.URL)

Example 9 with ForkEnvironment

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

the class Job2XMLTransformerTest method forkEnvironmentIsPreserved.

@Test
public void forkEnvironmentIsPreserved() throws Exception {
    File xmlFile = tmpFolder.newFile();
    TaskFlowJob job = new TaskFlowJob();
    JavaTask task = new JavaTask();
    task.setName("forkedTask");
    task.setExecutableClassName("oo.Bar");
    task.setForkEnvironment(new ForkEnvironment());
    job.addTask(task);
    new Job2XMLTransformer().job2xmlFile(job, xmlFile);
    TaskFlowJob recreatedJob = (TaskFlowJob) (JobFactory.getFactory().createJob(xmlFile.getAbsolutePath()));
    assertNotNull(recreatedJob.getTask("forkedTask").getForkEnvironment());
}
Also used : TaskFlowJob(org.ow2.proactive.scheduler.common.job.TaskFlowJob) JavaTask(org.ow2.proactive.scheduler.common.task.JavaTask) ForkEnvironment(org.ow2.proactive.scheduler.common.task.ForkEnvironment) File(java.io.File) Test(org.junit.Test)

Example 10 with ForkEnvironment

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

the class TaskContextVariableExtractor method extractVariablesThirdPartyCredentialsAndSystemEnvironmentVariables.

/**
 * Retrieve all third party credential variables in a map.
 *
 * @param taskContext all information to extract third party credentials is here.
 *
 * @return map containing thirdPartyCredentials
 */
public Map<String, String> extractVariablesThirdPartyCredentialsAndSystemEnvironmentVariables(TaskContext taskContext) {
    ForkEnvironment forkEnvironment = taskContext.getInitializer().getForkEnvironment();
    Map<String, Serializable> variables = new HashMap<>();
    try {
        variables = extractAllVariables(taskContext, null, "");
    } catch (IOException | ClassNotFoundException e) {
        logger.error(ERROR_READING_VARIABLES, e);
    }
    Map<String, String> thirdPartyCredentials = new HashMap<>();
    try {
        thirdPartyCredentials = forkedTaskVariablesManager.extractThirdPartyCredentials(taskContext);
    } catch (Exception e) {
        logger.error(ERROR_READING_VARIABLES, e);
    }
    HashMap<String, Serializable> systemEnvironmentVariables = new HashMap<String, Serializable>(System.getenv());
    systemEnvironmentVariables.putAll(variables);
    systemEnvironmentVariables.putAll(thirdPartyCredentials);
    return VariableSubstitutor.filterAndUpdate(forkEnvironment.getSystemEnvironment(), systemEnvironmentVariables);
}
Also used : Serializable(java.io.Serializable) HashMap(java.util.HashMap) IOException(java.io.IOException) ForkEnvironment(org.ow2.proactive.scheduler.common.task.ForkEnvironment) IOException(java.io.IOException)

Aggregations

ForkEnvironment (org.ow2.proactive.scheduler.common.task.ForkEnvironment)54 Test (org.junit.Test)27 TaskFlowJob (org.ow2.proactive.scheduler.common.job.TaskFlowJob)25 JavaTask (org.ow2.proactive.scheduler.common.task.JavaTask)22 ScriptExecutableContainer (org.ow2.proactive.scheduler.task.containers.ScriptExecutableContainer)17 TaskContext (org.ow2.proactive.scheduler.task.context.TaskContext)15 TaskLauncherInitializer (org.ow2.proactive.scheduler.task.TaskLauncherInitializer)14 SimpleScript (org.ow2.proactive.scripting.SimpleScript)14 NodeDataSpacesURIs (org.ow2.proactive.scheduler.task.context.NodeDataSpacesURIs)13 VariablesMap (org.ow2.proactive.scheduler.task.utils.VariablesMap)9 ScriptHandler (org.ow2.proactive.scripting.ScriptHandler)9 File (java.io.File)8 NativeTask (org.ow2.proactive.scheduler.common.task.NativeTask)8 TaskScript (org.ow2.proactive.scripting.TaskScript)8 JobId (org.ow2.proactive.scheduler.common.job.JobId)7 JobCreationException (org.ow2.proactive.scheduler.common.exception.JobCreationException)5 JobResult (org.ow2.proactive.scheduler.common.job.JobResult)5 InternalJob (org.ow2.proactive.scheduler.job.InternalJob)5 InternalTask (org.ow2.proactive.scheduler.task.internal.InternalTask)5 FileNotFoundException (java.io.FileNotFoundException)4