Search in sources :

Example 46 with Job

use of org.ow2.proactive.scheduler.common.job.Job in project scheduling by ow2-proactive.

the class StaxJobFactory method createJob.

private Job createJob(File file, Map<String, String> replacementVariables) throws JobCreationException {
    try {
        // Check if the file exist
        if (!file.exists()) {
            throw new FileNotFoundException("This file has not been found: " + file.getAbsolutePath());
        }
        // validate content using the proper XML schema
        File updatedFile = validate(file);
        // set relative path
        relativePathRoot = updatedFile.getParentFile().getAbsolutePath();
        // create and get XML STAX reader
        XMLStreamReader xmlsr;
        Map<String, ArrayList<String>> dependencies = new HashMap<>();
        Job job;
        try (InputStream inputStream = new FileInputStream(updatedFile)) {
            // use the server side property to accept encoding
            if (PASchedulerProperties.FILE_ENCODING.isSet()) {
                xmlsr = xmlInputFactory.createXMLStreamReader(inputStream, PASchedulerProperties.FILE_ENCODING.getValueAsString());
            } else {
                xmlsr = xmlInputFactory.createXMLStreamReader(inputStream);
            }
            // Create the job starting at the first cursor position of the XML Stream reader
            job = createJob(xmlsr, replacementVariables, dependencies);
            // Close the stream
            xmlsr.close();
        }
        // make dependencies
        makeDependences(job, dependencies);
        validate((TaskFlowJob) job);
        logger.debug("Job successfully created!");
        // debug mode only
        displayJobInfo(job);
        return job;
    } catch (JobCreationException jce) {
        jce.pushTag(XMLTags.JOB.getXMLName());
        throw jce;
    } catch (Exception e) {
        throw new JobCreationException(e);
    }
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) ArrayList(java.util.ArrayList) JobCreationException(org.ow2.proactive.scheduler.common.exception.JobCreationException) TaskFlowJob(org.ow2.proactive.scheduler.common.job.TaskFlowJob) Job(org.ow2.proactive.scheduler.common.job.Job) File(java.io.File) FileInputStream(java.io.FileInputStream) 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 47 with Job

use of org.ow2.proactive.scheduler.common.job.Job in project scheduling by ow2-proactive.

the class StaxJobFactory method createJobVariables.

/**
 * Create a map of variables from XML variables.
 * Leave the method with the cursor at the end of 'ELEMENT_VARIABLES' tag
 *
 * @param cursorVariables the streamReader with the cursor on the 'ELEMENT_VARIABLES' tag.
 * @param replacementVariables variables which have precedence over the one defined in the job
 * @return the map in which the variables were added.
 * @throws JobCreationException
 */
private Map<String, JobVariable> createJobVariables(XMLStreamReader cursorVariables, Map<String, String> replacementVariables) throws JobCreationException {
    HashMap<String, JobVariable> variablesMap = new LinkedHashMap<>();
    try {
        int eventType;
        while (cursorVariables.hasNext()) {
            eventType = cursorVariables.next();
            switch(eventType) {
                case XMLEvent.START_ELEMENT:
                    if (XMLTags.VARIABLE.matches(cursorVariables.getLocalName())) {
                        Map<String, String> attributesAsMap = getAttributesAsMap(cursorVariables, null);
                        String name = attributesAsMap.get(XMLAttributes.VARIABLE_NAME.getXMLName());
                        String value = attributesAsMap.get(XMLAttributes.VARIABLE_VALUE.getXMLName());
                        String model = attributesAsMap.get(XMLAttributes.VARIABLE_MODEL.getXMLName());
                        variablesMap.put(name, new JobVariable(name, value, model));
                    }
                    break;
                case XMLEvent.END_ELEMENT:
                    if (XMLTags.VARIABLES.matches(cursorVariables.getLocalName())) {
                        return replaceVariablesInJobVariablesMap(variablesMap, replacementVariables);
                    }
                    break;
            }
        }
    } catch (JobCreationException jce) {
        jce.pushTag(cursorVariables.getLocalName());
        throw jce;
    } catch (Exception e) {
        String attrtmp = null;
        if (cursorVariables.isStartElement() && cursorVariables.getAttributeCount() == 1) {
            attrtmp = cursorVariables.getAttributeLocalName(0);
        }
        throw new JobCreationException(cursorVariables.getLocalName(), attrtmp, e);
    }
    return variablesMap;
}
Also used : JobCreationException(org.ow2.proactive.scheduler.common.exception.JobCreationException) JobVariable(org.ow2.proactive.scheduler.common.job.JobVariable) 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) LinkedHashMap(java.util.LinkedHashMap)

Example 48 with Job

use of org.ow2.proactive.scheduler.common.job.Job in project scheduling by ow2-proactive.

the class StaxJobFactory method replaceVariablesInJobVariablesMap.

