Search in sources :

Example 1 with Result

use of org.opencastproject.job.api.JobBarrier.Result in project opencast by opencast.

the class JobUtilTest method testWaitForJobs.

@Test
public void testWaitForJobs() {
    Job job1 = new JobImpl(1);
    job1.setStatus(Status.FINISHED);
    Job job2 = new JobImpl(2);
    job2.setStatus(Status.RUNNING);
    Job job3 = new JobImpl(3);
    job3.setStatus(Status.FINISHED);
    Result result = JobUtil.waitForJobs(job2, serviceRegistry, 0L, 0L, new Job[] { job1, job3 });
    assertTrue(result.isSuccess());
}
Also used : JobUtil.waitForJob(org.opencastproject.util.JobUtil.waitForJob) Result(org.opencastproject.job.api.JobBarrier.Result) Test(org.junit.Test)

Example 2 with Result

use of org.opencastproject.job.api.JobBarrier.Result in project opencast by opencast.

the class SeriesUpdatedEventHandler method handleEvent.

public void handleEvent(final SeriesItem seriesItem) {
    // A series or its ACL has been updated. Find any mediapackages with that series, and update them.
    logger.debug("Handling {}", seriesItem);
    String seriesId = seriesItem.getSeriesId();
    // We must be an administrative user to make this query
    final User prevUser = securityService.getUser();
    final Organization prevOrg = securityService.getOrganization();
    try {
        securityService.setUser(SecurityUtil.createSystemUser(systemAccount, prevOrg));
        SearchQuery q = new SearchQuery().withSeriesId(seriesId);
        SearchResult result = searchService.getForAdministrativeRead(q);
        for (SearchResultItem item : result.getItems()) {
            MediaPackage mp = item.getMediaPackage();
            Organization org = organizationDirectoryService.getOrganization(item.getOrganization());
            securityService.setOrganization(org);
            // to the distribution channels as well
            if (SeriesItem.Type.UpdateAcl.equals(seriesItem.getType())) {
                // Build a new XACML file for this mediapackage
                Attachment fileRepoCopy = authorizationService.setAcl(mp, AclScope.Series, seriesItem.getAcl()).getB();
                // Distribute the updated XACML file
                Job distributionJob = distributionService.distribute(CHANNEL_ID, mp, fileRepoCopy.getIdentifier());
                JobBarrier barrier = new JobBarrier(null, serviceRegistry, distributionJob);
                Result jobResult = barrier.waitForJobs();
                if (jobResult.getStatus().get(distributionJob).equals(FINISHED)) {
                    mp.remove(fileRepoCopy);
                    mp.add(getFromXml(serviceRegistry.getJob(distributionJob.getId()).getPayload()));
                } else {
                    logger.error("Unable to distribute XACML {}", fileRepoCopy.getIdentifier());
                    continue;
                }
            }
            // Update the series dublin core
            if (SeriesItem.Type.UpdateCatalog.equals(seriesItem.getType())) {
                DublinCoreCatalog seriesDublinCore = seriesItem.getMetadata();
                mp.setSeriesTitle(seriesDublinCore.getFirst(DublinCore.PROPERTY_TITLE));
                // Update the series dublin core
                Catalog[] seriesCatalogs = mp.getCatalogs(MediaPackageElements.SERIES);
                if (seriesCatalogs.length == 1) {
                    Catalog c = seriesCatalogs[0];
                    String filename = FilenameUtils.getName(c.getURI().toString());
                    URI uri = workspace.put(mp.getIdentifier().toString(), c.getIdentifier(), filename, dublinCoreService.serialize(seriesDublinCore));
                    c.setURI(uri);
                    // setting the URI to a new source so the checksum will most like be invalid
                    c.setChecksum(null);
                    // Distribute the updated series dc
                    Job distributionJob = distributionService.distribute(CHANNEL_ID, mp, c.getIdentifier());
                    JobBarrier barrier = new JobBarrier(null, serviceRegistry, distributionJob);
                    Result jobResult = barrier.waitForJobs();
                    if (jobResult.getStatus().get(distributionJob).equals(FINISHED)) {
                        mp.remove(c);
                        mp.add(getFromXml(serviceRegistry.getJob(distributionJob.getId()).getPayload()));
                    } else {
                        logger.error("Unable to distribute series catalog {}", c.getIdentifier());
                        continue;
                    }
                }
            }
            // Remove the series catalog and isPartOf from episode catalog
            if (SeriesItem.Type.Delete.equals(seriesItem.getType())) {
                mp.setSeries(null);
                mp.setSeriesTitle(null);
                boolean retractSeriesCatalog = retractSeriesCatalog(mp);
                boolean updateEpisodeCatalog = updateEpisodeCatalog(mp);
                if (!retractSeriesCatalog || !updateEpisodeCatalog)
                    continue;
            }
            // Update the search index with the modified mediapackage
            Job searchJob = searchService.add(mp);
            JobBarrier barrier = new JobBarrier(null, serviceRegistry, searchJob);
            barrier.waitForJobs();
        }
    } catch (SearchException e) {
        logger.warn("Unable to find mediapackages in search: ", e.getMessage());
    } catch (UnauthorizedException e) {
        logger.warn(e.getMessage());
    } catch (MediaPackageException e) {
        logger.warn(e.getMessage());
    } catch (ServiceRegistryException e) {
        logger.warn(e.getMessage());
    } catch (NotFoundException e) {
        logger.warn(e.getMessage());
    } catch (IOException e) {
        logger.warn(e.getMessage());
    } catch (DistributionException e) {
        logger.warn(e.getMessage());
    } finally {
        securityService.setOrganization(prevOrg);
        securityService.setUser(prevUser);
    }
}
Also used : SearchQuery(org.opencastproject.search.api.SearchQuery) MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) User(org.opencastproject.security.api.User) Organization(org.opencastproject.security.api.Organization) SearchResultItem(org.opencastproject.search.api.SearchResultItem) SearchException(org.opencastproject.search.api.SearchException) NotFoundException(org.opencastproject.util.NotFoundException) SearchResult(org.opencastproject.search.api.SearchResult) Attachment(org.opencastproject.mediapackage.Attachment) IOException(java.io.IOException) JobBarrier(org.opencastproject.job.api.JobBarrier) URI(java.net.URI) Catalog(org.opencastproject.mediapackage.Catalog) DublinCoreCatalog(org.opencastproject.metadata.dublincore.DublinCoreCatalog) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException) Result(org.opencastproject.job.api.JobBarrier.Result) SearchResult(org.opencastproject.search.api.SearchResult) MediaPackage(org.opencastproject.mediapackage.MediaPackage) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException) DistributionException(org.opencastproject.distribution.api.DistributionException) Job(org.opencastproject.job.api.Job) DublinCoreCatalog(org.opencastproject.metadata.dublincore.DublinCoreCatalog)

