use of com.b2international.snowowl.core.jobs.RemoteJobs in project snow-owl by b2ihealthcare.
the class SearchMergeRequest method doExecute.
@Override
public Merges doExecute(RepositoryContext context) {
final ExpressionBuilder queryBuilder = Expressions.builder();
// add mergerequest type filter
queryBuilder.filter(RemoteJobEntry.Expressions.matchRequestType(ImmutableSet.of(BranchRebaseRequest.class.getSimpleName(), BranchMergeRequest.class.getSimpleName())));
if (containsKey(OptionKey.SOURCE)) {
queryBuilder.filter(RemoteJobEntry.Expressions.matchParameter(SOURCE_FIELD, getCollection(OptionKey.SOURCE, String.class)));
}
if (containsKey(OptionKey.TARGET)) {
queryBuilder.filter(RemoteJobEntry.Expressions.matchParameter(TARGET_FIELD, getCollection(OptionKey.TARGET, String.class)));
}
if (containsKey(OptionKey.STATUS)) {
queryBuilder.filter(RemoteJobEntry.Expressions.matchParameter(STATUS_FIELD, getCollection(OptionKey.STATUS, String.class)));
}
final RemoteJobs jobs = context.service(RemoteJobTracker.class).search(queryBuilder.build(), Integer.MAX_VALUE);
final ObjectMapper mapper = context.service(ObjectMapper.class);
final List<Merge> items = jobs.stream().map(job -> createMergefromJobEntry(job, mapper)).collect(Collectors.toList());
return new Merges(items, jobs.getSearchAfter(), jobs.getLimit(), jobs.getTotal());
}
use of com.b2international.snowowl.core.jobs.RemoteJobs in project snow-owl by b2ihealthcare.
the class ClassifyOperation method run.
/**
* Allocates a reasoner instance, performs the requested operation, then releases the borrowed instance back to the pool.
* @param monitor an {@link IProgressMonitor} to monitor operation progress
* @return the value returned by {@link #processResults(IProgressMonitor, long)}
* @throws OperationCanceledException
*/
public T run(final IProgressMonitor monitor) throws OperationCanceledException {
monitor.beginTask("Classification in progress...", IProgressMonitor.UNKNOWN);
try {
final String classificationId = UUID.randomUUID().toString();
final String jobId = IDs.sha1(classificationId);
final Notifications notifications = getServiceForClass(Notifications.class);
final BlockingQueue<RemoteJobEntry> jobQueue = Queues.newArrayBlockingQueue(1);
final Observable<RemoteJobEntry> jobObservable = notifications.ofType(RemoteJobNotification.class).filter(RemoteJobNotification::isChanged).filter(notification -> notification.getJobIds().contains(jobId)).concatMap(notification -> JobRequests.prepareSearch().one().filterById(jobId).buildAsync().execute(getEventBus())).map(RemoteJobs::first).map(Optional<RemoteJobEntry>::get).filter(RemoteJobEntry::isDone);
// "One-shot" subscription; it should self-destruct after the first notification
jobObservable.subscribe(new DisposableObserver<RemoteJobEntry>() {
@Override
public void onComplete() {
dispose();
}
@Override
public void onError(final Throwable t) {
dispose();
}
@Override
public void onNext(final RemoteJobEntry job) {
try {
jobQueue.put(job);
} catch (InterruptedException e) {
throw new SnowowlRuntimeException("Interrupted while trying to add a remote job entry to the queue.", e);
} finally {
dispose();
}
}
});
ClassificationRequests.prepareCreateClassification().setClassificationId(classificationId).setReasonerId(reasonerId).setUserId(userId).addAllConcepts(additionalConcepts).setParentLockContext(parentLockContext).build(branch).get(ApplicationContext.getServiceForClass(Environment.class));
while (true) {
if (monitor.isCanceled()) {
throw new OperationCanceledException();
}
try {
final RemoteJobEntry jobEntry = jobQueue.poll(CHECK_JOB_INTERVAL_SECONDS, TimeUnit.SECONDS);
if (jobEntry == null) {
continue;
}
switch(jobEntry.getState()) {
// $FALL-THROUGH$
case SCHEDULED:
case RUNNING:
case CANCEL_REQUESTED:
break;
case FINISHED:
try {
return processResults(classificationId);
} finally {
deleteEntry(jobId);
}
case CANCELED:
deleteEntry(jobId);
throw new OperationCanceledException();
case FAILED:
deleteEntry(jobId);
throw new SnowowlRuntimeException("Failed to retrieve the results of the classification.");
default:
throw new IllegalStateException("Unexpected state '" + jobEntry.getState() + "'.");
}
} catch (final InterruptedException e) {
// Nothing to do
}
}
} finally {
monitor.done();
}
}
Aggregations