Skip to content

Commit

Permalink
Allow TM Merge (project version) to be cancelled
Browse files Browse the repository at this point in the history
  • Loading branch information
seanf committed May 23, 2018
1 parent 52757b9 commit 1f7f9cd
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 23 deletions.
Expand Up @@ -24,6 +24,8 @@
import java.util.List;
import java.util.concurrent.Future;

import javax.annotation.Nonnull;

import org.zanata.async.handle.MergeTranslationsTaskHandle;
import org.zanata.async.handle.TransMemoryMergeTaskHandle;
import org.zanata.rest.dto.VersionTMMerge;
Expand Down Expand Up @@ -56,12 +58,8 @@ Future<List<TranslationService.TranslationResult>> executeMergeAsync(TransMemory

/**
* TM merge for a project version
* @param versionId
* @param mergeRequest
* @param handle
* @return
*/
Future<Void> startMergeTranslations(Long versionId,
VersionTMMerge mergeRequest,
MergeTranslationsTaskHandle handle);
@Nonnull MergeTranslationsTaskHandle asyncTaskHandle);
}
Expand Up @@ -32,6 +32,7 @@
import java.util.function.Consumer;
import java.util.stream.Collectors;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.enterprise.context.RequestScoped;
import javax.enterprise.event.Event;
Expand Down Expand Up @@ -284,16 +285,12 @@ public Future<List<TranslationResult>> executeMergeAsync(TransMemoryMergeRequest

/**
* TM Merge for an entire project version
* @param targetVersionId
* @param mergeRequest
* @param handle
* @return
*/
@Async
@Override
public Future<Void> startMergeTranslations(Long targetVersionId,
VersionTMMerge mergeRequest,
MergeTranslationsTaskHandle handle) {
@Nonnull MergeTranslationsTaskHandle taskHandle) {
InternalTMSource internalTMSource = mergeRequest.getInternalTMSource();
List<Long> fromVersionIds = internalTMSource.getFilteredProjectVersionIds();
if (internalTMSource.getChoice() == SelectSome
Expand Down Expand Up @@ -335,22 +332,18 @@ public Future<Void> startMergeTranslations(Long targetVersionId,
return AsyncTaskResult.completed();
}

taskHandle.setTriggeredBy(authenticatedAccount.getUsername());
taskHandle.setMaxProgress((int) activeTextFlows);
taskHandle.setTotalTextFlows(activeTextFlows);

Optional<MergeTranslationsTaskHandle>
taskHandleOpt = Optional.ofNullable(handle);
if (taskHandleOpt.isPresent()) {
MergeTranslationsTaskHandle handle1 = taskHandleOpt.get();
handle1.setTriggeredBy(authenticatedAccount.getUsername());
handle1.setMaxProgress((int) activeTextFlows);
handle1.setTotalTextFlows(activeTextFlows);
}
Stopwatch overallStopwatch = Stopwatch.createStarted();
log.info("merge translations from TM start: from {} to {}",
mergeRequest.getInternalTMSource(),
targetVersionStr);
int textFlowIndex = 0;
boolean cancelled = false;

while (textFlowIndex < activeTextFlows) {
while (textFlowIndex < activeTextFlows && !(cancelled = taskHandle.isCancelled())) {
// Get the next batch of text flows. They may or may not need
// translation, but they won't change during the TM merge
// (unless someone uploads a source document). We will be
Expand Down Expand Up @@ -384,13 +377,12 @@ public Future<Void> startMergeTranslations(Long targetVersionId,
charCount, wordCount, 1);
}
}
taskHandleOpt.ifPresent(
mergeTranslationsTaskHandle -> mergeTranslationsTaskHandle
.increaseProgress(textFlowsIds.size()));
taskHandle.increaseProgress(textFlowsIds.size());
textFlowIndex += BATCH_SIZE;
}
versionStateCacheImpl.clearVersionStatsCache(targetVersionId);
log.info("merge translation from TM end: from {} to {}, {}",
log.info("{} merge translations from TM: from {} to {}, {}",
cancelled ? "CANCELLED" : "COMPLETED",
mergeRequest.getInternalTMSource(),
targetVersionStr, overallStopwatch);

Expand Down

0 comments on commit 1f7f9cd

Please sign in to comment.