Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor (DownloadCall): Simplify the method DownloadCall::execute #170

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -125,16 +125,31 @@ public boolean cancel() {
public void execute() throws InterruptedException {
currentThread = Thread.currentThread();

boolean retry;
int retryCount = 0;

// ready param
final OkDownload okDownload = OkDownload.with();
final ProcessFileStrategy fileStrategy = okDownload.processFileStrategy();

// inspect task start
inspectTaskStart();
do {

tryDownloadInLoop(okDownload, fileStrategy);

// finish
finishing = true;
blockChainList.clear();

final DownloadCache cache = this.cache;
if (canceled || cache == null) return;

final EndCause cause = getEndCause(cache);
Exception realCause = realCauseOrNull(cache, cause);
inspectTaskEnd(cache, cause, realCause);
}

private void tryDownloadInLoop(OkDownload okDownload, ProcessFileStrategy fileStrategy)
throws InterruptedException {
int retryCount = 0;
while (true) {
// 0. check basic param before start
if (task.getUrl().length() <= 0) {
this.cache = new DownloadCache.PreError(
Expand Down Expand Up @@ -219,35 +234,31 @@ public void execute() throws InterruptedException {
if (cache.isPreconditionFailed()
&& retryCount++ < MAX_COUNT_RETRY_FOR_PRECONDITION_FAILED) {
store.remove(task.getId());
retry = true;
} else {
retry = false;
break;
}
} while (retry);

// finish
finishing = true;
blockChainList.clear();

final DownloadCache cache = this.cache;
if (canceled || cache == null) return;
}
}

final EndCause cause;
Exception realCause = null;
private EndCause getEndCause(DownloadCache cache) {
if (cache.isServerCanceled() || cache.isUnknownError()
|| cache.isPreconditionFailed()) {
// error
cause = EndCause.ERROR;
realCause = cache.getRealCause();
return EndCause.ERROR;
} else if (cache.isFileBusyAfterRun()) {
cause = EndCause.FILE_BUSY;
return EndCause.FILE_BUSY;
} else if (cache.isPreAllocateFailed()) {
cause = EndCause.PRE_ALLOCATE_FAILED;
realCause = cache.getRealCause();
return EndCause.PRE_ALLOCATE_FAILED;
} else {
cause = EndCause.COMPLETED;
return EndCause.COMPLETED;
}
inspectTaskEnd(cache, cause, realCause);
}

private Exception realCauseOrNull(DownloadCache cache, EndCause cause) {
if (cause == EndCause.ERROR || cause == EndCause.PRE_ALLOCATE_FAILED) {
return cache.getRealCause();
}
return null;
}

private void inspectTaskStart() {
Expand Down