Example 3 with Result

use of org.opencastproject.job.api.JobBarrier.Result in project opencast by opencast.

the class SeriesUpdatedEventHandler method retractSeriesCatalog.

private boolean retractSeriesCatalog(MediaPackage mp) throws DistributionException {
    // Retract the series catalog
    for (Catalog c : mp.getCatalogs(MediaPackageElements.SERIES)) {
        Job retractJob = distributionService.retract(CHANNEL_ID, mp, c.getIdentifier());
        JobBarrier barrier = new JobBarrier(null, serviceRegistry, retractJob);
        Result jobResult = barrier.waitForJobs();
        if (jobResult.getStatus().get(retractJob).equals(FINISHED)) {
            mp.remove(c);
        } else {
            logger.error("Unable to retract series catalog {}", c.getIdentifier());
            return false;
        }
    }
    return true;
}
Also used : Job(org.opencastproject.job.api.Job) JobBarrier(org.opencastproject.job.api.JobBarrier) Catalog(org.opencastproject.mediapackage.Catalog) DublinCoreCatalog(org.opencastproject.metadata.dublincore.DublinCoreCatalog) Result(org.opencastproject.job.api.JobBarrier.Result) SearchResult(org.opencastproject.search.api.SearchResult)

Example 4 with Result

use of org.opencastproject.job.api.JobBarrier.Result in project opencast by opencast.