protected Map<String, JobVariable> replaceVariablesInJobVariablesMap(Map<String, JobVariable> variablesMap, Map<String, String> replacementVariables) throws JobCreationException {
    HashMap<String, String> updatedReplacementVariables = new HashMap<>();
    HashMap<String, JobVariable> updatedVariablesMap = new HashMap<>(variablesMap);
    // replacements will include at first variables defined in the job
    for (JobVariable variable : updatedVariablesMap.values()) {
        updatedReplacementVariables.put(variable.getName(), variable.getValue());
    }
    if (replacementVariables != null) {
        // overwritten by variables used at job submission
        updatedReplacementVariables.putAll(replacementVariables);
    }
    for (Map.Entry<String, String> replacementVariable : updatedReplacementVariables.entrySet()) {
        if (updatedVariablesMap.containsKey(replacementVariable.getKey())) {
            // if the variable is already defined in the job, overwrite its value by the replacement variable,
            // eventually using other variables as pattern replacements
            JobVariable jobVariable = updatedVariablesMap.get(replacementVariable.getKey());
            jobVariable.setValue(replace(replacementVariable.getValue(), updatedReplacementVariables));
            if (jobVariable.getModel() != null) {
                // model of an existing variable can use other variables as pattern replacements
                jobVariable.setModel(replace(jobVariable.getModel(), updatedReplacementVariables));
            }
        } else {
            // if the variable is not defined in the job, create a new job variable with an empty model
            updatedVariablesMap.put(replacementVariable.getKey(), new JobVariable(replacementVariable.getKey(), replace(replacementVariable.getValue(), updatedReplacementVariables), null));
        }
    }
    return updatedVariablesMap;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) JobVariable(org.ow2.proactive.scheduler.common.job.JobVariable) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 49 with Job

use of org.ow2.proactive.scheduler.common.job.Job in project scheduling by ow2-proactive.

the class BatchJobInfrastructure method removeNode.

/**
 * {@inheritDoc}
 */
@Override
public void removeNode(Node node) throws RMException {
    String deleteCmd = getDeleteJobCommand();
    String jobID = null;
    String nodeName = node.getNodeInformation().getName();
    if ((jobID = getCurrentNode(nodeName)) != null) {
        try {
            deleteJob(jobID);
        } catch (RMException e) {
            logger.warn(deleteCmd + " command failed, cannot ensure job " + jobID + " is deleted. Anyway, node " + nodeName + " is removed from the infrastructure manager.", e);
        }
        // atomic remove is important, furthermore we ensure consistent
        // trace
        writeLock.lock();
        try {
            removeCurrentNode(nodeName);
            logger.debug("Node " + nodeName + " removed. # of current nodes: " + getCurrentNodesSize() + " # of deploying nodes: " + getNbDeployingNodes());
        } catch (RuntimeException e) {
            logger.error("Exception while removing a node: " + e.getMessage());
            throw e;
        } finally {
            writeLock.unlock();
        }
    } else {
        logger.error("Node " + nodeName + " is not known as a Node belonging to this " + getClass().getSimpleName());
    }
}
Also used : Throwables.getStackTraceAsString(com.google.common.base.Throwables.getStackTraceAsString) RMException(org.ow2.proactive.resourcemanager.exception.RMException)

Example 50 with Job

use of org.ow2.proactive.scheduler.common.job.Job in project scheduling by ow2-proactive.

the class GetJobOutputCommand method execute.

@Override
public void execute(ApplicationContext currentContext) throws CLIException {
    SchedulerRestInterface scheduler = currentContext.getRestClient().getScheduler();
    try {
        String output = null;
        if (this.tag == null) {
            output = scheduler.jobLogs(currentContext.getSessionId(), jobId);
        } else {
            output = scheduler.taskLogByTag(currentContext.getSessionId(), jobId, tag);
        }
        resultStack(currentContext).push(output);
        if (!currentContext.isSilent()) {
            writeLine(currentContext, "%s", output);
        }
    } catch (Exception e) {
        handleError(String.format("An error occurred while retrieving %s output:", job()), e, currentContext);
    }
}
Also used : SchedulerRestInterface(org.ow2.proactive_grid_cloud_portal.common.SchedulerRestInterface) CLIException(org.ow2.proactive_grid_cloud_portal.cli.CLIException)

Aggregations

Test (org.junit.Test)260 TaskFlowJob (org.ow2.proactive.scheduler.common.job.TaskFlowJob)198 InternalJob (org.ow2.proactive.scheduler.job.InternalJob)145 JobId (org.ow2.proactive.scheduler.common.job.JobId)122 InternalTask (org.ow2.proactive.scheduler.task.internal.InternalTask)91 JavaTask (org.ow2.proactive.scheduler.common.task.JavaTask)87 File (java.io.File)76 SimpleScript (org.ow2.proactive.scripting.SimpleScript)70 TaskResult (org.ow2.proactive.scheduler.common.task.TaskResult)68 TaskScript (org.ow2.proactive.scripting.TaskScript)65 JobIdImpl (org.ow2.proactive.scheduler.job.JobIdImpl)64 Scheduler (org.ow2.proactive.scheduler.common.Scheduler)58 ScriptExecutableContainer (org.ow2.proactive.scheduler.task.containers.ScriptExecutableContainer)52 UnknownJobException (org.ow2.proactive.scheduler.common.exception.UnknownJobException)48 TaskId (org.ow2.proactive.scheduler.common.task.TaskId)48 ArrayList (java.util.ArrayList)46 JobState (org.ow2.proactive.scheduler.common.job.JobState)45 TaskResultImpl (org.ow2.proactive.scheduler.task.TaskResultImpl)45 PermissionException (org.ow2.proactive.scheduler.common.exception.PermissionException)41 NotConnectedException (org.ow2.proactive.scheduler.common.exception.NotConnectedException)39