Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
Give meaningful names to async tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
seanf committed Nov 14, 2013
1 parent dcab905 commit 515f8b1
Show file tree
Hide file tree
Showing 12 changed files with 50 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,8 @@ private class ReindexTask implements
@Override
public AsyncTaskHandle<Boolean> getHandle() {
if (handle == null) {
handle = new AsyncTaskHandle<Boolean>();
String name = getClass().getSimpleName(); //+":"+indexingOptions
handle = new AsyncTaskHandle<Boolean>(name);
handle.setMaxProgress(getTotalOperations());
}
return handle;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ public boolean validateSlug(String slug, String componentId) {
}

public void clearTransMemory(final String transMemorySlug) {
asyncTaskManagerServiceImpl.startTask(new SimpleAsyncTask<Void>() {
String name = "TranslationMemoryAction.clearTransMemory: "+transMemorySlug;
asyncTaskManagerServiceImpl.startTask(new SimpleAsyncTask<Void>(name) {
@Override
public Void call() throws Exception {
TranslationMemoryResourceService tmResource =
Expand Down
13 changes: 13 additions & 0 deletions zanata-war/src/main/java/org/zanata/async/AsyncTaskHandle.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
* href="mailto:camunoz@redhat.com">camunoz@redhat.com</a>
*/
public class AsyncTaskHandle<V> extends AbstractFuture<V> {

@Getter
private final String taskName;

@Getter
@Setter
public int maxProgress = 100;
Expand All @@ -47,6 +51,15 @@ public class AsyncTaskHandle<V> extends AbstractFuture<V> {
@Setter
public int currentProgress = 0;

public AsyncTaskHandle(String taskName) {
this.taskName = taskName;
}

@Override
public String toString() {
return getClass().getSimpleName()+"(taskName="+taskName+")";
}

@Override
protected boolean setException(Throwable throwable) {
return super.setException(throwable);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,10 @@
public abstract class SimpleAsyncTask<V> implements
AsyncTask<V, AsyncTaskHandle<V>> {
@Getter
private final AsyncTaskHandle<V> handle = new AsyncTaskHandle<V>();
private final AsyncTaskHandle<V> handle;

public SimpleAsyncTask(String taskName) {
handle = new AsyncTaskHandle<V>(taskName);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ public class TimedAsyncHandle<V> extends AsyncTaskHandle<V> {
@Getter
private long finishTime;

public TimedAsyncHandle(String taskName) {
super(taskName);
}

public void startTiming() {
startTime = System.currentTimeMillis();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,11 @@ public abstract class CopyTransTask implements

protected HCopyTransOptions copyTransOptions;

private final CopyTransTaskHandle handle = new CopyTransTaskHandle();
private final CopyTransTaskHandle handle;

public CopyTransTask(HCopyTransOptions copyTransOptions) {
public CopyTransTask(HCopyTransOptions copyTransOptions, String taskName) {
this.copyTransOptions = copyTransOptions;
this.handle = new CopyTransTaskHandle(taskName);
}

@Override
Expand Down Expand Up @@ -88,6 +89,10 @@ public static class CopyTransTaskHandle extends TimedAsyncHandle<Void> {
@Setter
private String triggeredBy;

public CopyTransTaskHandle(String taskName) {
super(taskName);
}

/**
* Increments the processed documents by 1
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class DocumentCopyTransTask extends CopyTransTask {

public DocumentCopyTransTask(HDocument document,
HCopyTransOptions copyTransOptions) {
super(copyTransOptions);
super(copyTransOptions, "DocumentCopyTransTask: "+document.getDocId());
this.document = document;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class IterationCopyTransTask extends CopyTransTask {

public IterationCopyTransTask(HProjectIteration projectIteration,
HCopyTransOptions options) {
super(options);
super(options, "IterationCopyTransTask: "+projectIteration.getSlug());
this.projectIteration = projectIteration;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ public AsyncTaskHandle<String> getHandle() {
}

private AsyncTaskHandle<String> buildHandle() {
AsyncTaskHandle<String> newHandle = new AsyncTaskHandle<String>();
String name = "ZipFileBuildTask: "+projectSlug+"-"+iterationSlug+"-"+localeId;
AsyncTaskHandle<String> newHandle = new AsyncTaskHandle<String>(name);

// Max documents to process
DocumentDAO documentDAO =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,21 +118,17 @@ public ProcessStatus startSourceDocCreation(final String idNoSlash,
}
}

SimpleAsyncTask<Void> task = new SimpleAsyncTask<Void>() {
String name = "SourceDocCreation: "+projectSlug+"-"+iterationSlug+"-"+idNoSlash;
SimpleAsyncTask<Void> task = new SimpleAsyncTask<Void>(name) {
@Override
public Void call() throws Exception {
DocumentService documentServiceImpl =
(DocumentService) Component
.getInstance(DocumentServiceImpl.class);
documentServiceImpl.saveDocument(projectSlug, iterationSlug,
resource, extensions, copytrans, true);
getHandle().setCurrentProgress(getHandle().getMaxProgress()); // TODO
// This
// should
// update
// with
// real
// progress
// TODO This should update with real progress
getHandle().setCurrentProgress(getHandle().getMaxProgress());
return null;
}
};
Expand All @@ -153,7 +149,8 @@ public ProcessStatus startSourceDocCreationOrUpdate(final String idNoSlash,

resourceUtils.validateExtensions(extensions); // gettext, comment

SimpleAsyncTask<Void> task = new SimpleAsyncTask<Void>() {
String name = "SourceDocCreationOrUpdate: "+projectSlug+"-"+iterationSlug+"-"+idNoSlash;
SimpleAsyncTask<Void> task = new SimpleAsyncTask<Void>(name) {
@Override
public Void call() throws Exception {
DocumentService documentServiceImpl =
Expand Down Expand Up @@ -198,8 +195,9 @@ public ProcessStatus startTranslatedDocCreationOrUpdate(
final String id = URIHelper.convertFromDocumentURIId(idNoSlash);
final MergeType finalMergeType = mergeType;

String name = "TranslatedDocCreationOrUpdate: "+projectSlug+"-"+iterationSlug+"-"+idNoSlash+"-"+locale;
SimpleAsyncTask<List<String>> task =
new SimpleAsyncTask<List<String>>() {
new SimpleAsyncTask<List<String>>(name) {
@Override
public List<String> call() throws Exception {
TranslationService translationServiceImpl =
Expand Down
2 changes: 1 addition & 1 deletion zanata-war/src/main/webapp/admin/processmanager.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
<rich:column>
<f:facet
name="header">#{messages['jsf.processmanager.Type']}</f:facet>
#{handle.getClass().getSimpleName()}
#{handle.getTaskName()}
</rich:column>
<rich:column>
<f:facet name="header">#{messages['jsf.Status']}</f:facet>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void taskReturnsValue() throws Exception {

// Start an asynchronous process
AsyncTaskHandle<String> handle =
taskExecutor.startTask(new SimpleAsyncTask<String>() {
taskExecutor.startTask(new SimpleAsyncTask<String>("taskReturnsValue") {
@Override
public String call() throws Exception {
return expectedRetVal;
Expand All @@ -73,7 +73,7 @@ public String call() throws Exception {
public void executionError() throws Exception {
// Start an asynchronous process that throws an exception
AsyncTaskHandle<String> handle =
taskExecutor.startTask(new SimpleAsyncTask<String>() {
taskExecutor.startTask(new SimpleAsyncTask<String>("executionError") {
@Override
public String call() throws Exception {
throw new RuntimeException("Expected Exception");
Expand All @@ -90,7 +90,7 @@ public String call() throws Exception {
public void progressUpdates() throws Exception {
final List<Integer> progressUpdates = Lists.newArrayList();
// "Mock" the task handle so that progress updates are recorded
final AsyncTaskHandle<Void> taskHandle = new AsyncTaskHandle<Void>() {
final AsyncTaskHandle<Void> taskHandle = new AsyncTaskHandle<Void>("progressUpdates") {
@Override
public void setCurrentProgress(int progress) {
super.setCurrentProgress(progress);
Expand Down

0 comments on commit 515f8b1

Please sign in to comment.