Search in sources :

Example 6 with Job

use of org.opencastproject.job.api.Job 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 7 with Job

use of org.opencastproject.job.api.Job in project opencast by opencast.

the class DuplicateEventWorkflowOperationHandlerTest method mockDependencies.

private void mockDependencies(int numberOfCopies) throws Exception {
    clonedMediaPackages = Capture.newInstance(CaptureType.ALL);
    reset(workspace, assetManager, distributionService);
    URI uriDc = getClass().getResource("/dublincore.xml").toURI();
    for (int i = 0; i < numberOfCopies; i++) {
        expect(workspace.read(eq(URI.create("dublincore.xml")))).andReturn(new FileInputStream(new File(uriDc))).times(1);
    }
    expect(workspace.get(anyObject())).andReturn(new File(getClass().getResource("/av.mov").toURI())).anyTimes();
    expect(workspace.put(anyString(), anyString(), eq("dublincore.xml"), anyObject())).andReturn(uriDc).times(numberOfCopies);
    replay(workspace);
    final AResult qResult = createNiceMock(AResult.class);
    expect(qResult.getRecords()).andReturn(Stream.empty()).anyTimes();
    replay(qResult);
    final ASelectQuery qSelect = createNiceMock(ASelectQuery.class);
    expect(qSelect.where(anyObject())).andReturn(qSelect).anyTimes();
    expect(qSelect.run()).andReturn(qResult).anyTimes();
    replay(qSelect);
    final AQueryBuilder qBuilder = createNiceMock(AQueryBuilder.class);
    expect(qBuilder.select(anyObject())).andReturn(qSelect).anyTimes();
    replay(qBuilder);
    expect(assetManager.createQuery()).andReturn(qBuilder).anyTimes();
    expect(assetManager.takeSnapshot(eq(AssetManager.DEFAULT_OWNER), capture(clonedMediaPackages))).andReturn(createNiceMock(Snapshot.class)).times(numberOfCopies);
    replay(assetManager);
    final Job distributionJob = createNiceMock(Job.class);
    final Publication internalPub = (Publication) mp.getElementById("pub-int");
    final List<MediaPackageElement> internalPubElements = new ArrayList<>();
    Collections.addAll(internalPubElements, (internalPub.getAttachments()));
    Collections.addAll(internalPubElements, (internalPub.getCatalogs()));
    Collections.addAll(internalPubElements, (internalPub.getTracks()));
    expect(distributionJob.getStatus()).andReturn(Job.Status.FINISHED).anyTimes();
    for (MediaPackageElement e : internalPubElements) {
        expect(distributionJob.getPayload()).andReturn(MediaPackageElementParser.getAsXml(e)).times(numberOfCopies);
    }
    replay(distributionJob);
    expect(distributionService.distribute(eq(InternalPublicationChannel.CHANNEL_ID), anyObject(), anyString())).andReturn(distributionJob).anyTimes();
    replay(distributionService);
}
Also used : MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) ArrayList(java.util.ArrayList) AResult(org.opencastproject.assetmanager.api.query.AResult) AQueryBuilder(org.opencastproject.assetmanager.api.query.AQueryBuilder) Publication(org.opencastproject.mediapackage.Publication) ASelectQuery(org.opencastproject.assetmanager.api.query.ASelectQuery) Job(org.opencastproject.job.api.Job) URI(java.net.URI) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 8 with Job

use of org.opencastproject.job.api.Job in project opencast by opencast.

the class AbstractJobProducerEndpoint method dispatchJob.

/**
 * @see org.opencastproject.job.api.JobProducer#acceptJob(org.opencastproject.job.api.Job)
 */
@POST
@Path("/dispatch")
public Response dispatchJob(@FormParam("id") long jobId, @FormParam("operation") String jobOperation) throws ServiceRegistryException {
    final JobProducer service = getService();
    if (service == null)
        throw new WebApplicationException(Status.SERVICE_UNAVAILABLE);
    // See if the service is ready to accept anything
    if (!service.isReadyToAcceptJobs(jobOperation)) {
        logger.debug("Service {} is not ready to accept jobs with operation {}", service, jobOperation);
        return Response.status(Status.SERVICE_UNAVAILABLE).build();
    }
    Job job;
    try {
        job = getServiceRegistry().getJob(jobId);
    } catch (NotFoundException e) {
        logger.warn("Unable to find dispatched job {}", jobId);
        return Response.status(Status.NOT_FOUND).build();
    }
    // See if the service has strong feelings about this particular job
    try {
        if (!service.isReadyToAccept(job)) {
            logger.debug("Service {} temporarily refused to accept job {}", service, jobId);
            return Response.status(Status.SERVICE_UNAVAILABLE).build();
        }
    } catch (UndispatchableJobException e) {
        logger.warn("Service {} permanently refused to accept job {}", service, jobId);
        return Response.status(Status.PRECONDITION_FAILED).build();
    }
    service.acceptJob(job);
    return Response.noContent().build();
}
Also used : JobProducer(org.opencastproject.job.api.JobProducer) WebApplicationException(javax.ws.rs.WebApplicationException) UndispatchableJobException(org.opencastproject.serviceregistry.api.UndispatchableJobException) NotFoundException(org.opencastproject.util.NotFoundException) Job(org.opencastproject.job.api.Job) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Example 9 with Job