the class ConfigurablePublishWorkflowOperationHandlerTest method testNormal.

@Test
public void testNormal() throws WorkflowOperationException, URISyntaxException, DistributionException, MediaPackageException {
    String channelId = "engage-player";
    String attachmentId = "attachment-id";
    String catalogId = "catalog-id";
    String trackId = "track-id";
    Attachment attachment = new AttachmentImpl();
    attachment.addTag("engage-download");
    attachment.setIdentifier(attachmentId);
    attachment.setURI(new URI("http://api.com/attachment"));
    Catalog catalog = CatalogImpl.newInstance();
    catalog.addTag("engage-download");
    catalog.setIdentifier(catalogId);
    catalog.setURI(new URI("http://api.com/catalog"));
    Track track = new TrackImpl();
    track.addTag("engage-streaming");
    track.setIdentifier(trackId);
    track.setURI(new URI("http://api.com/track"));
    Publication publicationtest = new PublicationImpl(trackId, channelId, new URI("http://api.com/publication"), MimeType.mimeType(trackId, trackId));
    Track unrelatedTrack = new TrackImpl();
    unrelatedTrack.addTag("unrelated");
    Capture<MediaPackageElement> capturePublication = Capture.newInstance();
    MediaPackage mediapackageClone = EasyMock.createNiceMock(MediaPackage.class);
    EasyMock.expect(mediapackageClone.getElements()).andStubReturn(new MediaPackageElement[] { attachment, catalog, track, unrelatedTrack });
    EasyMock.expect(mediapackageClone.getIdentifier()).andStubReturn(new IdImpl("mp-id-clone"));
    EasyMock.expectLastCall();
    EasyMock.replay(mediapackageClone);
    MediaPackage mediapackage = EasyMock.createNiceMock(MediaPackage.class);
    EasyMock.expect(mediapackage.getElements()).andStubReturn(new MediaPackageElement[] { attachment, catalog, track, unrelatedTrack });
    EasyMock.expect(mediapackage.clone()).andStubReturn(mediapackageClone);
    EasyMock.expect(mediapackage.getIdentifier()).andStubReturn(new IdImpl("mp-id"));
    mediapackage.add(EasyMock.capture(capturePublication));
    mediapackage.add(publicationtest);
    EasyMock.expect(mediapackage.getPublications()).andStubReturn(new Publication[] { publicationtest });
    EasyMock.expectLastCall();
    EasyMock.replay(mediapackage);
    WorkflowOperationInstance op = EasyMock.createNiceMock(WorkflowOperationInstance.class);
    EasyMock.expect(op.getConfiguration(ConfigurablePublishWorkflowOperationHandler.CHANNEL_ID_KEY)).andStubReturn(channelId);
    EasyMock.expect(op.getConfiguration(ConfigurablePublishWorkflowOperationHandler.MIME_TYPE)).andStubReturn("text/html");
    EasyMock.expect(op.getConfiguration(ConfigurablePublishWorkflowOperationHandler.URL_PATTERN)).andStubReturn("http://api.opencast.org/api/events/${event_id}");
    EasyMock.expect(op.getConfiguration(ConfigurablePublishWorkflowOperationHandler.SOURCE_TAGS)).andStubReturn("engage-download,engage-streaming");
    EasyMock.expect(op.getConfiguration(ConfigurablePublishWorkflowOperationHandler.CHECK_AVAILABILITY)).andStubReturn("true");
    EasyMock.expect(op.getConfiguration(ConfigurablePublishWorkflowOperationHandler.STRATEGY)).andStubReturn("retract");
    EasyMock.expect(op.getConfiguration(ConfigurablePublishWorkflowOperationHandler.MODE)).andStubReturn("single");
    EasyMock.replay(op);
    WorkflowInstance workflowInstance = EasyMock.createNiceMock(WorkflowInstance.class);
    EasyMock.expect(workflowInstance.getMediaPackage()).andStubReturn(mediapackage);
    EasyMock.expect(workflowInstance.getCurrentOperation()).andStubReturn(op);
    EasyMock.replay(workflowInstance);
    JobContext jobContext = EasyMock.createNiceMock(JobContext.class);
    EasyMock.replay(jobContext);
    Job attachmentJob = EasyMock.createNiceMock(Job.class);
    EasyMock.expect(attachmentJob.getPayload()).andReturn(MediaPackageElementParser.getAsXml(attachment));
    EasyMock.replay(attachmentJob);
    Job catalogJob = EasyMock.createNiceMock(Job.class);
    EasyMock.expect(catalogJob.getPayload()).andReturn(MediaPackageElementParser.getAsXml(catalog));
    EasyMock.replay(catalogJob);
    Job trackJob = EasyMock.createNiceMock(Job.class);
    EasyMock.expect(trackJob.getPayload()).andReturn(MediaPackageElementParser.getAsXml(track));
    EasyMock.replay(trackJob);
    Job retractJob = EasyMock.createNiceMock(Job.class);
    EasyMock.expect(retractJob.getPayload()).andReturn(MediaPackageElementParser.getAsXml(track));
    EasyMock.replay(retractJob);
    DownloadDistributionService distributionService = EasyMock.createNiceMock(DownloadDistributionService.class);
    // Make sure that all of the elements are distributed.
    EasyMock.expect(distributionService.distribute(channelId, mediapackage, attachmentId, true)).andReturn(attachmentJob);
    EasyMock.expect(distributionService.distribute(channelId, mediapackage, catalogId, true)).andReturn(catalogJob);
    EasyMock.expect(distributionService.distribute(channelId, mediapackage, trackId, true)).andReturn(trackJob);
    EasyMock.expect(distributionService.retract(channelId, mediapackage, channelId)).andReturn(retractJob);
    EasyMock.replay(distributionService);
    SecurityService securityService = EasyMock.createNiceMock(SecurityService.class);
    EasyMock.expect(securityService.getOrganization()).andStubReturn(org);
    EasyMock.replay(securityService);
    ServiceRegistry serviceRegistry = EasyMock.createNiceMock(ServiceRegistry.class);
    EasyMock.replay(serviceRegistry);
    // Override the waitForStatus method to not block the jobs
    ConfigurablePublishWorkflowOperationHandler configurePublish = new ConfigurablePublishWorkflowOperationHandler() {

        @Override
        protected Result waitForStatus(long timeout, Job... jobs) {
            HashMap<Job, Status> map = Stream.mk(jobs).foldl(new HashMap<Job, Status>(), new Fn2<HashMap<Job, Status>, Job, HashMap<Job, Status>>() {

                @Override
                public HashMap<Job, Status> apply(HashMap<Job, Status> a, Job b) {
                    a.put(b, Status.FINISHED);
                    return a;
                }
            });
            return new Result(map);
        }
    };
    configurePublish.setDownloadDistributionService(distributionService);
    configurePublish.setSecurityService(securityService);
    configurePublish.setServiceRegistry(serviceRegistry);
    WorkflowOperationResult result = configurePublish.start(workflowInstance, jobContext);
    assertNotNull(result.getMediaPackage());
    assertTrue("The publication element has not been added to the mediapackage.", capturePublication.hasCaptured());
    assertTrue("Some other type of element has been added to the mediapackage instead of the publication element.", capturePublication.getValue().getElementType().equals(MediaPackageElement.Type.Publication));
    Publication publication = (Publication) capturePublication.getValue();
    assertEquals(1, publication.getAttachments().length);
    assertNotEquals(attachment.getIdentifier(), publication.getAttachments()[0].getIdentifier());
    attachment.setIdentifier(publication.getAttachments()[0].getIdentifier());
    assertEquals(attachment, publication.getAttachments()[0]);
    assertEquals(1, publication.getCatalogs().length);
    assertNotEquals(catalog.getIdentifier(), publication.getCatalogs()[0].getIdentifier());
    catalog.setIdentifier(publication.getCatalogs()[0].getIdentifier());
    assertEquals(catalog, publication.getCatalogs()[0]);
    assertEquals(1, publication.getTracks().length);
    assertNotEquals(track.getIdentifier(), publication.getTracks()[0].getIdentifier());
    track.setIdentifier(publication.getTracks()[0].getIdentifier());
    assertEquals(track, publication.getTracks()[0]);
}
Also used : HashMap(java.util.HashMap) TrackImpl(org.opencastproject.mediapackage.track.TrackImpl) Attachment(org.opencastproject.mediapackage.Attachment) WorkflowInstance(org.opencastproject.workflow.api.WorkflowInstance) URI(java.net.URI) IdImpl(org.opencastproject.mediapackage.identifier.IdImpl) WorkflowOperationResult(org.opencastproject.workflow.api.WorkflowOperationResult) Result(org.opencastproject.job.api.JobBarrier.Result) WorkflowOperationResult(org.opencastproject.workflow.api.WorkflowOperationResult) WorkflowOperationInstance(org.opencastproject.workflow.api.WorkflowOperationInstance) MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) SecurityService(org.opencastproject.security.api.SecurityService) JobContext(org.opencastproject.job.api.JobContext) Job(org.opencastproject.job.api.Job) Status(org.opencastproject.job.api.Job.Status) Publication(org.opencastproject.mediapackage.Publication) Catalog(org.opencastproject.mediapackage.Catalog) DownloadDistributionService(org.opencastproject.distribution.api.DownloadDistributionService) PublicationImpl(org.opencastproject.mediapackage.PublicationImpl) MediaPackage(org.opencastproject.mediapackage.MediaPackage) AttachmentImpl(org.opencastproject.mediapackage.attachment.AttachmentImpl) ServiceRegistry(org.opencastproject.serviceregistry.api.ServiceRegistry) Track(org.opencastproject.mediapackage.Track) Test(org.junit.Test)

