Search in sources :

Example 6 with SchedulerTransaction

use of org.opencastproject.scheduler.api.SchedulerService.SchedulerTransaction in project opencast by opencast.

the class SchedulerServiceImplTest method testTransactionCommitCollision.

@Test
public void testTransactionCommitCollision() throws Exception {
    Date start = new Date();
    Date end = new Date(System.currentTimeMillis() + 60000);
    String captureDeviceID = "demo";
    String seriesId = "series1";
    Set<String> userIds = new HashSet<>();
    userIds.add("user1");
    userIds.add("user2");
    MediaPackage mp = generateEvent(Opt.<String>none());
    mp.setSeries(seriesId);
    DublinCoreCatalog event = generateEvent(captureDeviceID, start, end);
    addDublinCore(Opt.<String>none(), mp, event);
    Map<String, String> caProperties = generateCaptureAgentMetadata("demo");
    MediaPackage mp2 = generateEvent(Opt.<String>none());
    try {
        schedSvc.addEvent(start, end, captureDeviceID, userIds, mp2, wfPropertiesUpdated, caProperties, Opt.some(false), Opt.some("existing"), SchedulerService.ORIGIN);
    } catch (SchedulerTransactionLockException e) {
        fail("Transaction create lock not working!");
    }
    /* Test transaction collision of already existing event and new transaction event */
    SchedulerTransaction trx = schedSvc.createTransaction("new");
    assertEquals("new", trx.getSource());
    AQueryBuilder query = assetManager.createQuery();
    AResult result = query.select(query.snapshot(), query.properties()).where(query.organizationId().eq(new DefaultOrganization().getId()).and(query.mediaPackageId(mp.getIdentifier().compact()))).run();
    Opt<ARecord> record = result.getRecords().head();
    assertFalse(record.isSome());
    trx.addEvent(start, end, captureDeviceID, userIds, mp, wfPropertiesUpdated, caProperties, Opt.some(false));
    query = assetManager.createQuery();
    result = query.select(query.snapshot(), query.properties()).where(query.organizationId().eq(new DefaultOrganization().getId()).and(query.mediaPackageId(mp.getIdentifier().compact()))).run();
    record = result.getRecords().head();
    assertTrue(record.isSome());
    try {
        trx.commit();
        fail("Pre-conflict detection not working!");
    } catch (SchedulerConflictException e) {
        assertNotNull(e);
    }
    query = assetManager.createQuery();
    result = query.select(query.snapshot(), query.properties()).where(query.organizationId().eq(new DefaultOrganization().getId()).and(query.mediaPackageId(mp.getIdentifier().compact()))).run();
    record = result.getRecords().head();
    assertTrue(record.isSome());
    try {
        schedSvc.getTransaction(trx.getId());
    } catch (NotFoundException e) {
        fail("Transaction found!");
    }
    /* Test transaction collision of already existing event and new opted out transaction event */
    MediaPackage mp4 = (MediaPackage) mp.clone();
    mp4.setIdentifier(new IdImpl("newuuid"));
    SchedulerTransaction trx2 = schedSvc.createTransaction("optedout");
    assertEquals("optedout", trx2.getSource());
    query = assetManager.createQuery();
    result = query.select(query.snapshot(), query.properties()).where(query.organizationId().eq(new DefaultOrganization().getId()).and(query.mediaPackageId(mp4.getIdentifier().compact()))).run();
    record = result.getRecords().head();
    assertFalse(record.isSome());
    trx2.addEvent(start, end, captureDeviceID, userIds, mp4, wfPropertiesUpdated, caProperties, Opt.some(true));
    query = assetManager.createQuery();
    result = query.select(query.snapshot(), query.properties()).where(query.organizationId().eq(new DefaultOrganization().getId()).and(query.mediaPackageId(mp4.getIdentifier().compact()))).run();
    record = result.getRecords().head();
    assertTrue(record.isSome());
    try {
        trx2.commit();
    } catch (SchedulerConflictException e) {
        fail("Pre-conflict detection not working!");
    }
    /* Test transaction collision of two transaction events */
    schedSvc.removeEvent(mp2.getIdentifier().compact());
    MediaPackage mp3 = generateEvent(Opt.<String>none());
    trx.addEvent(start, end, captureDeviceID, userIds, mp3, wfPropertiesUpdated, caProperties, Opt.some(false));
    try {
        trx.commit();
        fail("Pre-conflict detection not working!");
    } catch (SchedulerConflictException e) {
        assertNotNull(e);
    }
    query = assetManager.createQuery();
    result = query.select(query.snapshot(), query.properties()).where(query.organizationId().eq(new DefaultOrganization().getId()).and(query.mediaPackageId(mp.getIdentifier().compact()))).run();
    record = result.getRecords().head();
    assertTrue(record.isSome());
    try {
        schedSvc.getTransaction(trx.getId());
    } catch (NotFoundException e) {
        fail("Transaction found!");
    }
    /* Test transaction collision of two transaction events but one opted out */
    MediaPackage mp5 = (MediaPackage) mp.clone();
    mp5.setIdentifier(new IdImpl("newuuid2"));
    SchedulerTransaction trx3 = schedSvc.createTransaction("optedout2");
    assertEquals("optedout2", trx3.getSource());
    query = assetManager.createQuery();
    result = query.select(query.snapshot(), query.properties()).where(query.organizationId().eq(new DefaultOrganization().getId()).and(query.mediaPackageId(mp5.getIdentifier().compact()))).run();
    record = result.getRecords().head();
    assertFalse(record.isSome());
    trx3.addEvent(start, end, captureDeviceID, userIds, mp5, wfPropertiesUpdated, caProperties, Opt.some(false));
    query = assetManager.createQuery();
    result = query.select(query.snapshot(), query.properties()).where(query.organizationId().eq(new DefaultOrganization().getId()).and(query.mediaPackageId(mp5.getIdentifier().compact()))).run();
    record = result.getRecords().head();
    assertTrue(record.isSome());
    MediaPackage mp6 = generateEvent(Opt.<String>none());
    trx.addEvent(start, end, captureDeviceID, userIds, mp6, wfPropertiesUpdated, caProperties, Opt.some(true));
    try {
        trx3.commit();
    } catch (SchedulerConflictException e) {
        fail("Pre-conflict detection not working!");
    }
}
Also used : SchedulerConflictException(org.opencastproject.scheduler.api.SchedulerConflictException) AQueryBuilder(org.opencastproject.assetmanager.api.query.AQueryBuilder) NotFoundException(org.opencastproject.util.NotFoundException) SchedulerTransaction(org.opencastproject.scheduler.api.SchedulerService.SchedulerTransaction) Date(java.util.Date) IdImpl(org.opencastproject.mediapackage.identifier.IdImpl) ARecord(org.opencastproject.assetmanager.api.query.ARecord) SchedulerTransactionLockException(org.opencastproject.scheduler.api.SchedulerTransactionLockException) MediaPackage(org.opencastproject.mediapackage.MediaPackage) RichAResult(org.opencastproject.assetmanager.api.query.RichAResult) AResult(org.opencastproject.assetmanager.api.query.AResult) DublinCoreCatalog(org.opencastproject.metadata.dublincore.DublinCoreCatalog) HashSet(java.util.HashSet) DefaultOrganization(org.opencastproject.security.api.DefaultOrganization) Test(org.junit.Test)

