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!");
}
}
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);
}
}
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);
}
}
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();
}
}
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);
}
Aggregations