Example 5 with Result

use of org.opencastproject.job.api.JobBarrier.Result in project opencast by opencast.

the class JobUtilTest method testWaitForJob.

@Test
public void testWaitForJob() {
    Job job1 = new JobImpl(1);
    job1.setStatus(Status.FINISHED);
    Job job2 = new JobImpl(1);
    job2.setStatus(Status.RUNNING);
    Job job3 = new JobImpl(1);
    job3.setStatus(Status.FINISHED);
    Result result = waitForJob(serviceRegistry, job1);
    assertTrue(result.isSuccess());
    result = waitForJob(job2, serviceRegistry, job3);
    assertTrue(result.isSuccess());
    result = waitForJob(serviceRegistry, Option.some(2000L), job1);
    assertTrue(result.isSuccess());
    result = waitForJob(serviceRegistry, Option.<Long>none(), job1);
    assertTrue(result.isSuccess());
    result = waitForJob(job2, serviceRegistry, Option.some(2000L), job3);
    assertTrue(result.isSuccess());
    result = waitForJob(job2, serviceRegistry, Option.some(2000L), job3);
    assertTrue(result.isSuccess());
}
Also used : JobUtil.waitForJob(org.opencastproject.util.JobUtil.waitForJob) Result(org.opencastproject.job.api.JobBarrier.Result) Test(org.junit.Test)