Example 7 with SchedulerTransaction

use of org.opencastproject.scheduler.api.SchedulerService.SchedulerTransaction in project opencast by opencast.

the class SchedulerRestService method rollbackTransaction.

@POST
@Path("/transaction/{id}/rollback")
@RestQuery(name = "rollbacktransaction", description = "Rolls back the scheduler transaction with specified id", returnDescription = "Successfully rolled back scheduler transaction", reponses = { @RestResponse(responseCode = SC_OK, description = "Successfully rolled back scheduler transaction"), @RestResponse(responseCode = SC_NOT_FOUND, description = "Scheduler transaction with specified ID does not exist"), @RestResponse(responseCode = SC_UNAUTHORIZED, description = "You do not have permission to rollback the scheduler transaction. Maybe you need to authenticate.") }, pathParameters = { @RestParameter(name = "id", type = RestParameter.Type.STRING, isRequired = true, description = "ID of scheduler transaction") })
public Response rollbackTransaction(@PathParam("id") String transactionId) throws UnauthorizedException, NotFoundException {
    try {
        SchedulerTransaction transaction = service.getTransaction(transactionId);
        transaction.rollback();
        return Response.ok().build();
    } catch (SchedulerException e) {
        logger.error("Unable to rollback scheduler transaction '{}': {}", transactionId, getStackTrace(e));
        throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR);
    }
}
Also used : SchedulerException(org.opencastproject.scheduler.api.SchedulerException) WebApplicationException(javax.ws.rs.WebApplicationException) SchedulerTransaction(org.opencastproject.scheduler.api.SchedulerService.SchedulerTransaction) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) RestQuery(org.opencastproject.util.doc.rest.RestQuery)

