use of org.opencastproject.util.data.Effect0 in project opencast by opencast.
the class EventCommentDatabaseServiceImpl method repopulate.
@Override
public void repopulate(final String indexName) throws Exception {
final String destinationId = CommentItem.COMMENT_QUEUE_PREFIX + WordUtils.capitalize(indexName);
try {
final int total = countComments();
final int[] current = new int[1];
current[0] = 0;
logger.info("Re-populating index '{}' with comments for events. There are {} events with comments to add", indexName, total);
final int responseInterval = (total < 100) ? 1 : (total / 100);
final Map<String, List<String>> eventsWithComments = getEventsWithComments();
for (String orgId : eventsWithComments.keySet()) {
Organization organization = organizationDirectoryService.getOrganization(orgId);
SecurityUtil.runAs(securityService, organization, SecurityUtil.createSystemUser(cc, organization), new Effect0() {
@Override
protected void run() {
for (String eventId : eventsWithComments.get(orgId)) {
try {
List<EventComment> comments = getComments(eventId);
boolean hasOpenComments = !Stream.$(comments).filter(filterOpenComments).toList().isEmpty();
boolean needsCutting = !Stream.$(comments).filter(filterNeedsCuttingComment).toList().isEmpty();
messageSender.sendObjectMessage(destinationId, MessageSender.DestinationType.Queue, CommentItem.update(eventId, !comments.isEmpty(), hasOpenComments, needsCutting));
current[0] += comments.size();
if (responseInterval == 1 || comments.size() > responseInterval || current[0] == total || current[0] % responseInterval < comments.size()) {
messageSender.sendObjectMessage(IndexProducer.RESPONSE_QUEUE, MessageSender.DestinationType.Queue, IndexRecreateObject.update(indexName, IndexRecreateObject.Service.Comments, total, current[0]));
}
} catch (EventCommentDatabaseException e) {
logger.error("Unable to retrieve event comments for organization {}", orgId, e);
} catch (Throwable t) {
logger.error("Unable to update comment on event {} for organization {}", eventId, orgId, t);
}
}
}
});
}
} catch (Exception e) {
logger.warn("Unable to index event comments", e);
throw new ServiceException(e.getMessage());
}
Organization organization = new DefaultOrganization();
SecurityUtil.runAs(securityService, organization, SecurityUtil.createSystemUser(cc, organization), new Effect0() {
@Override
protected void run() {
messageSender.sendObjectMessage(IndexProducer.RESPONSE_QUEUE, MessageSender.DestinationType.Queue, IndexRecreateObject.end(indexName, IndexRecreateObject.Service.Comments));
}
});
}
use of org.opencastproject.util.data.Effect0 in project opencast by opencast.
the class IoSupport method acquireLock.
/**
* Acquire a lock on a file. Return a key to release the lock.
*
* @return a key to release the lock
*
* @throws NotFoundException
* if the path to the file, to create a lock for, does not exist
* @throws IOException
* if the file lock can not be created due to access limitations
*/
private static Effect0 acquireLock(File file) throws NotFoundException, IOException {
final RandomAccessFile raf;
try {
raf = new RandomAccessFile(file, "rw");
} catch (FileNotFoundException e) {
// make sure to create all parent directories before locking the file
throw new NotFoundException("Error acquiring lock for " + file.getAbsolutePath(), e);
}
final FileLock lock = raf.getChannel().lock();
return new Effect0() {
@Override
protected void run() {
try {
lock.release();
} catch (IOException ignore) {
}
IoSupport.closeQuietly(raf);
}
};
}
use of org.opencastproject.util.data.Effect0 in project opencast by opencast.
the class EventsLoader method addArchiveEntry.
private void addArchiveEntry(final MediaPackage mediaPackage) {
final User user = securityService.getUser();
final Organization organization = securityService.getOrganization();
singleThreadExecutor.execute(new Runnable() {
@Override
public void run() {
SecurityUtil.runAs(securityService, organization, user, new Effect0() {
@Override
protected void run() {
assetManager.takeSnapshot(DEFAULT_OWNER, mediaPackage);
}
});
}
});
}
use of org.opencastproject.util.data.Effect0 in project opencast by opencast.
the class IndexServiceImpl method updateCommentCatalog.
@Override
public void updateCommentCatalog(final Event event, final List<EventComment> comments) throws Exception {
final SecurityContext securityContext = new SecurityContext(securityService, securityService.getOrganization(), securityService.getUser());
executorService.execute(new Runnable() {
@Override
public void run() {
securityContext.runInContext(new Effect0() {
@Override
protected void run() {
try {
MediaPackage mediaPackage = getEventMediapackage(event);
updateMediaPackageCommentCatalog(mediaPackage, comments);
switch(getEventSource(event)) {
case WORKFLOW:
logger.info("Update workflow mediapacakge {} with updated comments catalog.", event.getIdentifier());
Opt<WorkflowInstance> workflowInstance = getCurrentWorkflowInstance(event.getIdentifier());
if (workflowInstance.isNone()) {
logger.error("No workflow instance for event {} found!", event.getIdentifier());
throw new IndexServiceException("No workflow instance found for event " + event.getIdentifier());
}
WorkflowInstance instance = workflowInstance.get();
instance.setMediaPackage(mediaPackage);
updateWorkflowInstance(instance);
break;
case ARCHIVE:
logger.info("Update archive mediapacakge {} with updated comments catalog.", event.getIdentifier());
assetManager.takeSnapshot(DEFAULT_OWNER, mediaPackage);
break;
case SCHEDULE:
logger.info("Update scheduled mediapacakge {} with updated comments catalog.", event.getIdentifier());
schedulerService.updateEvent(event.getIdentifier(), Opt.<Date>none(), Opt.<Date>none(), Opt.<String>none(), Opt.<Set<String>>none(), Opt.some(mediaPackage), Opt.<Map<String, String>>none(), Opt.<Map<String, String>>none(), Opt.<Opt<Boolean>>none(), SchedulerService.ORIGIN);
break;
default:
logger.error("Unkown event source {}!", event.getSource().toString());
}
} catch (Exception e) {
logger.error("Unable to update event {} comment catalog: {}", event.getIdentifier(), getStackTrace(e));
}
}
});
}
});
}
use of org.opencastproject.util.data.Effect0 in project opencast by opencast.
the class ThemesServiceDatabaseImpl method repopulate.
@Override
public void repopulate(final String indexName) {
final String destinationId = ThemeItem.THEME_QUEUE_PREFIX + WordUtils.capitalize(indexName);
for (final Organization organization : organizationDirectoryService.getOrganizations()) {
SecurityUtil.runAs(securityService, organization, SecurityUtil.createSystemUser(cc, organization), new Effect0() {
@Override
protected void run() {
try {
final List<Theme> themes = getThemes();
int total = themes.size();
int current = 1;
logger.info("Re-populating '{}' index with themes from organization {}. There are {} theme(s) to add to the index.", indexName, securityService.getOrganization().getId(), total);
for (Theme theme : themes) {
messageSender.sendObjectMessage(destinationId, MessageSender.DestinationType.Queue, ThemeItem.update(toSerializableTheme(theme)));
messageSender.sendObjectMessage(IndexProducer.RESPONSE_QUEUE, MessageSender.DestinationType.Queue, IndexRecreateObject.update(indexName, IndexRecreateObject.Service.Themes, total, current));
current++;
}
} catch (ThemesServiceDatabaseException e) {
logger.error("Unable to get themes from the database because: {}", ExceptionUtils.getStackTrace(e));
throw new IllegalStateException(e);
}
}
});
}
Organization organization = new DefaultOrganization();
SecurityUtil.runAs(securityService, organization, SecurityUtil.createSystemUser(cc, organization), new Effect0() {
@Override
protected void run() {
messageSender.sendObjectMessage(IndexProducer.RESPONSE_QUEUE, MessageSender.DestinationType.Queue, IndexRecreateObject.end(indexName, IndexRecreateObject.Service.Themes));
}
});
}
Aggregations