Aggregations

Result (org.opencastproject.job.api.JobBarrier.Result)7 Job (org.opencastproject.job.api.Job)5 URI (java.net.URI)4 Catalog (org.opencastproject.mediapackage.Catalog)4 Test (org.junit.Test)3 JobBarrier (org.opencastproject.job.api.JobBarrier)3 MediaPackage (org.opencastproject.mediapackage.MediaPackage)3 DublinCoreCatalog (org.opencastproject.metadata.dublincore.DublinCoreCatalog)3 SearchResult (org.opencastproject.search.api.SearchResult)3 HashMap (java.util.HashMap)2 Status (org.opencastproject.job.api.Job.Status)2 Attachment (org.opencastproject.mediapackage.Attachment)2 JobUtil.waitForJob (org.opencastproject.util.JobUtil.waitForJob)2 WorkflowInstance (org.opencastproject.workflow.api.WorkflowInstance)2 WorkflowOperationInstance (org.opencastproject.workflow.api.WorkflowOperationInstance)2 Fn2 (com.entwinemedia.fn.Fn2)1 IOException (java.io.IOException)1 Before (org.junit.Before)1 DistributionException (org.opencastproject.distribution.api.DistributionException)1 DownloadDistributionService (org.opencastproject.distribution.api.DownloadDistributionService)1