Example 8 with SchedulerTransaction

use of org.opencastproject.scheduler.api.SchedulerService.SchedulerTransaction in project opencast by opencast.

the class SchedulerRestService method commitTransaction.

@POST
@Path("/transaction/{id}/commit")
@RestQuery(name = "committransaction", description = "Commits the scheduler transaction with specified id", returnDescription = "Successfully committed scheduler transaction", reponses = { @RestResponse(responseCode = SC_OK, description = "Successfully committed scheduler transaction"), @RestResponse(responseCode = SC_NOT_FOUND, description = "Scheduler transaction with specified ID does not exist"), @RestResponse(responseCode = SC_UNAUTHORIZED, description = "You do not have permission to commit the scheduler transaction. Maybe you need to authenticate.") }, pathParameters = { @RestParameter(name = "id", type = RestParameter.Type.STRING, isRequired = true, description = "ID of scheduler transaction") })
public Response commitTransaction(@PathParam("id") String transactionId) throws UnauthorizedException, NotFoundException {
    try {
        SchedulerTransaction transaction = service.getTransaction(transactionId);
        transaction.commit();
        return Response.ok().build();
    } catch (SchedulerException e) {
        logger.error("Unable to commit scheduler transaction '{}': {}", transactionId, getStackTrace(e));
        throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR);
    }
}
Also used : SchedulerException(org.opencastproject.scheduler.api.SchedulerException) WebApplicationException(javax.ws.rs.WebApplicationException) SchedulerTransaction(org.opencastproject.scheduler.api.SchedulerService.SchedulerTransaction) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) RestQuery(org.opencastproject.util.doc.rest.RestQuery)

Example 9 with SchedulerTransaction

use of org.opencastproject.scheduler.api.SchedulerService.SchedulerTransaction in project opencast by opencast.

the class SchedulerMigrationService method migrateScheduledEvents.

