Search in sources :

Example 1 with WorkflowOperationInstance

use of org.opencastproject.workflow.api.WorkflowOperationInstance in project opencast by opencast.

the class AssetManagerSnapshotWorkflowOperationHandler method start.

@Override
public WorkflowOperationResult start(WorkflowInstance wi, JobContext ctx) throws WorkflowOperationException {
    final MediaPackage mpWorkflow = wi.getMediaPackage();
    final WorkflowOperationInstance currentOperation = wi.getCurrentOperation();
    // Check which tags have been configured
    final String tags = StringUtils.trimToNull(currentOperation.getConfiguration("source-tags"));
    final String sourceFlavorsString = StringUtils.trimToEmpty(currentOperation.getConfiguration("source-flavors"));
    final String[] sourceFlavors = StringUtils.split(sourceFlavorsString, ",");
    if (sourceFlavors.length < 1 && tags == null)
        logger.debug("No source tags have been specified, so everything will be added to the AssetManager");
    final List<String> tagSet;
    // If a set of tags has been specified, use it
    if (tags != null) {
        tagSet = asList(tags);
    } else {
        tagSet = new ArrayList<>();
    }
    try {
        final MediaPackage mpAssetManager = getMediaPackageForArchival(mpWorkflow, tagSet, sourceFlavors);
        if (mpAssetManager != null) {
            logger.info("Take snapshot of media package {}", mpAssetManager);
            // adding media package to the episode service
            assetManager.takeSnapshot(DEFAULT_OWNER, mpAssetManager);
            logger.debug("Snapshot operation complete");
            return createResult(mpWorkflow, Action.CONTINUE);
        } else {
            return createResult(mpWorkflow, Action.CONTINUE);
        }
    } catch (Throwable t) {
        throw new WorkflowOperationException(t);
    }
}
Also used : WorkflowOperationInstance(org.opencastproject.workflow.api.WorkflowOperationInstance) MediaPackage(org.opencastproject.mediapackage.MediaPackage) WorkflowOperationException(org.opencastproject.workflow.api.WorkflowOperationException)

Example 2 with WorkflowOperationInstance

use of org.opencastproject.workflow.api.WorkflowOperationInstance in project opencast by opencast.

the class IngestDownloadWorkflowOperationHandler method start.

/**
 * {@inheritDoc}
 *
 * @see org.opencastproject.workflow.api.AbstractWorkflowOperationHandler#start(org.opencastproject.workflow.api.WorkflowInstance,
 *      JobContext)
 */
