Skip to content

Commit

Permalink
Merge pull request #398 from zanata/ZNTA-2057-async-refactor
Browse files Browse the repository at this point in the history
ZNTA-2057 async refactor
  • Loading branch information
Patrick Huang committed Jun 27, 2017
2 parents 5266b97 + 15c8694 commit ff15e34
Show file tree
Hide file tree
Showing 30 changed files with 1,097 additions and 527 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ public enum ProcessStatusCode {
/** The process has finshed with a failure */
@XmlEnumValue("Failed")
Failed,

/** The process has been cancelled */
@XmlEnumValue("Cancelled")
Cancelled
}

private String url;
Expand Down Expand Up @@ -111,6 +115,11 @@ public void setMessages(List<String> messages) {
this.messages = messages;
}

public ProcessStatus addMessage(String message) {
getMessages().add(message);
return this;
}

@XmlElement
public ProcessStatusCode getStatusCode() {
return statusCode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,10 @@ private void pushSrcDocToServer(final String docUri, final Resource srcDoc,
ConsoleUtils
.setProgressFeedbackMessage("Waiting for other clients ...");
break;
case Cancelled:
waitForCompletion = false;
ConsoleUtils.setProgressFeedbackMessage("Process is cancelled");
break;
}

// Wait before retrying
Expand Down Expand Up @@ -725,6 +729,10 @@ extensions, getOpts().getMergeType(),
ConsoleUtils
.setProgressFeedbackMessage("Waiting for other clients ...");
break;
case Cancelled:
waitForCompletion = false;
ConsoleUtils.setProgressFeedbackMessage("Process is cancelled");
break;
}

