diff --git a/task_manager/README.md b/task_manager/README.md index dcb615e..c6eae35 100644 --- a/task_manager/README.md +++ b/task_manager/README.md @@ -24,7 +24,8 @@ 3. 用“添加脚本”选择其他 `.js`;用“上移/下移”排序,勾选启用任务,分别配置次数、超时、重试和失败策略。删除任务只移除列表,保留脚本文件。 4. 设置整组循环次数,`0` 表示无限循环;任务间隔也应用于重复执行和失败重试。 5. 点击“开始整组”。界面会退到后台,任务自行打开对应应用。开启悬浮状态时,需先允许 AutoJs6 的悬浮窗权限;也可关闭该开关再运行。 -6. 从悬浮窗点击“停止整组”,或从最近任务切回管理器点击停止。运行中的返回键只让管理器退到后台,不取消调度。 +6. 主界面和悬浮窗都有“暂停/继续”。暂停立即请求结束当前脚本,确认退出后保留下一次未执行的位置。点“继续”按原顺序运行下一次,不重跑暂停结束的那次,也不计为失败重试。任务间隔中暂停会保留剩余等待时间。 +7. 从悬浮窗点击“停止整组”,或从最近任务切回管理器点击停止。暂停中同样可停止。运行中的返回键只让管理器退到后台,不取消调度。 悬浮窗显示当前任务、组轮数和单个任务次数,按住文字区可以拖动。若它遮挡任务按钮,可移开或关闭悬浮开关后运行。截屏授权、无障碍授权由原任务脚本继续处理;本管理器不上传截图。 @@ -42,6 +43,7 @@ A → A → B → A → A → B → A → A → B - “停止整组”:重试耗尽后不再调度其他任务。 - 超时:先请求停止当前引擎,再等它实际销毁,随后按失败策略重试或跳过。 - 停止整组:停止当前引擎,确认销毁后结束队列,不执行重试。 +- 暂停:结束当前这次执行,确认销毁后暂停;执行次数、顺序和整组轮数保留,继续时从下一次未执行项运行。例如 A 设置 2 次,A 的第 1 次被暂停结束后,继续运行 A 的第 2 次,再运行 B。如果已没有后续执行项,直接结束整组。暂停期间不能编辑任务。 - 15 秒仍无法确认停止:进入“停止待确认”,禁止启动下一任务。可再次点停止,或在 AutoJs6 运行列表结束旧任务;即使它稍后退出,也不会自动继续队列。 开始前若发现其他脚本正在运行,管理器会提示先结束,避免页面操作互相干扰;重复打开管理器也不会强行结束原管理器。运行中禁止改任务配置。 @@ -81,10 +83,12 @@ node --test task_manager/tests/*.test.js bash -n task_manager/deploy-oppo.sh ``` -当前 30 项离线测试通过。测试覆盖:顺序、单任务与整组次数、无限循环停止、间隔、重试与跳过、超时等待销毁、停止不响应、极速结束、启动期间取消、异常事件优先、业务日志判断,以及模拟控件的添加、排序、启用、保存和停止交互。 +当前 39 项离线测试通过。测试覆盖:顺序、单任务与整组次数、无限循环停止、间隔、重试与跳过、超时等待销毁、停止不响应、极速结束、启动期间取消、异常事件优先、业务日志判断,以及模拟控件的添加、排序、启用、保存、暂停/继续和停止交互。暂停测试涵盖当前脚本结束确认、不重跑本次执行、间隔冻结、执行次数和重试、停止覆盖暂停及引擎拒绝退出时阻止后续调度。 2026-10-21 在 OPPO 上修复启动提示找不到函数:AutoJs6 没有 `files.dirname()`,主程序启动、浏览上级目录和子脚本调度统一通过 Java File 获取父目录,测试样例直接用 `files.ensureDir(日志完整路径)` 创建父目录。离线测试不再模拟不存在的文件 API。 已同步并在 OPPO 打开管理器,控件树确认任务列表和按钮正常显示。用实际调度器和引擎适配器运行无页面点击的测试任务,验证 A→B→A→B 两轮,共成功 4 次;真实引擎事件及工作目录设置正常。验证记录保存在本地 `debug/2026-10-21/`,没有更改用户任务配置。 -悬浮窗、长期后台保活和手机上的失败重试/超时/停止仍需验证;这些分支已有离线行为测试。后续可使用 `examples` 下的样例:正常样例等待 2 秒后返回;失败样例故意抛异常;等待样例保持运行。样例只写测试日志,不点击任务应用。 +暂停功能已在 OPPO 验证:主界面及悬浮窗增加暂停/继续按钮;实际引擎 A 被暂停结束并确认销毁,暂停期间没有启动 B,继续后仅运行 B,A 没有重跑且没有触发失败重试。另验证暂停后停止整组。测试使用独立配置,没有修改用户任务列表。 + +长期后台保活和手机上的失败重试/超时分支仍需验证;这些分支已有离线行为测试。后续可使用 `examples` 下的样例:正常样例等待 2 秒后返回;失败样例故意抛异常;等待样例保持运行。样例只写测试日志,不点击任务应用。 diff --git a/task_manager/dist/任务管理器.zip b/task_manager/dist/任务管理器.zip deleted file mode 100644 index 5776549..0000000 Binary files a/task_manager/dist/任务管理器.zip and /dev/null differ diff --git a/task_manager/lib/scheduler.js b/task_manager/lib/scheduler.js index 400a9c7..d8b351a 100644 --- a/task_manager/lib/scheduler.js +++ b/task_manager/lib/scheduler.js @@ -54,16 +54,19 @@ function Scheduler(adapter, onEvent, options) { this.message = "设置任务后点击开始"; this.active = null; this.cancelled = false; + this.pauseRequested = false; + this.pauseRemainingMs = 0; this.wasBlocked = false; this.lastObserverError = ""; this.lastTask = ""; this.successes = 0; this.failures = 0; this.skipped = 0; + this.interrupted = 0; } Scheduler.prototype.isBusy = function () { - return /^(?:waiting|running|stopping|blocked)$/.test(this.phase); + return /^(?:waiting|running|pausing|paused|stopping|blocked)$/.test(this.phase); }; Scheduler.prototype.event = function (message) { @@ -86,8 +89,11 @@ Scheduler.prototype.start = function (plan, now) { this.successes = 0; this.failures = 0; this.skipped = 0; + this.interrupted = 0; this.active = null; this.cancelled = false; + this.pauseRequested = false; + this.pauseRemainingMs = 0; this.wasBlocked = false; this.lastObserverError = ""; this.lastTask = ""; @@ -99,6 +105,11 @@ Scheduler.prototype.start = function (plan, now) { Scheduler.prototype.requestStop = function () { if (!this.isBusy()) return; this.cancelled = true; + this.pauseRequested = false; + if (this.active && this.active.stopReason === "pause") { + this.active.stopReason = "user"; + if (this.phase !== "blocked") this.phase = "stopping"; + } // blocked 时依然允许重发停止请求,但绝不放行下一任务。 if (this.phase === "blocked" && this.active) { try { this.adapter.stop(this.active.handle); } catch (ignore) {} @@ -106,18 +117,58 @@ Scheduler.prototype.requestStop = function () { this.event("已请求停止整组,等待当前脚本完全退出"); }; +Scheduler.prototype.canTogglePause = function () { + return !this.cancelled && /^(?:waiting|running|pausing|paused)$/.test(this.phase); +}; + +// 暂停结束当前这次执行;确认引擎退出后保留下一次尚未执行的位置。 +Scheduler.prototype.requestPause = function (now) { + if (!this.canTogglePause() || this.pauseRequested) return; + this.pauseRequested = true; + if (this.active) { + if (this.active.stopReason === "pause") this.event("已请求暂停,等待当前脚本完全退出"); + else this.beginStop("pause", now); + } else { + this.pauseRemainingMs = Math.max(0, this.readyAt - now); + this.phase = "paused"; + this.event("整组已暂停,点击继续后运行后续任务"); + } +}; + +Scheduler.prototype.requestResume = function (now) { + if (!this.canTogglePause() || !this.pauseRequested) return; + this.pauseRequested = false; + if (this.active) { + this.event("已请求继续,等待当前脚本完全退出后运行下一次未执行项"); + return; + } + this.phase = "waiting"; + this.readyAt = now + this.pauseRemainingMs; + this.pauseRemainingMs = 0; + this.event("已继续整组,保持原任务顺序和执行次数"); +}; + +Scheduler.prototype.waitForNext = function (now) { + this.readyAt = now + this.plan.gapSeconds * 1000; + this.phase = this.pauseRequested ? "paused" : "waiting"; + if (this.pauseRequested) this.pauseRemainingMs = this.plan.gapSeconds * 1000; +}; + Scheduler.prototype.finish = function (phase, message) { this.phase = phase; + this.pauseRequested = false; + this.pauseRemainingMs = 0; this.event(message); }; Scheduler.prototype.beginStop = function (reason, now) { this.active.stopReason = reason; this.active.stopAt = now; - this.phase = "stopping"; + this.phase = reason === "pause" ? "pausing" : "stopping"; try { this.adapter.stop(this.active.handle); } catch (error) { this.active.stopError = String(error); } - this.event(reason === "user" ? "正在停止当前脚本,停止后结束整组" : reason + ";等待旧脚本退出"); + this.event(reason === "pause" ? "正在暂停:结束当前脚本,退出后暂停后续任务" : + reason === "user" ? "正在停止当前脚本,停止后结束整组" : reason + ";等待旧脚本退出"); }; Scheduler.prototype.advance = function (skipTask, now) { @@ -128,15 +179,15 @@ Scheduler.prototype.advance = function (skipTask, now) { this.attempt = 0; if (this.index >= this.plan.tasks.length) { if (this.plan.loops !== 0 && this.round >= this.plan.loops) { - this.finish("completed", "整组执行结束:成功 " + this.successes + " 次,跳过 " + this.skipped + " 个任务"); + this.finish("completed", "整组执行结束:成功 " + this.successes + " 次,跳过 " + this.skipped + " 个任务" + + (this.interrupted ? ",因暂停结束 " + this.interrupted + " 次" : "")); return; } this.round++; this.index = 0; } - this.phase = "waiting"; - this.readyAt = now + this.plan.gapSeconds * 1000; - this.event("下一项:" + this.plan.tasks[this.index].name + ";间隔 " + this.plan.gapSeconds + " 秒"); + this.waitForNext(now); + this.event((this.pauseRequested ? "整组已暂停;" : "") + "下一项:" + this.plan.tasks[this.index].name + ";间隔 " + this.plan.gapSeconds + " 秒"); }; Scheduler.prototype.failAttempt = function (reason, now) { @@ -144,9 +195,8 @@ Scheduler.prototype.failAttempt = function (reason, now) { var task = this.plan.tasks[this.index]; if (this.attempt < task.retries) { this.attempt++; - this.phase = "waiting"; - this.readyAt = now + this.plan.gapSeconds * 1000; - this.event(task.name + "失败:" + reason + ";准备重试 " + this.attempt + "/" + task.retries); + this.waitForNext(now); + this.event(task.name + "失败:" + reason + (this.pauseRequested ? ";已暂停,继续后重试 " : ";准备重试 ") + this.attempt + "/" + task.retries); } else if (task.onFailure === "skip") { this.skipped++; this.event(task.name + "失败:" + reason + ";跳过该任务的剩余次数"); @@ -158,6 +208,7 @@ Scheduler.prototype.tick = function (now) { if (!this.isBusy()) return; if (!this.active) { if (this.cancelled) { this.finish("stopped", "整组已停止"); return; } + if (this.phase === "paused") return; if (now < this.readyAt) return; var task = this.plan.tasks[this.index]; this.lastTask = task.name; @@ -181,6 +232,10 @@ Scheduler.prototype.tick = function (now) { this.adapter.release(active.handle); if (this.cancelled || this.wasBlocked || active.stopReason === "user") { this.finish("stopped", "当前脚本已退出,整组已停止"); + } else if (active.stopReason === "pause") { + this.interrupted++; + this.event(this.lastTask + "已按暂停要求结束,本次执行不重跑"); + this.advance(false, now); } else if (active.stopReason) this.failAttempt(active.stopReason, now); else if (!report.ok) this.failAttempt(report.reason || "脚本异常退出", now); else { @@ -204,11 +259,11 @@ Scheduler.prototype.tick = function (now) { Scheduler.prototype.status = function (now) { var task = this.plan && this.plan.tasks[this.index]; - return {phase: this.phase, message: this.message, taskName: task ? task.name : this.lastTask, + return {phase: this.phase, message: this.message, pauseRequested: this.pauseRequested, taskName: task ? task.name : this.lastTask, round: this.round || 0, loops: this.plan ? this.plan.loops : 0, index: (this.index || 0) + 1, taskCount: this.plan ? this.plan.tasks.length : 0, repetition: this.repetition || 0, times: task ? task.times : 0, attempt: this.attempt || 0, - successes: this.successes, failures: this.failures, skipped: this.skipped, + successes: this.successes, failures: this.failures, skipped: this.skipped, interrupted: this.interrupted, elapsedSeconds: this.active && now ? Math.floor((now - this.active.startedAt) / 1000) : 0, observerError: this.lastObserverError || ""}; }; diff --git a/task_manager/task-manager.auto.js b/task_manager/task-manager.auto.js index 75f0a29..776026c 100644 --- a/task_manager/task-manager.auto.js +++ b/task_manager/task-manager.auto.js @@ -102,7 +102,7 @@ ui.layout( '' + '' + '' + - '' + + '' + '' + '' + '