@Override
public WorkflowOperationResult start(WorkflowInstance workflowInstance, JobContext context) throws WorkflowOperationException {
    MediaPackage mediaPackage = workflowInstance.getMediaPackage();
    WorkflowOperationInstance currentOperation = workflowInstance.getCurrentOperation();
    boolean deleteExternal = BooleanUtils.toBoolean(currentOperation.getConfiguration(DELETE_EXTERNAL));
    String baseUrl = workspace.getBaseUri().toString();
    List<URI> externalUris = new ArrayList<URI>();
    for (MediaPackageElement element : mediaPackage.getElements()) {
        if (element.getURI() == null)
            continue;
        if (element.getElementType() == MediaPackageElement.Type.Publication) {
            logger.debug("Skipping downloading media package element {} from media package {} " + "because it is a publication: {}", element.getIdentifier(), mediaPackage.getIdentifier().compact(), element.getURI());
            continue;
        }
        URI originalElementUri = element.getURI();
        if (originalElementUri.toString().startsWith(baseUrl)) {
            logger.info("Skipping downloading already existing element {}", originalElementUri);
            continue;
        }
        // Download the external URI
        File file;
        try {
            file = workspace.get(element.getURI());
        } catch (Exception e) {
            logger.warn("Unable to download the external element {}", element.getURI());
            throw new WorkflowOperationException("Unable to download the external element " + element.getURI(), e);
        }
        // Put to working file repository and rewrite URI on element
        InputStream in = null;
        try {
            in = new FileInputStream(file);
            URI uri = workspace.put(mediaPackage.getIdentifier().compact(), element.getIdentifier(), FilenameUtils.getName(element.getURI().getPath()), in);
            element.setURI(uri);
        } catch (Exception e) {
            logger.warn("Unable to store downloaded element '{}': {}", element.getURI(), e.getMessage());
            throw new WorkflowOperationException("Unable to store downloaded element " + element.getURI(), e);
        } finally {
            IOUtils.closeQuietly(in);
            try {
                workspace.delete(originalElementUri);
            } catch (Exception e) {
                logger.warn("Unable to delete ingest-downloaded element {}: {}", element.getURI(), e);
            }
        }
        logger.info("Downloaded the external element {}", originalElementUri);
        // Store origianl URI for deletion
        externalUris.add(originalElementUri);
    }
    if (!deleteExternal || externalUris.size() == 0)
        return createResult(mediaPackage, Action.CONTINUE);
    // Find all external working file repository base Urls
    logger.debug("Assembling list of external working file repositories");
    List<String> externalWfrBaseUrls = new ArrayList<String>();
    try {
        for (ServiceRegistration reg : serviceRegistry.getServiceRegistrationsByType(WorkingFileRepository.SERVICE_TYPE)) {
            if (baseUrl.startsWith(reg.getHost())) {
                logger.trace("Skpping local working file repository");
                continue;
            }
            externalWfrBaseUrls.add(UrlSupport.concat(reg.getHost(), reg.getPath()));
        }
        logger.debug("{} external working file repositories found", externalWfrBaseUrls.size());
    } catch (ServiceRegistryException e) {
        logger.error("Unable to load WFR services from service registry: {}", e.getMessage());
        throw new WorkflowOperationException(e);
    }
    for (URI uri : externalUris) {
        String elementUri = uri.toString();
        // Delete external working file repository URI's
        String wfrBaseUrl = null;
        for (String url : externalWfrBaseUrls) {
            if (elementUri.startsWith(url)) {
                wfrBaseUrl = url;
                break;
            }
        }
        if (wfrBaseUrl == null) {
            logger.info("Unable to delete external URI {}, no working file repository found", elementUri);
            continue;
        }
        HttpDelete delete;
        if (elementUri.startsWith(UrlSupport.concat(wfrBaseUrl, WorkingFileRepository.MEDIAPACKAGE_PATH_PREFIX))) {
            String wfrDeleteUrl = elementUri.substring(0, elementUri.lastIndexOf("/"));
            delete = new HttpDelete(wfrDeleteUrl);
        } else if (elementUri.startsWith(UrlSupport.concat(wfrBaseUrl, WorkingFileRepository.COLLECTION_PATH_PREFIX))) {
            delete = new HttpDelete(elementUri);
        } else {
            logger.info("Unable to handle working file repository URI {}", elementUri);
            continue;
        }
        HttpResponse response = null;
        try {
            response = client.execute(delete);
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode == HttpStatus.SC_NO_CONTENT || statusCode == HttpStatus.SC_OK) {
                logger.info("Sucessfully deleted external URI {}", delete.getURI());
            } else if (statusCode == HttpStatus.SC_NOT_FOUND) {
                logger.info("External URI {} has already been deleted", delete.getURI());
            } else {
                logger.info("Unable to delete external URI {}, status code '{}' returned", delete.getURI(), statusCode);
            }
        } catch (TrustedHttpClientException e) {
            logger.warn("Unable to execute DELETE request on external URI {}", delete.getURI());
            throw new WorkflowOperationException(e);
        } finally {
            client.close(response);
        }
    }
    return createResult(mediaPackage, Action.CONTINUE);
}
Also used : HttpDelete(org.apache.http.client.methods.HttpDelete) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) HttpResponse(org.apache.http.HttpResponse) URI(java.net.URI) TrustedHttpClientException(org.opencastproject.security.api.TrustedHttpClientException) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException) WorkflowOperationException(org.opencastproject.workflow.api.WorkflowOperationException) TrustedHttpClientException(org.opencastproject.security.api.TrustedHttpClientException) FileInputStream(java.io.FileInputStream) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException) WorkflowOperationInstance(org.opencastproject.workflow.api.WorkflowOperationInstance) MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) MediaPackage(org.opencastproject.mediapackage.MediaPackage) WorkflowOperationException(org.opencastproject.workflow.api.WorkflowOperationException) File(java.io.File) ServiceRegistration(org.opencastproject.serviceregistry.api.ServiceRegistration)

Example 3 with WorkflowOperationInstance

use of org.opencastproject.workflow.api.WorkflowOperationInstance in project opencast by opencast.