// Wait before retrying
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ interface Listener {

void hideConfirmation();

void updateFileDownloadProgress(int currentProgress, int maxProgress);
void updateFileDownloadProgress(long currentProgress, long maxProgress);

void setDownloadInProgress(boolean inProgress);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ public void hideConfirmation() {

@Override
public void
updateFileDownloadProgress(int currentProgress, int maxProgress) {
updateFileDownloadProgress(long currentProgress, long maxProgress) {
confirmationBox.setProgressMessage(currentProgress + " of "
+ maxProgress);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,26 @@ public class GetDownloadAllFilesProgressResult implements DispatchResult {

private static final long serialVersionUID = 1L;

private int currentProgress;
private int maxProgress;
private long currentProgress;
private long maxProgress;
private String downloadId;

@SuppressWarnings("unused")
private GetDownloadAllFilesProgressResult() {
}

public GetDownloadAllFilesProgressResult(int currentProgress,
int maxProgress, String downloadId) {
public GetDownloadAllFilesProgressResult(long currentProgress,
long maxProgress, String downloadId) {
this.maxProgress = maxProgress;
this.currentProgress = currentProgress;
this.downloadId = downloadId;
}

public int getCurrentProgress() {
public long getCurrentProgress() {
return currentProgress;
}

public int getMaxProgress() {
public long getMaxProgress() {
return maxProgress;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,26 @@
*/
package org.zanata.action;

import static org.zanata.async.AsyncTaskKey.joinFields;

import java.io.Serializable;

import javax.annotation.Nonnull;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import javax.enterprise.context.Dependent;
import javax.inject.Inject;
import javax.inject.Named;

import org.zanata.async.AsyncTaskHandle;
import org.zanata.async.AsyncTaskHandleManager;
import org.zanata.async.AsyncTaskKey;
import org.zanata.async.GenericAsyncTaskKey;
import org.zanata.async.handle.CopyTransTaskHandle;
import org.zanata.model.HCopyTransOptions;
import org.zanata.model.HDocument;
import org.zanata.model.HProjectIteration;
import org.zanata.security.ZanataIdentity;
import org.zanata.service.CopyTransService;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
// TODO This class should be merged with the copy trans service (?)

/**
Expand All @@ -59,7 +65,7 @@ public class CopyTransManager implements Serializable {
private ZanataIdentity identity;

public boolean isCopyTransRunning(@Nonnull Object target) {
CopyTransProcessKey key;
AsyncTaskKey key;
if (target instanceof HProjectIteration) {
key = CopyTransProcessKey.getKey((HProjectIteration) target);
} else if (target instanceof HDocument) {
Expand Down Expand Up @@ -93,8 +99,9 @@ public void startCopyTrans(HDocument document, HCopyTransOptions options) {
"Copy Trans is already running for document \'"
+ document.getDocId() + "\'");
}
CopyTransProcessKey key = CopyTransProcessKey.getKey(document);
AsyncTaskKey key = CopyTransProcessKey.getKey(document);
CopyTransTaskHandle handle = new CopyTransTaskHandle();
handle.setTriggeredBy(identity.getAccountUsername());
asyncTaskHandleManager.registerTaskHandle(handle, key);
copyTransServiceImpl.startCopyTransForDocument(document, options,
handle);
Expand All @@ -111,16 +118,17 @@ public void startCopyTrans(HProjectIteration iteration,
"Copy Trans is already running for version \'"
+ iteration.getSlug() + "\'");
}
CopyTransProcessKey key = CopyTransProcessKey.getKey(iteration);
AsyncTaskKey key = CopyTransProcessKey.getKey(iteration);
CopyTransTaskHandle handle = new CopyTransTaskHandle();
handle.setTriggeredBy(identity.getAccountUsername());
asyncTaskHandleManager.registerTaskHandle(handle, key);
copyTransServiceImpl.startCopyTransForIteration(iteration, options,
handle);
}

public CopyTransTaskHandle
getCopyTransProcessHandle(@Nonnull Object target) {
CopyTransProcessKey key;
AsyncTaskKey key;
if (target instanceof HProjectIteration) {
key = CopyTransProcessKey.getKey((HProjectIteration) target);
} else if (target instanceof HDocument) {
Expand All @@ -145,93 +153,21 @@ public void cancelCopyTrans(@Nonnull HProjectIteration iteration) {
/**
* Internal class to index Copy Trans processes.
*/
private static final class CopyTransProcessKey implements Serializable {
private static final long serialVersionUID = -2054359069473618887L;
private String projectSlug;
private String iterationSlug;
private String docId;

public static CopyTransProcessKey getKey(HProjectIteration iteration) {
CopyTransProcessKey newKey = new CopyTransProcessKey();
newKey.setProjectSlug(iteration.getProject().getSlug());
newKey.setIterationSlug(iteration.getSlug());
return newKey;
}
private static final class CopyTransProcessKey {
private static final String KEY_NAME = "copyTransKey";

public static CopyTransProcessKey getKey(HDocument document) {
CopyTransProcessKey newKey = new CopyTransProcessKey();
newKey.setDocId(document.getDocId());
newKey.setProjectSlug(
document.getProjectIteration().getProject().getSlug());
newKey.setIterationSlug(document.getProjectIteration().getSlug());
return newKey;
}

@Override
public boolean equals(final Object o) {
if (o == this)
return true;
if (!(o instanceof CopyTransManager.CopyTransProcessKey))
return false;
final CopyTransProcessKey other = (CopyTransProcessKey) o;
final Object this$projectSlug = this.getProjectSlug();
final Object other$projectSlug = other.getProjectSlug();
if (this$projectSlug == null ? other$projectSlug != null
: !this$projectSlug.equals(other$projectSlug))
return false;
final Object this$iterationSlug = this.getIterationSlug();
final Object other$iterationSlug = other.getIterationSlug();
if (this$iterationSlug == null ? other$iterationSlug != null
: !this$iterationSlug.equals(other$iterationSlug))
return false;
final Object this$docId = this.getDocId();
final Object other$docId = other.getDocId();
if (this$docId == null ? other$docId != null
: !this$docId.equals(other$docId))
return false;
return true;
public static AsyncTaskKey getKey(HProjectIteration iteration) {
return new GenericAsyncTaskKey(joinFields(iteration.getProject().getSlug(),
iteration.getSlug(), null));
}

@Override
public int hashCode() {
final int PRIME = 59;
int result = 1;
final Object $projectSlug = this.getProjectSlug();
result = result * PRIME
+ ($projectSlug == null ? 43 : $projectSlug.hashCode());
final Object $iterationSlug = this.getIterationSlug();
result = result * PRIME
+ ($iterationSlug == null ? 43 : $iterationSlug.hashCode());
final Object $docId = this.getDocId();
result = result * PRIME + ($docId == null ? 43 : $docId.hashCode());
return result;
public static AsyncTaskKey getKey(HDocument document) {
String projectSlug = document.getProjectIteration().getProject().getSlug();
String versionSlug = document.getProjectIteration().getSlug();
String docId = document.getDocId();
return new GenericAsyncTaskKey(joinFields(KEY_NAME, projectSlug, versionSlug, docId));
}

public String getProjectSlug() {
return this.projectSlug;
}

public String getIterationSlug() {
return this.iterationSlug;
}

public String getDocId() {
return this.docId;
}

public void setProjectSlug(final String projectSlug) {
this.projectSlug = projectSlug;
}

public void setIterationSlug(final String iterationSlug) {
this.iterationSlug = iterationSlug;
}

public void setDocId(final String docId) {
this.docId = docId;
}

private CopyTransProcessKey() {
}
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
package org.zanata.action;

import static org.zanata.async.AsyncTaskKey.joinFields;

import java.io.Serializable;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

import javax.enterprise.context.Dependent;
import javax.inject.Inject;

import org.zanata.async.AsyncTaskHandleManager;
import org.zanata.async.AsyncTaskKey;
import org.zanata.async.GenericAsyncTaskKey;
import org.zanata.async.handle.CopyVersionTaskHandle;
import org.zanata.security.ZanataIdentity;
import org.zanata.service.CopyVersionService;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

/**
* Manages copy version tasks.
*
Expand Down Expand Up @@ -41,7 +48,7 @@ public class CopyVersionManager implements Serializable {
*/
public void startCopyVersion(String projectSlug, String versionSlug,
String newVersionSlug) {
CopyVersionKey key = CopyVersionKey.getKey(projectSlug, newVersionSlug);
AsyncTaskKey key = CopyVersionKey.getKey(projectSlug, newVersionSlug);
CopyVersionTaskHandle handle = new CopyVersionTaskHandle();
asyncTaskHandleManager.registerTaskHandle(handle, key);
copyVersionServiceImpl.startCopyVersion(projectSlug, versionSlug,
Expand Down Expand Up @@ -83,68 +90,22 @@ public boolean isCopyVersionRunning(String projectSlug,
/**
* Key used for copy version task
*
* @param projectSlug
* - target project identifier
* @param versionSlug
* - target version identifier
*/
public static final class CopyVersionKey implements Serializable {
// target project identifier
private final String projectSlug;
// target version identifier
private final String versionSlug;
public static final class CopyVersionKey {
private static final String KEY_NAME = "copyVersion";

public static CopyVersionKey getKey(String projectSlug,
String versionSlug) {
return new CopyVersionKey(projectSlug, versionSlug);
/**
*
* @param projectSlug
* - target project identifier
* @param versionSlug
* - target version identifier
*/
public static AsyncTaskKey
getKey(String projectSlug, String versionSlug) {
return new GenericAsyncTaskKey(
joinFields(KEY_NAME, projectSlug, versionSlug));
}

@Override
public boolean equals(final Object o) {
if (o == this)
return true;
if (!(o instanceof CopyVersionManager.CopyVersionKey))
return false;
final CopyVersionKey other = (CopyVersionKey) o;
final Object this$projectSlug = this.getProjectSlug();
final Object other$projectSlug = other.getProjectSlug();
if (this$projectSlug == null ? other$projectSlug != null
: !this$projectSlug.equals(other$projectSlug))
return false;
final Object this$versionSlug = this.getVersionSlug();
final Object other$versionSlug = other.getVersionSlug();
if (this$versionSlug == null ? other$versionSlug != null
: !this$versionSlug.equals(other$versionSlug))
return false;
return true;
}

@Override
public int hashCode() {
final int PRIME = 59;
int result = 1;
final Object $projectSlug = this.getProjectSlug();
result = result * PRIME
+ ($projectSlug == null ? 43 : $projectSlug.hashCode());
final Object $versionSlug = this.getVersionSlug();
result = result * PRIME
+ ($versionSlug == null ? 43 : $versionSlug.hashCode());
return result;
}

public String getProjectSlug() {
return this.projectSlug;
}

public String getVersionSlug() {
return this.versionSlug;
}

@java.beans.ConstructorProperties({ "projectSlug", "versionSlug" })
public CopyVersionKey(final String projectSlug,
final String versionSlug) {
this.projectSlug = projectSlug;
this.versionSlug = versionSlug;
}
}
}
Loading

0 comments on commit ff15e34

Please sign in to comment.