修复普通下载任务、组合任务共享执行队列、缓存池的问题

修复组合任务启动失败时,`DownloadGroupEntity`的状态变为执行中的问题
fix bug https://github.com/AriaLyy/Aria/issues/441
This commit is contained in:
laoyuyu
2019-08-05 19:29:32 +08:00
parent 1fb9f13dae
commit 558c9e1223
18 changed files with 164 additions and 60 deletions

View File

@@ -201,10 +201,12 @@ public abstract class AbsThreadTask<ENTITY extends AbsNormalEntity, TASK_WRAPPER
* @param state {@link IThreadState#STATE_STOP}..
* @param bundle 而外数据
*/
void sendState(int state, @Nullable Bundle bundle) {
synchronized void sendState(int state, @Nullable Bundle bundle) {
Message msg = mStateHandler.obtainMessage();
msg.what = state;
msg.obj = this;
if (state != IThreadState.STATE_UPDATE_PROGRESS) {
msg.obj = this;
}
if ((state == IThreadState.STATE_COMPLETE || state == IThreadState.STATE_FAIL)
&& (mTaskWrapper.getRequestType() == AbsTaskWrapper.M3U8_VOD

View File

@@ -96,12 +96,14 @@ public class ThreadStateManager implements IThreadState {
}
break;
case STATE_RUNNING:
mProgress += (long) msg.obj;
if (msg.obj instanceof Long) {
mProgress += (long) msg.obj;
}
break;
case STATE_UPDATE_PROGRESS:
if (msg.obj == null) {
mProgress = updateBlockProgress();
} else {
} else if (msg.obj instanceof Long) {
mProgress = (long) msg.obj;
}
break;

View File

@@ -211,12 +211,19 @@ public abstract class AbsGroupUtil implements IUtil, Runnable {
closeTimer();
return;
}
onStart();
startRunningFlow();
if (onStart()) {
startRunningFlow();
}
}
protected void onStart() {
/**
* 处理启动前的检查:获取组合任务大小
*
* @return {@code false} 将不再走后续流程,任务介绍
*/
protected boolean onStart() {
return false;
}
synchronized void closeTimer() {

View File

@@ -68,8 +68,7 @@ public class DGroupUtil extends AbsGroupUtil implements IUtil {
return false;
}
@Override protected void onStart() {
super.onStart();
@Override protected boolean onStart() {
if (mState.getCompleteNum() == mState.getSubSize()) {
mListener.onComplete();
} else {
@@ -84,6 +83,7 @@ public class DGroupUtil extends AbsGroupUtil implements IUtil {
} catch (InterruptedException e) {
e.printStackTrace();
}
return getLenComplete;
} else {
for (DTaskWrapper wrapper : mGTWrapper.getSubTaskWrapper()) {
if (wrapper.getState() != IEntity.STATE_COMPLETE) {
@@ -92,6 +92,7 @@ public class DGroupUtil extends AbsGroupUtil implements IUtil {
}
}
}
return true;
}
/**
@@ -137,6 +138,7 @@ public class DGroupUtil extends AbsGroupUtil implements IUtil {
*/
private void checkGetSizeComplete(int count, int failCount) {
if (failCount == mGTWrapper.getSubTaskWrapper().size()) {
mState.isRunning = false;
mListener.onFail(false, new AriaIOException(TAG, "获取子任务长度失败"));
notifyLock();
return;

View File

@@ -38,8 +38,7 @@ public class FtpDirDownloadUtil extends AbsGroupUtil {
return FTP_DIR;
}
@Override protected void onStart() {
super.onStart();
@Override protected boolean onStart() {
if (mGTWrapper.getEntity().getFileSize() > 1) {
startDownload();
} else {
@@ -57,6 +56,7 @@ public class FtpDirDownloadUtil extends AbsGroupUtil {
});
new Thread(infoThread).start();
}
return true;
}
private void startDownload() {

View File

@@ -26,7 +26,8 @@ import com.arialyy.aria.core.manager.TaskWrapperManager;
import com.arialyy.aria.core.manager.ThreadTaskManager;
import com.arialyy.aria.core.queue.pool.BaseCachePool;
import com.arialyy.aria.core.queue.pool.BaseExecutePool;
import com.arialyy.aria.core.queue.pool.DownloadSharePool;
import com.arialyy.aria.core.queue.pool.DGLoadSharePool;
import com.arialyy.aria.core.queue.pool.DLoadSharePool;
import com.arialyy.aria.core.queue.pool.UploadSharePool;
import com.arialyy.aria.core.upload.UploadTask;
import com.arialyy.aria.util.ALog;
@@ -47,9 +48,12 @@ public abstract class AbsTaskQueue<TASK extends AbsTask, TASK_WRAPPER extends Ab
AbsTaskQueue() {
switch (getQueueType()) {
case TYPE_D_QUEUE:
mCachePool = DLoadSharePool.getInstance().cachePool;
mExecutePool = DLoadSharePool.getInstance().executePool;
break;
case TYPE_DG_QUEUE:
mCachePool = DownloadSharePool.getInstance().cachePool;
mExecutePool = DownloadSharePool.getInstance().executePool;
mCachePool = DGLoadSharePool.getInstance().cachePool;
mExecutePool = DGLoadSharePool.getInstance().executePool;
break;
case TYPE_U_QUEUE:
mCachePool = UploadSharePool.getInstance().cachePool;

View File

@@ -0,0 +1,31 @@
/*
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.arialyy.aria.core.queue.pool;
import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.core.inf.AbsTask;
/**
* Created by AriaL on 2017/6/29.
* 单个下载任务的执行池
*/
class DGLoadExecutePool<TASK extends AbsTask> extends DLoadExecutePool<TASK> {
private final String TAG = "DGLoadExecutePool";
@Override protected int getMaxSize() {
return AriaManager.getInstance(AriaManager.APP).getDGroupConfig().getMaxTaskNum();
}
}

View File

@@ -19,21 +19,21 @@ package com.arialyy.aria.core.queue.pool;
* Created by Aria.Lao on 2017/7/17.
* 下载任务池该池子为简单任务和任务组共用
*/
public class DownloadSharePool {
private static volatile DownloadSharePool INSTANCE;
public class DGLoadSharePool {
private static volatile DGLoadSharePool INSTANCE;
public DownloadExecutePool executePool;
public DGLoadExecutePool executePool;
public BaseCachePool cachePool;
private DownloadSharePool() {
executePool = new DownloadExecutePool<>();
private DGLoadSharePool() {
executePool = new DGLoadExecutePool<>();
cachePool = new BaseCachePool<>();
}
public static DownloadSharePool getInstance() {
public static DGLoadSharePool getInstance() {
if (INSTANCE == null) {
synchronized (DownloadSharePool.class) {
INSTANCE = new DownloadSharePool();
synchronized (DGLoadSharePool.class) {
INSTANCE = new DGLoadSharePool();
}
}
return INSTANCE;

View File

@@ -26,7 +26,7 @@ import java.util.concurrent.TimeUnit;
* Created by AriaL on 2017/6/29.
* 单个下载任务的执行池
*/
class DownloadExecutePool<TASK extends AbsTask> extends BaseExecutePool<TASK> {
class DLoadExecutePool<TASK extends AbsTask> extends BaseExecutePool<TASK> {
private final String TAG = "DownloadExecutePool";
@Override protected int getMaxSize() {
@@ -34,7 +34,7 @@ class DownloadExecutePool<TASK extends AbsTask> extends BaseExecutePool<TASK> {
}
@Override public boolean putTask(TASK task) {
synchronized (DownloadExecutePool.class) {
synchronized (DLoadExecutePool.class) {
if (task == null) {
ALog.e(TAG, "任务不能为空!!");
return false;

View File

@@ -0,0 +1,41 @@
/*
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.arialyy.aria.core.queue.pool;
/**
* Created by Aria.Lao on 2017/7/17.
* 下载任务池,该池子为简单任务和任务组共用
*/
public class DLoadSharePool {
private static volatile DLoadSharePool INSTANCE;
public DLoadExecutePool executePool;
public BaseCachePool cachePool;
private DLoadSharePool() {
executePool = new DLoadExecutePool<>();
cachePool = new BaseCachePool<>();
}
public static DLoadSharePool getInstance() {
if (INSTANCE == null) {
synchronized (DLoadSharePool.class) {
INSTANCE = new DLoadSharePool();
}
}
return INSTANCE;
}
}

View File

@@ -106,7 +106,9 @@ class DelegateCommon extends AbsDelegate {
*/
boolean checkDataExist(SQLiteDatabase db, Class clazz, String... expression) {
db = checkDb(db);
CheckUtil.checkSqlExpression(expression);
if (!CheckUtil.checkSqlExpression(expression)) {
return false;
}
String sql = String.format("SELECT rowid, * FROM %s WHERE %s ", CommonUtil.getClassName(clazz),
expression[0]);
sql = sql.replace("?", "%s");

View File

@@ -15,7 +15,6 @@
*/
package com.arialyy.aria.orm;
import android.annotation.SuppressLint;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.text.TextUtils;
@@ -30,10 +29,8 @@ import java.lang.reflect.Field;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* Created by laoyuyu on 2018/3/22.
@@ -201,7 +198,9 @@ class DelegateFind extends AbsDelegate {
.append(cTableName.concat(".").concat(m.entityColumn()));
String sql;
if (expression != null && expression.length > 0) {
CheckUtil.checkSqlExpression(expression);
if (!CheckUtil.checkSqlExpression(expression)) {
return null;
}
sb.append(" WHERE ").append(expression[0]).append(" ");
sql = sb.toString();
sql = sql.replace("?", "%s");
@@ -321,7 +320,9 @@ class DelegateFind extends AbsDelegate {
*/
<T extends DbEntity> List<T> findData(SQLiteDatabase db, Class<T> clazz, String... expression) {
db = checkDb(db);
CheckUtil.checkSqlExpression(expression);
if (!CheckUtil.checkSqlExpression(expression)) {
return null;
}
String sql = String.format("SELECT rowid, * FROM %s WHERE %s", CommonUtil.getClassName(clazz),
expression[0]);
String[] params = new String[expression.length - 1];
@@ -339,7 +340,9 @@ class DelegateFind extends AbsDelegate {
return null;
}
db = checkDb(db);
CheckUtil.checkSqlExpression(expression);
if (!CheckUtil.checkSqlExpression(expression)) {
return null;
}
String sql = String.format("SELECT rowid, * FROM %s WHERE %s LIMIT %s,%s",
CommonUtil.getClassName(clazz),
expression[0], (page - 1) * num, num);

View File

@@ -34,13 +34,15 @@ class DelegateUpdate extends AbsDelegate {
private DelegateUpdate() {
}
/**
/**
* 删除某条数据
*/
synchronized <T extends DbEntity> void delData(SQLiteDatabase db, Class<T> clazz,
String... expression) {
db = checkDb(db);
CheckUtil.checkSqlExpression(expression);
if (!CheckUtil.checkSqlExpression(expression)) {
return;
}
String sql = "DELETE FROM " + CommonUtil.getClassName(clazz) + " WHERE " + expression[0] + " ";
sql = sql.replace("?", "%s");
@@ -60,7 +62,7 @@ class DelegateUpdate extends AbsDelegate {
ContentValues values = createValues(dbEntity);
if (values != null) {
db.update(CommonUtil.getClassName(dbEntity), values, "rowid=?",
new String[] {String.valueOf(dbEntity.rowID)});
new String[] { String.valueOf(dbEntity.rowID) });
} else {
ALog.e(TAG, "更新记录失败,记录没有属性字段");
}
@@ -69,7 +71,7 @@ class DelegateUpdate extends AbsDelegate {
/**
* 更新多条记录
*/
synchronized <T extends DbEntity> void updateManyData(SQLiteDatabase db, List<T> dbEntities) {
synchronized <T extends DbEntity> void updateManyData(SQLiteDatabase db, List<T> dbEntities) {
db = checkDb(db);
db.beginTransaction();
try {
@@ -84,7 +86,7 @@ class DelegateUpdate extends AbsDelegate {
if (value == null) {
ALog.e(TAG, "更新记录失败,记录没有属性字段");
} else {
db.update(table, value, "rowid=?", new String[] {String.valueOf(entity.rowID)});
db.update(table, value, "rowid=?", new String[] { String.valueOf(entity.rowID) });
}
}
db.setTransactionSuccessful();

View File

@@ -24,6 +24,7 @@ import com.arialyy.aria.core.upload.UTaskWrapper;
import com.arialyy.aria.core.upload.UploadEntity;
import com.arialyy.aria.exception.ParamException;
import java.io.File;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -68,17 +69,22 @@ public class CheckUtil {
/**
* 检查sql的expression是否合法
*
* @return false 不合法
*/
public static void checkSqlExpression(String... expression) {
public static boolean checkSqlExpression(String... expression) {
if (expression.length == 0) {
throw new IllegalArgumentException("sql语句表达式不能为null");
ALog.e(TAG, "sql语句表达式不能为null");
return false;
}
if (expression.length == 1) {
throw new IllegalArgumentException("表达式需要写入参数");
ALog.e(TAG, String.format("表达式需要写入参数,参数信息:%s", Arrays.toString(expression)));
return false;
}
String where = expression[0];
if (!where.contains("?")) {
throw new IllegalArgumentException("请在where语句的'='后编写?");
ALog.e(TAG, String.format("请在where语句的'='后编写?,参数信息:%s", Arrays.toString(expression)));
return false;
}
Pattern pattern = Pattern.compile("\\?");
Matcher matcher = pattern.matcher(where);
@@ -87,11 +93,14 @@ public class CheckUtil {
count++;
}
if (count < expression.length - 1) {
throw new IllegalArgumentException("条件语句的?个数不能小于参数个数");
ALog.e(TAG, String.format("条件语句的?个数不能小于参数个数,参数信息:%s", Arrays.toString(expression)));
return false;
}
if (count > expression.length - 1) {
throw new IllegalArgumentException("条件语句的?个数不能大于参数个数");
ALog.e(TAG, String.format("条件语句的?个数不能大于参数个数, 参数信息:%s", Arrays.toString(expression)));
return false;
}
return true;
}
/**