use of org.opencastproject.job.api.Job in project opencast by opencast.

the class IncidentCreatorWorkflowOperationHandler method start.

@Override
public WorkflowOperationResult start(WorkflowInstance wi, JobContext ctx) throws WorkflowOperationException {
    final WorkflowOperationInstance woi = wi.getCurrentOperation();
    final int code = option(woi.getConfiguration(OPT_CODE)).bind(Strings.toInt).getOrElse(1);
    final Severity severity = option(woi.getConfiguration(OPT_SEVERITY)).bind(parseEnum(Severity.FAILURE)).getOrElse(Severity.INFO);
    final List<Tuple<String, String>> details = option(woi.getConfiguration(OPT_DETAILS)).mlist().bind(splitBy(";")).map(splitBy("=")).filter(Tuples.<String>listHasSize(2)).map(Tuples.<String>fromList()).value();
    final Map<String, String> params = Immutables.map(option(woi.getConfiguration(OPT_PARAMS)).mlist().bind(splitBy(";")).map(splitBy("=")).filter(Tuples.<String>listHasSize(2)).map(Tuples.<String>fromList()).value());
    log.info("Create nop job");
    final Job job = nopService.nop();
    log.info("Log a dummy incident with code %d", code);
    serviceRegistry.incident().record(job, severity, code, params, details);
    if (!waitForStatus(job).isSuccess()) {
        throw new WorkflowOperationException("Job did not complete successfully");
    } else {
        return createResult(WorkflowOperationResult.Action.CONTINUE);
    }
}
Also used : WorkflowOperationInstance(org.opencastproject.workflow.api.WorkflowOperationInstance) WorkflowOperationException(org.opencastproject.workflow.api.WorkflowOperationException) Severity(org.opencastproject.job.api.Incident.Severity) Job(org.opencastproject.job.api.Job) Tuple(org.opencastproject.util.data.Tuple)

Example 10 with Job

use of org.opencastproject.job.api.Job in project opencast by opencast.

the class AbstractIncidentService method getIncidentsOfJob.

@Override
public IncidentTree getIncidentsOfJob(long jobId, boolean cascade) throws NotFoundException, IncidentServiceException {
    List<Incident> incidents = getIncidentsOfJob(jobId);
    List<IncidentTree> childIncidents = new ArrayList<IncidentTree>();
    try {
        Job job = getServiceRegistry().getJob(jobId);
        if (cascade && !"START_WORKFLOW".equals(job.getOperation())) {
            childIncidents = getChildIncidents(jobId);
        } else if (cascade && "START_WORKFLOW".equals(job.getOperation())) {
            for (WorkflowOperationInstance operation : getWorkflowService().getWorkflowById(jobId).getOperations()) {
                if (operation.getState().equals(OperationState.INSTANTIATED))
                    continue;
                IncidentTree operationResult = getIncidentsOfJob(operation.getId(), true);
                if (hasIncidents(Collections.list(operationResult)))
                    childIncidents.add(operationResult);
            }
        }
        return new IncidentTreeImpl(incidents, childIncidents);
    } catch (NotFoundException ignore) {
        // Workflow deleted
        return new IncidentTreeImpl(incidents, childIncidents);
    } catch (Exception e) {
        logger.error("Error loading child jobs of {}: {}", jobId);
        throw new IncidentServiceException(e);
    }
}
Also used : WorkflowOperationInstance(org.opencastproject.workflow.api.WorkflowOperationInstance) IncidentTreeImpl(org.opencastproject.job.api.IncidentTreeImpl) IncidentServiceException(org.opencastproject.serviceregistry.api.IncidentServiceException) ArrayList(java.util.ArrayList) NotFoundException(org.opencastproject.util.NotFoundException) Incident(org.opencastproject.job.api.Incident) Job(org.opencastproject.job.api.Job) IncidentServiceException(org.opencastproject.serviceregistry.api.IncidentServiceException) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException) NotFoundException(org.opencastproject.util.NotFoundException) IncidentTree(org.opencastproject.job.api.IncidentTree)

Aggregations

Job (org.opencastproject.job.api.Job)282 MediaPackage (org.opencastproject.mediapackage.MediaPackage)89 ArrayList (java.util.ArrayList)82 Test (org.junit.Test)82 URI (java.net.URI)68 NotFoundException (org.opencastproject.util.NotFoundException)58 Track (org.opencastproject.mediapackage.Track)56 JaxbJob (org.opencastproject.job.api.JaxbJob)52 MediaPackageException (org.opencastproject.mediapackage.MediaPackageException)51 WorkflowOperationException (org.opencastproject.workflow.api.WorkflowOperationException)50 MediaPackageElement (org.opencastproject.mediapackage.MediaPackageElement)49 IOException (java.io.IOException)45 ServiceRegistryException (org.opencastproject.serviceregistry.api.ServiceRegistryException)42 HttpPost (org.apache.http.client.methods.HttpPost)33 Path (javax.ws.rs.Path)31 Produces (javax.ws.rs.Produces)30 JobBarrier (org.opencastproject.job.api.JobBarrier)30 RestQuery (org.opencastproject.util.doc.rest.RestQuery)30 POST (javax.ws.rs.POST)29 HttpResponse (org.apache.http.HttpResponse)29