Commit fc38dd28 by pye52

调整日志上传策略,当没有日志时上传空压缩包,同时上传失败时会重试(最多重试3次)

parent e31ff44e
package com.bgycc.smartcanteen.command; package com.bgycc.smartcanteen.command;
import androidx.work.BackoffPolicy;
import androidx.work.Data; import androidx.work.Data;
import androidx.work.ListenableWorker; import androidx.work.ListenableWorker;
import androidx.work.OneTimeWorkRequest; import androidx.work.OneTimeWorkRequest;
...@@ -9,6 +10,7 @@ import com.google.gson.Gson; ...@@ -9,6 +10,7 @@ import com.google.gson.Gson;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
import java.util.concurrent.TimeUnit;
public class CommandHelper { public class CommandHelper {
private static final String LOG_UPLOAD = "LOG_PULL"; private static final String LOG_UPLOAD = "LOG_PULL";
...@@ -72,6 +74,11 @@ public class CommandHelper { ...@@ -72,6 +74,11 @@ public class CommandHelper {
worker = new OneTimeWorkRequest.Builder(workerClass) worker = new OneTimeWorkRequest.Builder(workerClass)
.setInputData(inputData) .setInputData(inputData)
.addTag(command.getAction()) .addTag(command.getAction())
.setBackoffCriteria(
BackoffPolicy.LINEAR,
OneTimeWorkRequest.MIN_BACKOFF_MILLIS,
TimeUnit.MILLISECONDS
)
.build(); .build();
} }
return worker; return worker;
......
...@@ -8,6 +8,7 @@ import androidx.work.WorkerParameters; ...@@ -8,6 +8,7 @@ import androidx.work.WorkerParameters;
import com.bgycc.smartcanteen.api.SCApi; import com.bgycc.smartcanteen.api.SCApi;
import com.bgycc.smartcanteen.api.SCRetrofit; import com.bgycc.smartcanteen.api.SCRetrofit;
import com.bgycc.smartcanteen.entity.CommandLog; import com.bgycc.smartcanteen.entity.CommandLog;
import com.bgycc.smartcanteen.utils.DeviceProxy;
import com.blankj.utilcode.util.FileUtils; import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.LogUtils; import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.PathUtils; import com.blankj.utilcode.util.PathUtils;
...@@ -40,6 +41,8 @@ public class LogCommandWorker extends CommandWorker { ...@@ -40,6 +41,8 @@ public class LogCommandWorker extends CommandWorker {
private static final String ZIP_FILE = "log.zip"; private static final String ZIP_FILE = "log.zip";
private static final String UPLOAD_DIR = "upload_log"; private static final String UPLOAD_DIR = "upload_log";
private static final long DEFAULT_DELAY = 1000; private static final long DEFAULT_DELAY = 1000;
// 上传失败时最多重试次数
private static final int MAX_RETRY_TIMES = 3;
private SimpleDateFormat parseFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()); private SimpleDateFormat parseFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
private SimpleDateFormat nameFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()); private SimpleDateFormat nameFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
...@@ -62,12 +65,18 @@ public class LogCommandWorker extends CommandWorker { ...@@ -62,12 +65,18 @@ public class LogCommandWorker extends CommandWorker {
return failed("日志上传指令不符合规范"); return failed("日志上传指令不符合规范");
} }
File logFile = getZipLogs(); File logFile = getZipLogs();
if (logFile != null) { if (logFile == null) {
inProgress("上传压缩文件至服务器"); return failed();
upload(logFile); }
inProgress("上传压缩文件至服务器");
if (upload(logFile)) {
return success(); return success();
} }
return failed(); // 检查该任务的重试次数,若未超过限制,则重试
if (getRunAttemptCount() >= MAX_RETRY_TIMES) {
return failed();
}
return Result.retry();
} }
private void parseDate() { private void parseDate() {
...@@ -106,8 +115,17 @@ public class LogCommandWorker extends CommandWorker { ...@@ -106,8 +115,17 @@ public class LogCommandWorker extends CommandWorker {
} }
if (uploadDir == null) return null; if (uploadDir == null) return null;
if (!copyTargetFiles(logDir, uploadDir)) { try {
LogUtils.d(TAG, "没有匹配的日志文件"); if (!copyTargetFiles(logDir, uploadDir)) {
LogUtils.d(TAG, "日志文件筛选出错");
FileUtils.delete(uploadDir);
uploadDir = null;
return null;
}
} catch (Exception e) {
LogUtils.e(TAG, "日志文件复制出错: " + e.getMessage(), e);
FileUtils.delete(uploadDir);
uploadDir = null;
return null; return null;
} }
...@@ -132,7 +150,7 @@ public class LogCommandWorker extends CommandWorker { ...@@ -132,7 +150,7 @@ public class LogCommandWorker extends CommandWorker {
private File getLogDirByType(String logType) { private File getLogDirByType(String logType) {
File logDir; File logDir;
if (logType.equals(BOOT_LOG)) { if (logType.equals(BOOT_LOG)) {
logDir = new File(PathUtils.getExternalStoragePath(), "boot_log"); logDir = new File(DeviceProxy.getSystemLogDir());
} else { } else {
logDir = new File(LogUtils.getConfig().getDir()); logDir = new File(LogUtils.getConfig().getDir());
} }
...@@ -161,6 +179,9 @@ public class LogCommandWorker extends CommandWorker { ...@@ -161,6 +179,9 @@ public class LogCommandWorker extends CommandWorker {
return date.after(startTime) && date.before(endTime); return date.after(startTime) && date.before(endTime);
}; };
List<File> logFiles = FileUtils.listFilesInDirWithFilter(src, filter, false, null); List<File> logFiles = FileUtils.listFilesInDirWithFilter(src, filter, false, null);
if (logFiles.isEmpty()) {
return true;
}
boolean copyResult = true; boolean copyResult = true;
inProgress("筛选目标日志文件"); inProgress("筛选目标日志文件");
int count = 0; int count = 0;
...@@ -173,10 +194,10 @@ public class LogCommandWorker extends CommandWorker { ...@@ -173,10 +194,10 @@ public class LogCommandWorker extends CommandWorker {
if (tempCopyResult) count++; if (tempCopyResult) count++;
} }
LogUtils.d(TAG, "待上传日志文件总数: " + logFiles.size() + ", 复制成功文件总数: " + count); LogUtils.d(TAG, "待上传日志文件总数: " + logFiles.size() + ", 复制成功文件总数: " + count);
return !logFiles.isEmpty() && copyResult; return copyResult;
} }
private void upload(File zip) { private boolean upload(File zip) {
OkHttpClient client = SCRetrofit.createOkHttpClientBuilder().build(); OkHttpClient client = SCRetrofit.createOkHttpClientBuilder().build();
SCApi api = SCRetrofit.createApi(client); SCApi api = SCRetrofit.createApi(client);
CommandLog.CommandLogData data = commandLog.getData(); CommandLog.CommandLogData data = commandLog.getData();
...@@ -190,9 +211,11 @@ public class LogCommandWorker extends CommandWorker { ...@@ -190,9 +211,11 @@ public class LogCommandWorker extends CommandWorker {
MultipartBody.Part body = MultipartBody.Part.createFormData("file", fileNameForServer, requestBody); MultipartBody.Part body = MultipartBody.Part.createFormData("file", fileNameForServer, requestBody);
try { try {
Response<String> response = api.upload(body).execute(); Response<String> response = api.upload(body).execute();
LogUtils.d(TAG, "日志文件上传成功: " + response); LogUtils.d(TAG, "日志文件上传完毕: " + response);
return response.isSuccessful();
} catch (IOException e) { } catch (IOException e) {
LogUtils.e(TAG, "日志文件上传失败: " + e.getMessage()); LogUtils.e(TAG, "日志文件上传失败: " + e.getMessage());
return false;
} finally { } finally {
FileUtils.delete(uploadDir); FileUtils.delete(uploadDir);
FileUtils.delete(zip); FileUtils.delete(zip);
......
package com.bgycc.smartcanteen.utils; package com.bgycc.smartcanteen.utils;
import android.os.Build;
import com.bgycc.smartcanteen.qrcode.IQRCodeScan; import com.bgycc.smartcanteen.qrcode.IQRCodeScan;
import com.bgycc.smartcanteen.qrcode.QRCodeScanFactory; import com.bgycc.smartcanteen.qrcode.QRCodeScanFactory;
import com.blankj.utilcode.util.PathUtils;
import java.io.File; import java.io.File;
...@@ -12,6 +15,7 @@ import java.io.File; ...@@ -12,6 +15,7 @@ import java.io.File;
public class DeviceProxy { public class DeviceProxy {
public static final String DEVICE_MODEL_TPS = "TPS"; public static final String DEVICE_MODEL_TPS = "TPS";
public static final String DEVICE_MODEL_QUAD = "QUAD"; public static final String DEVICE_MODEL_QUAD = "QUAD";
private static final String QUAD_SYSTEM_LOG = "boot_log";
public static IQRCodeScan createQRCodeScan() throws Exception { public static IQRCodeScan createQRCodeScan() throws Exception {
return QRCodeScanFactory.create(); return QRCodeScanFactory.create();
...@@ -20,4 +24,15 @@ public class DeviceProxy { ...@@ -20,4 +24,15 @@ public class DeviceProxy {
public static boolean updateApp(File updateApk) { public static boolean updateApp(File updateApk) {
return InstallManager.install(updateApk); return InstallManager.install(updateApk);
} }
public static String getSystemLogDir() {
String model = Build.MODEL;
if (model.contains(DEVICE_MODEL_TPS)) {
return PathUtils.getExternalStoragePath() + File.separator + QUAD_SYSTEM_LOG;
} else if (model.contains(DEVICE_MODEL_QUAD)) {
return PathUtils.getExternalStoragePath() + File.separator + QUAD_SYSTEM_LOG;
} else {
return PathUtils.getExternalStoragePath() + File.separator + QUAD_SYSTEM_LOG;
}
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment