Search in sources :

Example 11 with ExecuteException

use of org.opencastproject.execute.api.ExecuteException in project opencast by opencast.

the class ExecuteOnceWorkflowOperationHandler method start.

/**
 * {@inheritDoc}
 *
 * @see org.opencastproject.workflow.api.WorkflowOperationHandler#start(org.opencastproject.workflow.api.WorkflowInstance, JobContext)
 */
@Override
public WorkflowOperationResult start(WorkflowInstance workflowInstance, JobContext context) throws WorkflowOperationException {
    MediaPackage mediaPackage = workflowInstance.getMediaPackage();
    WorkflowOperationInstance operation = workflowInstance.getCurrentOperation();
    logger.debug("Running execute workflow operation with ID {}", operation.getId());
    // Get operation parameters
    String exec = StringUtils.trimToNull(operation.getConfiguration(EXEC_PROPERTY));
    String params = StringUtils.trimToNull(operation.getConfiguration(PARAMS_PROPERTY));
    float load = 1.0f;
    String loadPropertyStr = StringUtils.trimToEmpty(operation.getConfiguration(LOAD_PROPERTY));
    if (StringUtils.isNotBlank(loadPropertyStr)) {
        try {
            load = Float.parseFloat(loadPropertyStr);
        } catch (NumberFormatException e) {
            String description = StringUtils.trimToEmpty(operation.getDescription());
            logger.warn("Ignoring invalid load value '{}' on execute operation with description '{}'", loadPropertyStr, description);
        }
    }
    String targetFlavorStr = StringUtils.trimToNull(operation.getConfiguration(TARGET_FLAVOR_PROPERTY));
    String targetTags = StringUtils.trimToNull(operation.getConfiguration(TARGET_TAGS_PROPERTY));
    String outputFilename = StringUtils.trimToNull(operation.getConfiguration(OUTPUT_FILENAME_PROPERTY));
    String expectedTypeStr = StringUtils.trimToNull(operation.getConfiguration(EXPECTED_TYPE_PROPERTY));
    boolean setWfProps = Boolean.valueOf(StringUtils.trimToNull(operation.getConfiguration(SET_WF_PROPS_PROPERTY)));
    // Unmarshall target flavor
    MediaPackageElementFlavor targetFlavor = null;
    if (targetFlavorStr != null)
        targetFlavor = MediaPackageElementFlavor.parseFlavor(targetFlavorStr);
    // Unmarshall expected mediapackage element type
    MediaPackageElement.Type expectedType = null;
    if (expectedTypeStr != null) {
        for (MediaPackageElement.Type type : MediaPackageElement.Type.values()) if (type.toString().equalsIgnoreCase(expectedTypeStr)) {
            expectedType = type;
            break;
        }
        if (expectedType == null)
            throw new WorkflowOperationException("'" + expectedTypeStr + "' is not a valid element type");
    }
    // Process the result element
    MediaPackageElement resultElement = null;
    try {
        Job job = executeService.execute(exec, params, mediaPackage, outputFilename, expectedType, load);
        WorkflowOperationResult result = null;
        // Wait for all jobs to be finished
        if (!waitForStatus(job).isSuccess())
            throw new WorkflowOperationException("Execute operation failed");
        if (StringUtils.isNotBlank(job.getPayload())) {
            if (setWfProps) {
                // The job payload is a file with set of properties for the workflow
                resultElement = MediaPackageElementParser.getFromXml(job.getPayload());
                final Properties properties = new Properties();
                File propertiesFile = workspace.get(resultElement.getURI());
                try (InputStream is = new FileInputStream(propertiesFile)) {
                    properties.load(is);
                }
                logger.debug("Loaded {} properties from {}", properties.size(), propertiesFile);
                workspace.deleteFromCollection(ExecuteService.COLLECTION, propertiesFile.getName());
                Map<String, String> wfProps = new HashMap<String, String>((Map) properties);
                result = createResult(mediaPackage, wfProps, Action.CONTINUE, job.getQueueTime());
            } else {
                // The job payload is a new element for the MediaPackage
                resultElement = MediaPackageElementParser.getFromXml(job.getPayload());
                if (resultElement.getElementType() == MediaPackageElement.Type.Track) {
                    // Have the track inspected and return the result
                    Job inspectionJob = null;
                    inspectionJob = inspectionService.inspect(resultElement.getURI());
                    JobBarrier barrier = new JobBarrier(job, serviceRegistry, inspectionJob);
                    if (!barrier.waitForJobs().isSuccess()) {
                        throw new ExecuteException("Media inspection of " + resultElement.getURI() + " failed");
                    }
                    resultElement = MediaPackageElementParser.getFromXml(inspectionJob.getPayload());
                }
                // Store new element to mediaPackage
                mediaPackage.add(resultElement);
                URI uri = workspace.moveTo(resultElement.getURI(), mediaPackage.getIdentifier().toString(), resultElement.getIdentifier(), outputFilename);
                resultElement.setURI(uri);
                // Set new flavor
                if (targetFlavor != null)
                    resultElement.setFlavor(targetFlavor);
                // Set new tags
                if (targetTags != null) {
                    // Assume the tags starting with "-" means we want to eliminate such tags form the result element
                    for (String tag : asList(targetTags)) {
                        if (tag.startsWith("-"))
                            // We remove the tag resulting from stripping all the '-' characters at the beginning of the tag
                            resultElement.removeTag(tag.replaceAll("^-+", ""));
                        else
                            resultElement.addTag(tag);
                    }
                }
                result = createResult(mediaPackage, Action.CONTINUE, job.getQueueTime());
            }
        } else {
            // Payload is empty
            result = createResult(mediaPackage, Action.CONTINUE, job.getQueueTime());
        }
        logger.debug("Execute operation {} completed", operation.getId());
        return result;
    } catch (ExecuteException e) {
        throw new WorkflowOperationException(e);
    } catch (MediaPackageException e) {
        throw new WorkflowOperationException("Some result element couldn't be serialized", e);
    } catch (NotFoundException e) {
        throw new WorkflowOperationException("Could not find mediapackage", e);
    } catch (IOException e) {
        throw new WorkflowOperationException("Error unmarshalling a result mediapackage element", e);
    } catch (MediaInspectionException e) {
        throw new WorkflowOperationException("Media inspection of " + resultElement.getURI() + " failed", e);
    }
}
Also used : HashMap(java.util.HashMap) NotFoundException(org.opencastproject.util.NotFoundException) Properties(java.util.Properties) URI(java.net.URI) WorkflowOperationResult(org.opencastproject.workflow.api.WorkflowOperationResult) MediaInspectionException(org.opencastproject.inspection.api.MediaInspectionException) WorkflowOperationInstance(org.opencastproject.workflow.api.WorkflowOperationInstance) MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) WorkflowOperationException(org.opencastproject.workflow.api.WorkflowOperationException) ExecuteException(org.opencastproject.execute.api.ExecuteException) Job(org.opencastproject.job.api.Job) MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) MediaPackageElementFlavor(org.opencastproject.mediapackage.MediaPackageElementFlavor) JobBarrier(org.opencastproject.job.api.JobBarrier) FileInputStream(java.io.FileInputStream) MediaPackage(org.opencastproject.mediapackage.MediaPackage) File(java.io.File)

Aggregations

ExecuteException (org.opencastproject.execute.api.ExecuteException)11 IOException (java.io.IOException)7 MediaPackageException (org.opencastproject.mediapackage.MediaPackageException)7 ArrayList (java.util.ArrayList)5 Job (org.opencastproject.job.api.Job)5 MediaPackageElement (org.opencastproject.mediapackage.MediaPackageElement)5 MediaPackage (org.opencastproject.mediapackage.MediaPackage)4 NotFoundException (org.opencastproject.util.NotFoundException)4 File (java.io.File)3 URI (java.net.URI)3 FileInputStream (java.io.FileInputStream)2 HashMap (java.util.HashMap)2 HttpResponse (org.apache.http.HttpResponse)2 NameValuePair (org.apache.http.NameValuePair)2 UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)2 HttpPost (org.apache.http.client.methods.HttpPost)2 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)2 MediaInspectionException (org.opencastproject.inspection.api.MediaInspectionException)2 MediaPackageElementFlavor (org.opencastproject.mediapackage.MediaPackageElementFlavor)2 ServiceRegistryException (org.opencastproject.serviceregistry.api.ServiceRegistryException)2