- 修复组任务,其中一个子任务在获取文件长度失败后,重新恢复组合任务,组合任务状态变为完成的问题 https://github.com/AriaLyy/Aria/issues/628
This commit is contained in:
@@ -137,7 +137,7 @@ public class RecordHelper {
|
||||
} else if (tr.startLocation != realLocation) { // 处理记录小于分块文件长度的情况
|
||||
ALog.i(TAG, String.format("修正分块【%s】的进度记录为:%s", temp.getPath(), realLocation));
|
||||
tr.startLocation = realLocation;
|
||||
}else {
|
||||
} else {
|
||||
ALog.i(TAG, String.format("修正分块【%s】的进度记录为:%s", temp.getPath(), realLocation));
|
||||
tr.startLocation = realLocation;
|
||||
tr.isComplete = false;
|
||||
@@ -163,8 +163,10 @@ public class RecordHelper {
|
||||
// 目标文件
|
||||
File targetFile = new File(mTaskRecord.filePath);
|
||||
// 处理组合任务其中一个子任务完成的情况
|
||||
if (tr.isComplete && targetFile.exists() && targetFile.length() == mWrapper.getEntity()
|
||||
.getFileSize()) {
|
||||
if (tr.isComplete
|
||||
&& targetFile.exists()
|
||||
&& targetFile.length() != 0
|
||||
&& targetFile.length() == mWrapper.getEntity().getFileSize()) {
|
||||
tr.isComplete = true;
|
||||
} else {
|
||||
ALog.w(TAG, String.format("文件【%s】不存在,任务将重新开始", file.getPath()));
|
||||
|
||||
@@ -153,7 +153,7 @@ public abstract class AbsGroupLoader implements ILoaderVisitor, ILoader {
|
||||
if (!checkSubTask(url, "开始")) {
|
||||
return;
|
||||
}
|
||||
if (!mState.isRunning) {
|
||||
if (!mState.isRunning.get()) {
|
||||
startTimer();
|
||||
}
|
||||
AbsSubDLoadUtil d = getDownloader(url, false);
|
||||
@@ -212,7 +212,7 @@ public abstract class AbsGroupLoader implements ILoaderVisitor, ILoader {
|
||||
}
|
||||
|
||||
@Override public boolean isRunning() {
|
||||
return mState != null && mState.isRunning;
|
||||
return mState != null && mState.isRunning.get();
|
||||
}
|
||||
|
||||
@Override public void cancel() {
|
||||
@@ -275,11 +275,11 @@ public abstract class AbsGroupLoader implements ILoaderVisitor, ILoader {
|
||||
}
|
||||
|
||||
private synchronized void startTimer() {
|
||||
mState.isRunning = true;
|
||||
mState.isRunning.set(true);
|
||||
mTimer = new ScheduledThreadPoolExecutor(1);
|
||||
mTimer.scheduleWithFixedDelay(new Runnable() {
|
||||
@Override public void run() {
|
||||
if (!mState.isRunning) {
|
||||
if (!mState.isRunning.get()) {
|
||||
closeTimer();
|
||||
} else if (mCurrentLocation >= 0) {
|
||||
long t = 0;
|
||||
@@ -321,7 +321,7 @@ public abstract class AbsGroupLoader implements ILoaderVisitor, ILoader {
|
||||
}
|
||||
}
|
||||
|
||||
protected void fail(AriaException e, boolean needRetry){
|
||||
protected void fail(AriaException e, boolean needRetry) {
|
||||
closeTimer();
|
||||
getListener().onFail(needRetry, e);
|
||||
}
|
||||
|
||||
@@ -19,6 +19,8 @@ import com.arialyy.aria.core.listener.IDGroupListener;
|
||||
import com.arialyy.aria.core.wrapper.AbsTaskWrapper;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
/**
|
||||
* 组合任务执行中的状态信息
|
||||
@@ -32,17 +34,18 @@ public final class GroupRunState {
|
||||
/**
|
||||
* 已经完成的任务数
|
||||
*/
|
||||
private int mCompleteNum;
|
||||
private AtomicInteger mCompleteNum = new AtomicInteger();
|
||||
|
||||
/**
|
||||
* 失败的任务数
|
||||
*/
|
||||
private int mFailNum;
|
||||
private AtomicInteger mFailNum = new AtomicInteger();
|
||||
|
||||
/**
|
||||
* 停止的任务数
|
||||
*/
|
||||
private int mStopNum;
|
||||
private AtomicInteger mStopNum = new AtomicInteger();
|
||||
;
|
||||
|
||||
/**
|
||||
* 当前进度
|
||||
@@ -62,7 +65,7 @@ public final class GroupRunState {
|
||||
/**
|
||||
* 是否在执行
|
||||
*/
|
||||
boolean isRunning = false;
|
||||
AtomicBoolean isRunning = new AtomicBoolean(false);
|
||||
|
||||
/**
|
||||
* 子任务失败、停止记录,用于当子任务失败重新被用户点击开始时,更新{@link #mStopNum}或{@link #mFailNum}
|
||||
@@ -78,7 +81,7 @@ public final class GroupRunState {
|
||||
mGroupHash = groupHash;
|
||||
}
|
||||
|
||||
public void setSubSize(int subSize){
|
||||
public void setSubSize(int subSize) {
|
||||
mSubSize = subSize;
|
||||
}
|
||||
|
||||
@@ -88,11 +91,11 @@ public final class GroupRunState {
|
||||
* @return {@code true}组合任务正在执行
|
||||
*/
|
||||
public boolean isRunning() {
|
||||
return isRunning;
|
||||
return isRunning.get();
|
||||
}
|
||||
|
||||
public void setRunning(boolean running) {
|
||||
isRunning = running;
|
||||
isRunning.set(running);
|
||||
}
|
||||
|
||||
String getGroupHash() {
|
||||
@@ -110,21 +113,21 @@ public final class GroupRunState {
|
||||
* 获取失败的数量
|
||||
*/
|
||||
public int getFailNum() {
|
||||
return mFailNum;
|
||||
return mFailNum.get();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取停止的数量
|
||||
*/
|
||||
public int getStopNum() {
|
||||
return mStopNum;
|
||||
return mStopNum.get();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取完成的数量
|
||||
*/
|
||||
public int getCompleteNum() {
|
||||
return mCompleteNum;
|
||||
return mCompleteNum.get();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -138,7 +141,7 @@ public final class GroupRunState {
|
||||
* 更新完成的数量,mCompleteNum + 1
|
||||
*/
|
||||
public void updateCompleteNum() {
|
||||
mCompleteNum++;
|
||||
mCompleteNum.getAndIncrement();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -156,10 +159,10 @@ public final class GroupRunState {
|
||||
public void updateCount(String key) {
|
||||
if (mFailTemp.contains(key)) {
|
||||
mFailTemp.remove(key);
|
||||
mFailNum--;
|
||||
mFailNum.getAndDecrement();
|
||||
} else if (mStopTemp.contains(key)) {
|
||||
mStopTemp.remove(key);
|
||||
mStopNum--;
|
||||
mStopNum.getAndDecrement();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -170,7 +173,7 @@ public final class GroupRunState {
|
||||
*/
|
||||
public void countStopNum(String key) {
|
||||
mStopTemp.add(key);
|
||||
mStopNum++;
|
||||
mStopNum.getAndIncrement();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -180,6 +183,6 @@ public final class GroupRunState {
|
||||
*/
|
||||
public void countFailNum(String key) {
|
||||
mFailTemp.add(key);
|
||||
mFailNum++;
|
||||
mFailNum.getAndIncrement();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -121,9 +121,14 @@ final class SimpleSchedulers implements Handler.Callback {
|
||||
|| mGState.getStopNum() + mGState.getFailNum() + mGState.getCompleteNum()
|
||||
== mGState.getSubSize()) {
|
||||
mQueue.clear();
|
||||
mGState.isRunning = false;
|
||||
mGState.listener.onFail(false, new AriaException(TAG,
|
||||
String.format("任务组【%s】下载失败", mGState.getGroupHash())));
|
||||
mGState.isRunning.set(false);
|
||||
if (mGState.getCompleteNum() > 0) {
|
||||
ALog.e(TAG, String.format("任务组【%s】停止", mGState.getGroupHash()));
|
||||
mGState.listener.onStop(mGState.getProgress());
|
||||
} else {
|
||||
mGState.listener.onFail(false, new AriaException(TAG,
|
||||
String.format("任务组【%s】下载失败", mGState.getGroupHash())));
|
||||
}
|
||||
} else {
|
||||
startNext();
|
||||
}
|
||||
@@ -147,7 +152,7 @@ final class SimpleSchedulers implements Handler.Callback {
|
||||
+ mQueue.getCacheSize()
|
||||
== mGState.getSubSize()) {
|
||||
mQueue.clear();
|
||||
mGState.isRunning = false;
|
||||
mGState.isRunning.set(false);
|
||||
mGState.listener.onStop(mGState.getProgress());
|
||||
} else {
|
||||
startNext();
|
||||
@@ -183,7 +188,7 @@ final class SimpleSchedulers implements Handler.Callback {
|
||||
mGState.listener.onStop(mGState.getProgress());
|
||||
}
|
||||
mQueue.clear();
|
||||
mGState.isRunning = false;
|
||||
mGState.isRunning.set(false);
|
||||
} else {
|
||||
startNext();
|
||||
}
|
||||
|
||||
@@ -24,26 +24,28 @@ import com.arialyy.aria.core.inf.IThreadStateManager;
|
||||
import com.arialyy.aria.core.listener.IEventListener;
|
||||
import com.arialyy.aria.exception.AriaException;
|
||||
import com.arialyy.aria.util.ALog;
|
||||
import com.arialyy.aria.util.CommonUtil;
|
||||
import com.arialyy.aria.util.FileUtil;
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
/**
|
||||
* 线程任务管理器,用于处理多线程下载时任务的状态回调
|
||||
*/
|
||||
public class NormalThreadStateManager implements IThreadStateManager {
|
||||
private final String TAG = "ThreadTaskStateManager";
|
||||
private final String TAG = CommonUtil.getClassName(this);
|
||||
|
||||
/**
|
||||
* 任务状态回调
|
||||
*/
|
||||
private IEventListener mListener;
|
||||
private int mThreadNum; // 启动的线程总数
|
||||
private int mCancelNum = 0; // 已经取消的线程的数
|
||||
private int mStopNum = 0; // 已经停止的线程数
|
||||
private int mFailNum = 0; // 失败的线程数
|
||||
private int mCompleteNum = 0; // 完成的线程数
|
||||
private AtomicInteger mCancelNum = new AtomicInteger(0); // 已经取消的线程的数
|
||||
private AtomicInteger mStopNum = new AtomicInteger(0); // 已经停止的线程数
|
||||
private AtomicInteger mFailNum = new AtomicInteger(0); // 失败的线程数
|
||||
private AtomicInteger mCompleteNum = new AtomicInteger(0); // 完成的线程数
|
||||
private long mProgress; //当前总进度
|
||||
private TaskRecord mTaskRecord; // 任务记录
|
||||
private Looper mLooper;
|
||||
@@ -75,19 +77,19 @@ public class NormalThreadStateManager implements IThreadStateManager {
|
||||
checkLooper();
|
||||
switch (msg.what) {
|
||||
case STATE_STOP:
|
||||
mStopNum++;
|
||||
mStopNum.getAndIncrement();
|
||||
if (isStop()) {
|
||||
quitLooper();
|
||||
}
|
||||
break;
|
||||
case STATE_CANCEL:
|
||||
mCancelNum++;
|
||||
mCancelNum.getAndIncrement();
|
||||
if (isCancel()) {
|
||||
quitLooper();
|
||||
}
|
||||
break;
|
||||
case STATE_FAIL:
|
||||
mFailNum++;
|
||||
mFailNum.getAndIncrement();
|
||||
if (isFail()) {
|
||||
Bundle b = msg.getData();
|
||||
mListener.onFail(b.getBoolean(DATA_RETRY, false),
|
||||
@@ -96,7 +98,7 @@ public class NormalThreadStateManager implements IThreadStateManager {
|
||||
}
|
||||
break;
|
||||
case STATE_COMPLETE:
|
||||
mCompleteNum++;
|
||||
mCompleteNum.getAndIncrement();
|
||||
if (isComplete()) {
|
||||
ALog.d(TAG, "isComplete, completeNum = " + mCompleteNum);
|
||||
//if (mTaskRecord.taskType == ITaskWrapper.D_SFTP) {
|
||||
@@ -168,7 +170,7 @@ public class NormalThreadStateManager implements IThreadStateManager {
|
||||
//ALog.d(TAG,
|
||||
// String.format("isStop; stopNum: %s, cancelNum: %s, failNum: %s, completeNum: %s", mStopNum,
|
||||
// mCancelNum, mFailNum, mCompleteNum));
|
||||
return mStopNum == mThreadNum || mStopNum + mCompleteNum == mThreadNum;
|
||||
return mStopNum.get() == mThreadNum || mStopNum.get() + mCompleteNum.get() == mThreadNum;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -179,8 +181,8 @@ public class NormalThreadStateManager implements IThreadStateManager {
|
||||
//ALog.d(TAG,
|
||||
// String.format("isFail; stopNum: %s, cancelNum: %s, failNum: %s, completeNum: %s", mStopNum,
|
||||
// mCancelNum, mFailNum, mCompleteNum));
|
||||
return mCompleteNum != mThreadNum
|
||||
&& (mFailNum == mThreadNum || mFailNum + mCompleteNum == mThreadNum);
|
||||
return mCompleteNum.get() != mThreadNum
|
||||
&& (mFailNum.get() == mThreadNum || mFailNum.get() + mCompleteNum.get() == mThreadNum);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -192,7 +194,7 @@ public class NormalThreadStateManager implements IThreadStateManager {
|
||||
// String.format("isComplete; stopNum: %s, cancelNum: %s, failNum: %s, completeNum: %s",
|
||||
// mStopNum,
|
||||
// mCancelNum, mFailNum, mCompleteNum));
|
||||
return mCompleteNum == mThreadNum;
|
||||
return mCompleteNum.get() == mThreadNum;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -202,7 +204,7 @@ public class NormalThreadStateManager implements IThreadStateManager {
|
||||
//ALog.d(TAG, String.format("isCancel; stopNum: %s, cancelNum: %s, failNum: %s, completeNum: %s",
|
||||
// mStopNum,
|
||||
// mCancelNum, mFailNum, mCompleteNum));
|
||||
return mCancelNum == mThreadNum;
|
||||
return mCancelNum.get() == mThreadNum;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -98,7 +98,7 @@ class DelegateFind extends AbsDelegate {
|
||||
*/
|
||||
<T extends AbsDbWrapper> List<T> findRelationData(SQLiteDatabase db, Class<T> clazz,
|
||||
String... expression) {
|
||||
return exeRelationSql(db, clazz, 1, 10, expression);
|
||||
return exeRelationSql(db, clazz, 1, Integer.MAX_VALUE, expression);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user