private void migrateScheduledEvents() throws SQLException {
    SchedulerTransaction tx = null;
    ResultSet result = null;
    Statement stm = null;
    try (Connection connection = dataSource.getConnection()) {
        logger.info("Scheduler transaction | start");
        tx = schedulerService.createTransaction("opencast");
        stm = connection.createStatement();
        result = stm.executeQuery("SELECT id, access_control, blacklisted, capture_agent_metadata, dublin_core, mediapackage_id, opt_out, review_date, review_status FROM mh_scheduled_event");
        List<Event> events = transform(result);
        for (Event event : events) {
            // Filtering in advance is dangerous because the whole process lasts very long
            if (!isOutdated(event)) {
                schedule(tx, event);
                logger.info("Migrated event '{}'", event.mediaPackageId);
            } else {
                logger.info("Ignoring outdated event '{}'", event.mediaPackageId);
            }
        }
        tx.commit();
        // Update review status
        for (Event event : events) {
            if (!isOutdated(event)) {
                schedulerService.updateReviewStatus(event.mediaPackageId, event.reviewStatus);
            }
        }
        logger.info("Scheduler transaction | end");
    } catch (Exception e) {
        final String stackTrace = ExceptionUtils.getStackTrace(e);
        logger.error(format("Scheduler transaction | error\n%s", stackTrace));
        if (tx != null) {
            logger.error("Scheduler transaction | rollback transaction");
            try {
                tx.rollback();
            } catch (Exception e2) {
                final String stackTrace2 = ExceptionUtils.getStackTrace(e2);
                logger.error(format("Scheduler transaction | error doing rollback\n%s", stackTrace2));
            }
        }
    } finally {
        if (result != null)
            result.close();
        if (stm != null)
            stm.close();
    }
}
Also used : Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) Connection(java.sql.Connection) SchedulerTransaction(org.opencastproject.scheduler.api.SchedulerService.SchedulerTransaction) SchedulerException(org.opencastproject.scheduler.api.SchedulerException) ConfigurationException(org.osgi.service.cm.ConfigurationException) MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) SAXException(org.xml.sax.SAXException) SQLException(java.sql.SQLException) UnauthorizedException(org.opencastproject.security.api.UnauthorizedException) NotFoundException(org.opencastproject.util.NotFoundException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 10 with SchedulerTransaction

use of org.opencastproject.scheduler.api.SchedulerService.SchedulerTransaction in project opencast by opencast.

the class SchedulerMigrationServiceTest method setUp.

@Before
public void setUp() throws Exception {
    OrganizationDirectoryService orgDirService = createNiceMock(OrganizationDirectoryService.class);
    expect(orgDirService.getOrganization(anyString())).andReturn(new DefaultOrganization()).anyTimes();
    replay(orgDirService);
    SecurityService securityService = createNiceMock(SecurityService.class);
    expect(securityService.getOrganization()).andReturn(new DefaultOrganization()).anyTimes();
    expect(securityService.getUser()).andReturn(new JaxbUser()).anyTimes();
    replay(securityService);
    SchedulerTransaction schedulerTransaction = createNiceMock(SchedulerTransaction.class);
    replay(schedulerTransaction);
    SchedulerService schedulerService = createNiceMock(SchedulerService.class);
    expect(schedulerService.createTransaction(anyString())).andReturn(schedulerTransaction).anyTimes();
    expect(schedulerService.search(anyObject(Opt.class), anyObject(Opt.class), anyObject(Opt.class), anyObject(Opt.class), anyObject(Opt.class))).andReturn(new ArrayList<>());
    replay(schedulerService);
    Workspace workspace = createNiceMock(Workspace.class);
    expect(workspace.put(anyString(), anyString(), anyString(), anyObject(InputStream.class))).andReturn(new URI("test")).anyTimes();
    replay(workspace);
    AuthorizationService authorizationService = createNiceMock(AuthorizationService.class);
    replay(authorizationService);
    schedulerMigrationService.setAuthorizationService(authorizationService);
    schedulerMigrationService.setOrganizationDirectoryService(orgDirService);
    schedulerMigrationService.setSchedulerService(schedulerService);
    schedulerMigrationService.setSecurityService(securityService);
    schedulerMigrationService.setWorkspace(workspace);
}
Also used : SchedulerService(org.opencastproject.scheduler.api.SchedulerService) Opt(com.entwinemedia.fn.data.Opt) AuthorizationService(org.opencastproject.security.api.AuthorizationService) SecurityService(org.opencastproject.security.api.SecurityService) JaxbUser(org.opencastproject.security.api.JaxbUser) SchedulerTransaction(org.opencastproject.scheduler.api.SchedulerService.SchedulerTransaction) URI(java.net.URI) OrganizationDirectoryService(org.opencastproject.security.api.OrganizationDirectoryService) DefaultOrganization(org.opencastproject.security.api.DefaultOrganization) Workspace(org.opencastproject.workspace.api.Workspace) Before(org.junit.Before)

Aggregations

SchedulerTransaction (org.opencastproject.scheduler.api.SchedulerService.SchedulerTransaction)12 HashSet (java.util.HashSet)8 MediaPackage (org.opencastproject.mediapackage.MediaPackage)8 Date (java.util.Date)7 Test (org.junit.Test)7 DublinCoreCatalog (org.opencastproject.metadata.dublincore.DublinCoreCatalog)7 DefaultOrganization (org.opencastproject.security.api.DefaultOrganization)7 NotFoundException (org.opencastproject.util.NotFoundException)7 AQueryBuilder (org.opencastproject.assetmanager.api.query.AQueryBuilder)6 AResult (org.opencastproject.assetmanager.api.query.AResult)6 RichAResult (org.opencastproject.assetmanager.api.query.RichAResult)6 SchedulerTransactionLockException (org.opencastproject.scheduler.api.SchedulerTransactionLockException)5 ARecord (org.opencastproject.assetmanager.api.query.ARecord)4 SchedulerConflictException (org.opencastproject.scheduler.api.SchedulerConflictException)4 SchedulerException (org.opencastproject.scheduler.api.SchedulerException)4 Path (javax.ws.rs.Path)3 WebApplicationException (javax.ws.rs.WebApplicationException)3 RestQuery (org.opencastproject.util.doc.rest.RestQuery)3 IOException (java.io.IOException)2 POST (javax.ws.rs.POST)2