the class InspectWorkflowOperationHandler 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 = (MediaPackage) workflowInstance.getMediaPackage().clone();
    // Inspect the tracks
    long totalTimeInQueue = 0;
    WorkflowOperationInstance operation = workflowInstance.getCurrentOperation();
    boolean rewrite = "true".equalsIgnoreCase(operation.getConfiguration(OPT_OVERWRITE));
    boolean acceptNoMedia = "true".equalsIgnoreCase(operation.getConfiguration(OPT_ACCEPT_NO_MEDIA));
    final Map<String, String> options = new HashMap<String, String>();
    if ("true".equalsIgnoreCase(operation.getConfiguration(OPT_ACCURATE_FRAME_COUNT))) {
        logger.info("Using accurate frame count for inspection media package {}", mediaPackage);
        options.put(MediaInspectionOptions.OPTION_ACCURATE_FRAME_COUNT, Boolean.TRUE.toString());
    }
    // Test if there are tracks in the mediapackage
    if (mediaPackage.getTracks().length == 0) {
        logger.warn("Recording {} contains no media", mediaPackage);
        if (!acceptNoMedia)
            throw new WorkflowOperationException("Mediapackage " + mediaPackage + " contains no media");
    }
    for (Track track : mediaPackage.getTracks()) {
        logger.info("Inspecting track '{}' of {}", track.getIdentifier(), mediaPackage);
        Job inspectJob = null;
        Track inspectedTrack;
        if (track != null && track.getURI() != null && (track.getURI().toString().endsWith(".vtt") || track.getURI().toString().endsWith(".srt"))) {
            inspectedTrack = (Track) track.clone();
            inspectedTrack.setMimeType(MimeType.mimeType("text", "vtt"));
            logger.info("Track '{}' of {} contains captions", track.getIdentifier(), mediaPackage);
        } else {
            try {
                inspectJob = inspectionService.enrich(track, rewrite, options);
                if (!waitForStatus(inspectJob).isSuccess()) {
                    throw new WorkflowOperationException("Track " + track + " could not be inspected");
                }
            } catch (MediaInspectionException e) {
                throw new WorkflowOperationException("Error inspecting media package", e);
            } catch (MediaPackageException e) {
                throw new WorkflowOperationException("Error parsing media package", e);
            }
            // add this receipt's queue and execution times to the total
            long timeInQueue = inspectJob.getQueueTime() == null ? 0 : inspectJob.getQueueTime();
            totalTimeInQueue += timeInQueue;
            try {
                inspectedTrack = (Track) MediaPackageElementParser.getFromXml(inspectJob.getPayload());
            } catch (MediaPackageException e) {
                throw new WorkflowOperationException("Unable to parse track from job " + inspectJob.getId(), e);
            }
            if (inspectedTrack == null)
                throw new WorkflowOperationException("Track " + track + " could not be inspected");
            if (inspectedTrack.getStreams().length == 0)
                throw new WorkflowOperationException(format("Track %s does not contain any streams", track));
        }
        // Replace the original track with the inspected one
        try {
            mediaPackage.remove(track);
            mediaPackage.add(inspectedTrack);
        } catch (UnsupportedElementException e) {
            logger.error("Error adding {} to media package", inspectedTrack, e);
        }
    }
    // Update dublin core with metadata
    try {
        updateDublinCore(mediaPackage);
    } catch (Exception e) {
        logger.warn("Unable to update dublin core data: {}", e.getMessage(), e);
        throw new WorkflowOperationException(e.getMessage());
    }
    return createResult(mediaPackage, Action.CONTINUE, totalTimeInQueue);
}
Also used : MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) HashMap(java.util.HashMap) UnsupportedElementException(org.opencastproject.mediapackage.UnsupportedElementException) WorkflowOperationException(org.opencastproject.workflow.api.WorkflowOperationException) MediaInspectionException(org.opencastproject.inspection.api.MediaInspectionException) NotFoundException(org.opencastproject.util.NotFoundException) IOException(java.io.IOException) MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) MediaInspectionException(org.opencastproject.inspection.api.MediaInspectionException) WorkflowOperationInstance(org.opencastproject.workflow.api.WorkflowOperationInstance) UnsupportedElementException(org.opencastproject.mediapackage.UnsupportedElementException) MediaPackage(org.opencastproject.mediapackage.MediaPackage) WorkflowOperationException(org.opencastproject.workflow.api.WorkflowOperationException) Job(org.opencastproject.job.api.Job) Track(org.opencastproject.mediapackage.Track)

Example 4 with WorkflowOperationInstance

use of org.opencastproject.workflow.api.WorkflowOperationInstance in project opencast by opencast.

the class CopyWorkflowOperationHandlerTest method getWorkflowOperationResult.

