Skip to content

Commit 62301ce

Browse files
committed
优化自动重置任务逻辑
1 parent 6d05481 commit 62301ce

File tree

2 files changed

+45
-28
lines changed

2 files changed

+45
-28
lines changed

app/src/main/java/me/teble/xposed/autodaily/hook/SplashActivityHook.kt

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -54,31 +54,17 @@ class SplashActivityHook : BaseHook() {
5454
val daemonDir = context.getExternalFilesDir("xa_daemon")
5555
val file = File(daemonDir, ".init_service")
5656
if (file.exists()) {
57-
val accessFile = RandomAccessFile(file, "rw")
58-
val lock = accessFile.channel.tryLock()
59-
lock?.let {
60-
LogUtil.d("获取文件锁成功,守护进程未在运行?")
61-
context.openJumpModuleDialog(lock, file)
62-
}
63-
}
64-
ConfUnit.versionInfoCache ?: ConfigUtil.checkUpdate(false)
65-
val conf = loadSaveConf()
66-
val currDateStr = TimeUtil.getCNDate().formatDate()
67-
conf.taskGroups.forEach { group ->
68-
group.tasks.forEach { task ->
69-
val errInfo = task.errInfo
70-
if (errInfo.dateStr.isNotEmpty() && errInfo.dateStr != currDateStr) {
71-
if (task.retryCount != 0) {
72-
task.retryCount = 0
73-
task.errInfo = TaskErrorInfo()
74-
}
75-
} else if (errInfo.count >= 3 && task.retryCount < 3) {
76-
task.errInfo = TaskErrorInfo()
77-
task.retryCount++
78-
LogUtil.d("重置task: ${task.id} retryCount: ${task.retryCount}")
57+
runCatching {
58+
val accessFile = RandomAccessFile(file, "rw")
59+
val lock = accessFile.channel.tryLock()
60+
lock?.let {
61+
LogUtil.d("获取文件锁成功,守护进程未在运行?")
62+
context.openJumpModuleDialog(lock, file)
7963
}
80-
}
64+
}.onFailure { LogUtil.e(it, "try lock fail: ") }
8165
}
66+
ConfUnit.versionInfoCache ?: ConfigUtil.checkUpdate(false)
67+
autoResetTask(true)
8268
}
8369
handler.sendEmptyMessageDelayed(AUTO_EXEC, 10_000)
8470
}
@@ -88,11 +74,11 @@ class SplashActivityHook : BaseHook() {
8874
val context = it.thisObject as Activity
8975
scope.launch {
9076
withContext(Dispatchers.IO) {
91-
loadSaveConf()
9277
if (ConfUnit.versionInfoCache == null
9378
|| System.currentTimeMillis() - ConfUnit.lastFetchTime > 3 * 60 * 60_000L) {
9479
ConfigUtil.fetchUpdateInfo()
9580
}
81+
autoResetTask(false)
9682
}
9783
if (ConfUnit.needUpdate) {
9884
context.openAppUpdateDialog()
@@ -107,6 +93,35 @@ class SplashActivityHook : BaseHook() {
10793

10894
private lateinit var appUpdateDialog: AlertDialog
10995

96+
private var lastAutoResetTime = 0L
97+
98+
private fun autoResetTask(isOnCreate: Boolean) {
99+
if (System.currentTimeMillis() - lastAutoResetTime < 30 * 60_000L) {
100+
return
101+
}
102+
lastAutoResetTime = System.currentTimeMillis()
103+
val conf = loadSaveConf()
104+
val currDateStr = TimeUtil.getCNDate().formatDate()
105+
conf.taskGroups.forEach { group ->
106+
group.tasks.forEach { task ->
107+
val errInfo = task.errInfo
108+
if (errInfo.dateStr.isNotEmpty() && errInfo.dateStr != currDateStr) {
109+
if (task.retryCount != 0) {
110+
task.retryCount = 0
111+
task.errInfo = TaskErrorInfo()
112+
}
113+
} else if (errInfo.count >= 3) {
114+
if ((!isOnCreate && task.retryCount < 2) ||
115+
(isOnCreate && task.retryCount < 3)) {
116+
task.errInfo = TaskErrorInfo()
117+
task.retryCount++
118+
LogUtil.d("重置task: ${task.id} retryCount: ${task.retryCount}")
119+
}
120+
}
121+
}
122+
}
123+
}
124+
110125
suspend fun Activity.openAppUpdateDialog() {
111126
withContext(Dispatchers.IO) {
112127
var builder: AlertDialog.Builder? = null

app/src/main/java/me/teble/xposed/autodaily/ui/SignLayout.kt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,12 @@ fun SignLayout(navController: NavHostController) {
3030
conf = ConfigUtil.loadSaveConf()
3131
conf.taskGroups.forEach { taskGroup ->
3232
taskGroup.tasks.forEach {
33-
if (it.retryCount >= 3) {
34-
errorMap[it.id] = "任务执行失败次数过多,今日暂停执行,可能存在功能不兼容"
35-
} else if (it.errInfo.count >= 3) {
36-
errorMap[it.id] = "任务执行失败次数超过3次,本次启动暂停执行,可长按重置执行记录"
33+
if (it.errInfo.count >= 3) {
34+
if (it.retryCount >= 3) {
35+
errorMap[it.id] = "任务执行失败次数过多,今日暂停执行,可能存在功能不兼容"
36+
} else {
37+
errorMap[it.id] = "任务执行失败次数超过3次,本次启动暂停执行,可长按重置执行记录"
38+
}
3739
}
3840
}
3941
}

0 commit comments

Comments
 (0)