private WorkflowOperationResult getWorkflowOperationResult(MediaPackage mp, Map<String, String> configurations) throws WorkflowOperationException {
    // Add the mediapackage to a workflow instance
    WorkflowInstanceImpl workflowInstance = new WorkflowInstanceImpl();
    workflowInstance.setId(1);
    workflowInstance.setState(WorkflowState.RUNNING);
    workflowInstance.setMediaPackage(mp);
    WorkflowOperationInstanceImpl operation = new WorkflowOperationInstanceImpl("op", OperationState.RUNNING);
    operation.setTemplate("copy");
    operation.setState(OperationState.RUNNING);
    for (String key : configurations.keySet()) {
        operation.setConfiguration(key, configurations.get(key));
    }
    List<WorkflowOperationInstance> operationsList = new ArrayList<WorkflowOperationInstance>();
    operationsList.add(operation);
    workflowInstance.setOperations(operationsList);
    // Run the media package through the operation handler, ensuring that metadata gets added
    return operationHandler.start(workflowInstance, null);
}
Also used : WorkflowInstanceImpl(org.opencastproject.workflow.api.WorkflowInstanceImpl) WorkflowOperationInstance(org.opencastproject.workflow.api.WorkflowOperationInstance) ArrayList(java.util.ArrayList) WorkflowOperationInstanceImpl(org.opencastproject.workflow.api.WorkflowOperationInstanceImpl)

Example 5 with WorkflowOperationInstance

use of org.opencastproject.workflow.api.WorkflowOperationInstance in project opencast by opencast.

the class DuplicateEventWorkflowOperationHandlerTest method getWorkflowOperationResult.

private WorkflowOperationResult getWorkflowOperationResult(MediaPackage mp, Map<String, String> configurations) throws WorkflowOperationException {
    // Add the mediapackage to a workflow instance
    WorkflowInstanceImpl workflowInstance = new WorkflowInstanceImpl();
    workflowInstance.setId(1);
    workflowInstance.setState(WorkflowState.RUNNING);
    workflowInstance.setMediaPackage(mp);
    WorkflowOperationInstanceImpl operation = new WorkflowOperationInstanceImpl("op", OperationState.RUNNING);
    operation.setTemplate("create-event");
    operation.setState(OperationState.RUNNING);
    for (String key : configurations.keySet()) {
        operation.setConfiguration(key, configurations.get(key));
    }
    List<WorkflowOperationInstance> operationsList = new ArrayList<>();
    operationsList.add(operation);
    workflowInstance.setOperations(operationsList);
    // Run the media package through the operation handler
    return operationHandler.start(workflowInstance, null);
}
Also used : WorkflowInstanceImpl(org.opencastproject.workflow.api.WorkflowInstanceImpl) WorkflowOperationInstance(org.opencastproject.workflow.api.WorkflowOperationInstance) ArrayList(java.util.ArrayList) WorkflowOperationInstanceImpl(org.opencastproject.workflow.api.WorkflowOperationInstanceImpl) EasyMock.anyString(org.easymock.EasyMock.anyString)

Aggregations

WorkflowOperationInstance (org.opencastproject.workflow.api.WorkflowOperationInstance)104 ArrayList (java.util.ArrayList)51 MediaPackage (org.opencastproject.mediapackage.MediaPackage)48 WorkflowInstanceImpl (org.opencastproject.workflow.api.WorkflowInstanceImpl)37 WorkflowOperationInstanceImpl (org.opencastproject.workflow.api.WorkflowOperationInstanceImpl)33 Test (org.junit.Test)32 WorkflowOperationException (org.opencastproject.workflow.api.WorkflowOperationException)31 WorkflowOperationResult (org.opencastproject.workflow.api.WorkflowOperationResult)28 WorkflowInstance (org.opencastproject.workflow.api.WorkflowInstance)24 Job (org.opencastproject.job.api.Job)23 MediaPackageElement (org.opencastproject.mediapackage.MediaPackageElement)19 MediaPackageElementFlavor (org.opencastproject.mediapackage.MediaPackageElementFlavor)19 URI (java.net.URI)18 NotFoundException (org.opencastproject.util.NotFoundException)16 Track (org.opencastproject.mediapackage.Track)14 IOException (java.io.IOException)13 File (java.io.File)12 HashMap (java.util.HashMap)12 MediaPackageException (org.opencastproject.mediapackage.MediaPackageException)11 ServiceRegistryException (org.opencastproject.serviceregistry.api.ServiceRegistryException)10