自动任务管理增加暂停选项

This commit is contained in:
hmz 2026-10-21 15:41:55 +08:00
parent ec26e73bba
commit 5a5cd234fa
6 changed files with 197 additions and 18 deletions

View File

@ -24,7 +24,8 @@
3. 用“添加脚本”选择其他 `.js`;用“上移/下移”排序,勾选启用任务,分别配置次数、超时、重试和失败策略。删除任务只移除列表,保留脚本文件。 3. 用“添加脚本”选择其他 `.js`;用“上移/下移”排序,勾选启用任务,分别配置次数、超时、重试和失败策略。删除任务只移除列表,保留脚本文件。
4. 设置整组循环次数,`0` 表示无限循环;任务间隔也应用于重复执行和失败重试。 4. 设置整组循环次数,`0` 表示无限循环;任务间隔也应用于重复执行和失败重试。
5. 点击“开始整组”。界面会退到后台,任务自行打开对应应用。开启悬浮状态时,需先允许 AutoJs6 的悬浮窗权限;也可关闭该开关再运行。 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 运行列表结束旧任务;即使它稍后退出,也不会自动继续队列。 - 15 秒仍无法确认停止:进入“停止待确认”,禁止启动下一任务。可再次点停止,或在 AutoJs6 运行列表结束旧任务;即使它稍后退出,也不会自动继续队列。
开始前若发现其他脚本正在运行,管理器会提示先结束,避免页面操作互相干扰;重复打开管理器也不会强行结束原管理器。运行中禁止改任务配置。 开始前若发现其他脚本正在运行,管理器会提示先结束,避免页面操作互相干扰;重复打开管理器也不会强行结束原管理器。运行中禁止改任务配置。
@ -81,10 +83,12 @@ node --test task_manager/tests/*.test.js
bash -n task_manager/deploy-oppo.sh bash -n task_manager/deploy-oppo.sh
``` ```
当前 30 项离线测试通过。测试覆盖:顺序、单任务与整组次数、无限循环停止、间隔、重试与跳过、超时等待销毁、停止不响应、极速结束、启动期间取消、异常事件优先、业务日志判断,以及模拟控件的添加、排序、启用、保存和停止交互。 当前 39 项离线测试通过。测试覆盖:顺序、单任务与整组次数、无限循环停止、间隔、重试与跳过、超时等待销毁、停止不响应、极速结束、启动期间取消、异常事件优先、业务日志判断,以及模拟控件的添加、排序、启用、保存、暂停/继续和停止交互。暂停测试涵盖当前脚本结束确认、不重跑本次执行、间隔冻结、执行次数和重试、停止覆盖暂停及引擎拒绝退出时阻止后续调度。
2026-10-21 在 OPPO 上修复启动提示找不到函数:AutoJs6 没有 `files.dirname()`,主程序启动、浏览上级目录和子脚本调度统一通过 Java File 获取父目录,测试样例直接用 `files.ensureDir(日志完整路径)` 创建父目录。离线测试不再模拟不存在的文件 API。 2026-10-21 在 OPPO 上修复启动提示找不到函数:AutoJs6 没有 `files.dirname()`,主程序启动、浏览上级目录和子脚本调度统一通过 Java File 获取父目录,测试样例直接用 `files.ensureDir(日志完整路径)` 创建父目录。离线测试不再模拟不存在的文件 API。
已同步并在 OPPO 打开管理器,控件树确认任务列表和按钮正常显示。用实际调度器和引擎适配器运行无页面点击的测试任务,验证 A→B→A→B 两轮,共成功 4 次;真实引擎事件及工作目录设置正常。验证记录保存在本地 `debug/2026-10-21/`,没有更改用户任务配置。 已同步并在 OPPO 打开管理器,控件树确认任务列表和按钮正常显示。用实际调度器和引擎适配器运行无页面点击的测试任务,验证 A→B→A→B 两轮,共成功 4 次;真实引擎事件及工作目录设置正常。验证记录保存在本地 `debug/2026-10-21/`,没有更改用户任务配置。
悬浮窗、长期后台保活和手机上的失败重试/超时/停止仍需验证;这些分支已有离线行为测试。后续可使用 `examples` 下的样例:正常样例等待 2 秒后返回;失败样例故意抛异常;等待样例保持运行。样例只写测试日志,不点击任务应用。 暂停功能已在 OPPO 验证:主界面及悬浮窗增加暂停/继续按钮;实际引擎 A 被暂停结束并确认销毁,暂停期间没有启动 B,继续后仅运行 B,A 没有重跑且没有触发失败重试。另验证暂停后停止整组。测试使用独立配置,没有修改用户任务列表。
长期后台保活和手机上的失败重试/超时分支仍需验证;这些分支已有离线行为测试。后续可使用 `examples` 下的样例:正常样例等待 2 秒后返回;失败样例故意抛异常;等待样例保持运行。样例只写测试日志,不点击任务应用。

Binary file not shown.

View File

@ -54,16 +54,19 @@ function Scheduler(adapter, onEvent, options) {
this.message = "设置任务后点击开始"; this.message = "设置任务后点击开始";
this.active = null; this.active = null;
this.cancelled = false; this.cancelled = false;
this.pauseRequested = false;
this.pauseRemainingMs = 0;
this.wasBlocked = false; this.wasBlocked = false;
this.lastObserverError = ""; this.lastObserverError = "";
this.lastTask = ""; this.lastTask = "";
this.successes = 0; this.successes = 0;
this.failures = 0; this.failures = 0;
this.skipped = 0; this.skipped = 0;
this.interrupted = 0;
} }
Scheduler.prototype.isBusy = function () { 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) { Scheduler.prototype.event = function (message) {
@ -86,8 +89,11 @@ Scheduler.prototype.start = function (plan, now) {
this.successes = 0; this.successes = 0;
this.failures = 0; this.failures = 0;
this.skipped = 0; this.skipped = 0;
this.interrupted = 0;
this.active = null; this.active = null;
this.cancelled = false; this.cancelled = false;
this.pauseRequested = false;
this.pauseRemainingMs = 0;
this.wasBlocked = false; this.wasBlocked = false;
this.lastObserverError = ""; this.lastObserverError = "";
this.lastTask = ""; this.lastTask = "";
@ -99,6 +105,11 @@ Scheduler.prototype.start = function (plan, now) {
Scheduler.prototype.requestStop = function () { Scheduler.prototype.requestStop = function () {
if (!this.isBusy()) return; if (!this.isBusy()) return;
this.cancelled = true; 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 时依然允许重发停止请求,但绝不放行下一任务。 // blocked 时依然允许重发停止请求,但绝不放行下一任务。
if (this.phase === "blocked" && this.active) { if (this.phase === "blocked" && this.active) {
try { this.adapter.stop(this.active.handle); } catch (ignore) {} try { this.adapter.stop(this.active.handle); } catch (ignore) {}
@ -106,18 +117,58 @@ Scheduler.prototype.requestStop = function () {
this.event("已请求停止整组,等待当前脚本完全退出"); 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) { Scheduler.prototype.finish = function (phase, message) {
this.phase = phase; this.phase = phase;
this.pauseRequested = false;
this.pauseRemainingMs = 0;
this.event(message); this.event(message);
}; };
Scheduler.prototype.beginStop = function (reason, now) { Scheduler.prototype.beginStop = function (reason, now) {
this.active.stopReason = reason; this.active.stopReason = reason;
this.active.stopAt = now; this.active.stopAt = now;
this.phase = "stopping"; this.phase = reason === "pause" ? "pausing" : "stopping";
try { this.adapter.stop(this.active.handle); } try { this.adapter.stop(this.active.handle); }
catch (error) { this.active.stopError = String(error); } 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) { Scheduler.prototype.advance = function (skipTask, now) {
@ -128,15 +179,15 @@ Scheduler.prototype.advance = function (skipTask, now) {
this.attempt = 0; this.attempt = 0;
if (this.index >= this.plan.tasks.length) { if (this.index >= this.plan.tasks.length) {
if (this.plan.loops !== 0 && this.round >= this.plan.loops) { 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; return;
} }
this.round++; this.round++;
this.index = 0; this.index = 0;
} }
this.phase = "waiting"; this.waitForNext(now);
this.readyAt = now + this.plan.gapSeconds * 1000; this.event((this.pauseRequested ? "整组已暂停;" : "") + "下一项:" + this.plan.tasks[this.index].name + ";间隔 " + this.plan.gapSeconds + " 秒");
this.event("下一项:" + this.plan.tasks[this.index].name + ";间隔 " + this.plan.gapSeconds + " 秒");
}; };
Scheduler.prototype.failAttempt = function (reason, now) { Scheduler.prototype.failAttempt = function (reason, now) {
@ -144,9 +195,8 @@ Scheduler.prototype.failAttempt = function (reason, now) {
var task = this.plan.tasks[this.index]; var task = this.plan.tasks[this.index];
if (this.attempt < task.retries) { if (this.attempt < task.retries) {
this.attempt++; this.attempt++;
this.phase = "waiting"; this.waitForNext(now);
this.readyAt = now + this.plan.gapSeconds * 1000; this.event(task.name + "失败:" + reason + (this.pauseRequested ? ";已暂停,继续后重试 " : ";准备重试 ") + this.attempt + "/" + task.retries);
this.event(task.name + "失败:" + reason + ";准备重试 " + this.attempt + "/" + task.retries);
} else if (task.onFailure === "skip") { } else if (task.onFailure === "skip") {
this.skipped++; this.skipped++;
this.event(task.name + "失败:" + reason + ";跳过该任务的剩余次数"); this.event(task.name + "失败:" + reason + ";跳过该任务的剩余次数");
@ -158,6 +208,7 @@ Scheduler.prototype.tick = function (now) {
if (!this.isBusy()) return; if (!this.isBusy()) return;
if (!this.active) { if (!this.active) {
if (this.cancelled) { this.finish("stopped", "整组已停止"); return; } if (this.cancelled) { this.finish("stopped", "整组已停止"); return; }
if (this.phase === "paused") return;
if (now < this.readyAt) return; if (now < this.readyAt) return;
var task = this.plan.tasks[this.index]; var task = this.plan.tasks[this.index];
this.lastTask = task.name; this.lastTask = task.name;
@ -181,6 +232,10 @@ Scheduler.prototype.tick = function (now) {
this.adapter.release(active.handle); this.adapter.release(active.handle);
if (this.cancelled || this.wasBlocked || active.stopReason === "user") { if (this.cancelled || this.wasBlocked || active.stopReason === "user") {
this.finish("stopped", "当前脚本已退出,整组已停止"); 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 (active.stopReason) this.failAttempt(active.stopReason, now);
else if (!report.ok) this.failAttempt(report.reason || "脚本异常退出", now); else if (!report.ok) this.failAttempt(report.reason || "脚本异常退出", now);
else { else {
@ -204,11 +259,11 @@ Scheduler.prototype.tick = function (now) {
Scheduler.prototype.status = function (now) { Scheduler.prototype.status = function (now) {
var task = this.plan && this.plan.tasks[this.index]; 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, round: this.round || 0, loops: this.plan ? this.plan.loops : 0,
index: (this.index || 0) + 1, taskCount: this.plan ? this.plan.tasks.length : 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, 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, elapsedSeconds: this.active && now ? Math.floor((now - this.active.startedAt) / 1000) : 0,
observerError: this.lastObserverError || ""}; observerError: this.lastObserverError || ""};
}; };

View File

@ -102,7 +102,7 @@ ui.layout(
'<input id="gapInput" inputType="number" singleLine="true" textSize="17sp"/></vertical>' + '<input id="gapInput" inputType="number" singleLine="true" textSize="17sp"/></vertical>' +
'</horizontal>' + '</horizontal>' +
'<Switch id="floatSwitch" text="运行时显示悬浮状态和停止按钮" textSize="14sp" padding="0 8"/>' + '<Switch id="floatSwitch" text="运行时显示悬浮状态和停止按钮" textSize="14sp" padding="0 8"/>' +
'<text text="管理器需保持运行,界面可以退到后台。停止整组会同时停止当前任务。" textSize="12sp" textColor="#667085"/>' + '<text text="暂停会结束当前脚本;继续时从下一次未执行项接着运行。管理器需保持运行,界面可以退到后台。" textSize="12sp" textColor="#667085"/>' +
'<horizontal gravity="center_vertical" marginTop="16">' + '<horizontal gravity="center_vertical" marginTop="16">' +
'<text text="任务顺序" textSize="17sp" textStyle="bold" textColor="#172B4D" w="0" layout_weight="1"/>' + '<text text="任务顺序" textSize="17sp" textStyle="bold" textColor="#172B4D" w="0" layout_weight="1"/>' +
'<button id="addButton" text="添加脚本" textSize="14sp"/>' + '<button id="addButton" text="添加脚本" textSize="14sp"/>' +
@ -114,6 +114,7 @@ ui.layout(
'<horizontal padding="10 6" bg="#FFFFFF">' + '<horizontal padding="10 6" bg="#FFFFFF">' +
'<button id="saveButton" text="保存" w="0" layout_weight="1"/>' + '<button id="saveButton" text="保存" w="0" layout_weight="1"/>' +
'<button id="startButton" text="开始整组" w="0" layout_weight="2" textColor="#173B71"/>' + '<button id="startButton" text="开始整组" w="0" layout_weight="2" textColor="#173B71"/>' +
'<button id="pauseButton" text="暂停" w="0" layout_weight="2" textColor="#173B71"/>' +
'<button id="stopButton" text="停止整组" w="0" layout_weight="2" textColor="#B42318"/>' + '<button id="stopButton" text="停止整组" w="0" layout_weight="2" textColor="#B42318"/>' +
'</horizontal>' + '</horizontal>' +
'</vertical>' '</vertical>'
@ -246,13 +247,20 @@ function editTask(index) {
function closeFloating() { function closeFloating() {
if (floatingWindow) { floatingWindow.close(); floatingWindow = null; } if (floatingWindow) { floatingWindow.close(); floatingWindow = null; }
} }
function togglePause() {
if (scheduler.pauseRequested) scheduler.requestResume(Date.now());
else scheduler.requestPause(Date.now());
updateScreen();
}
function showFloating() { function showFloating() {
if (!plan.floating) return; if (!plan.floating) return;
floatingWindow = floaty.rawWindow('<vertical padding="8" bg="#E6173B71" w="152">' + floatingWindow = floaty.rawWindow('<vertical padding="8" bg="#E6173B71" w="152">' +
'<text id="statusLabel" text="整组运行中 · 拖动可移动" textSize="12sp" textColor="#FFFFFF" padding="0 5"/>' + '<text id="statusLabel" text="整组运行中 · 拖动可移动" textSize="12sp" textColor="#FFFFFF" padding="0 5"/>' +
'<button id="pauseQueue" text="暂停" h="44" textSize="13sp"/>' +
'<button id="stopQueue" text="停止整组" h="44" textSize="13sp"/></vertical>'); '<button id="stopQueue" text="停止整组" h="44" textSize="13sp"/></vertical>');
floatingWindow.setPosition(0, Math.round(device.height * 0.25)); floatingWindow.setPosition(0, Math.round(device.height * 0.25));
floatingWindow.stopQueue.on("click", guard(function () { scheduler.requestStop(); })); floatingWindow.stopQueue.on("click", guard(function () { scheduler.requestStop(); }));
floatingWindow.pauseQueue.on("click", guard(togglePause));
var drag = null; var drag = null;
floatingWindow.statusLabel.setOnTouchListener(new android.view.View.OnTouchListener({onTouch: function (view, event) { floatingWindow.statusLabel.setOnTouchListener(new android.view.View.OnTouchListener({onTouch: function (view, event) {
if (!floatingWindow) return false; if (!floatingWindow) return false;
@ -268,19 +276,24 @@ function showFloating() {
function updateScreen() { function updateScreen() {
if (!scheduler || closing) return; if (!scheduler || closing) return;
var state = scheduler.status(Date.now()); var state = scheduler.status(Date.now());
var names = {idle: "尚未开始", waiting: "等待下一次执行", running: "正在执行", stopping: "正在停止", blocked: "停止待确认", var names = {idle: "尚未开始", waiting: "等待下一次执行", running: "正在执行", pausing: "正在结束当前脚本", paused: "整组已暂停", stopping: "正在停止", blocked: "停止待确认",
completed: "整组已结束", stopped: "整组已停止", failed: "因失败停止"}; completed: "整组已结束", stopped: "整组已停止", failed: "因失败停止"};
ui.phaseText.setText(names[state.phase]); ui.phaseText.setText(names[state.phase]);
ui.progressText.setText(state.taskCount ? "第 " + state.round + "/" + (state.loops || "∞") + " 轮 · " + state.taskName + ui.progressText.setText(state.taskCount ? "第 " + state.round + "/" + (state.loops || "∞") + " 轮 · " + state.taskName +
"\n本轮第 " + Math.min(state.index, state.taskCount) + "/" + state.taskCount + " 个任务 · 第 " + state.repetition + "/" + (state.times || "—") + "\n本轮第 " + Math.min(state.index, state.taskCount) + "/" + state.taskCount + " 个任务 · 第 " + state.repetition + "/" + (state.times || "—") +
" 次 · 已运行 " + state.elapsedSeconds + " 秒\n成功 " + state.successes + " 次 · 失败尝试 " + state.failures + " 次 · 跳过 " + state.skipped + " 个任务" : "还未启动任务"); " 次 · 已运行 " + state.elapsedSeconds + " 秒\n成功 " + state.successes + " 次 · 失败尝试 " + state.failures + " 次 · 跳过 " + state.skipped + " 个任务" +
(state.interrupted ? " · 暂停结束 " + state.interrupted + " 次" : "") : "还未启动任务");
ui.messageText.setText(state.message + (state.observerError ? "\n运行记录保存失败:" + state.observerError : "")); ui.messageText.setText(state.message + (state.observerError ? "\n运行记录保存失败:" + state.observerError : ""));
ui.logText.setText(recentLogs.join("\n") || "等待开始"); ui.logText.setText(recentLogs.join("\n") || "等待开始");
var busy = scheduler.isBusy(); var busy = scheduler.isBusy();
[ui.startButton, ui.saveButton, ui.addButton, ui.loopsInput, ui.gapInput, ui.floatSwitch].forEach(function (view) { view.setEnabled(!busy); }); [ui.startButton, ui.saveButton, ui.addButton, ui.loopsInput, ui.gapInput, ui.floatSwitch].forEach(function (view) { view.setEnabled(!busy); });
ui.stopButton.setEnabled(busy); ui.stopButton.setEnabled(busy);
ui.pauseButton.setEnabled(scheduler.canTogglePause());
ui.pauseButton.setText(state.pauseRequested ? "继续" : "暂停");
rowControls.forEach(function (controls) { controls.forEach(function (view) { view.setEnabled(!busy); }); }); rowControls.forEach(function (controls) { controls.forEach(function (view) { view.setEnabled(!busy); }); });
if (floatingWindow) { if (floatingWindow) {
floatingWindow.pauseQueue.setEnabled(scheduler.canTogglePause());
floatingWindow.pauseQueue.setText(state.pauseRequested ? "继续" : "暂停");
floatingWindow.statusLabel.setText(state.taskName + "\n第 " + state.round + " 轮 · " + state.repetition + "/" + state.times + " 次\n" + names[state.phase]); floatingWindow.statusLabel.setText(state.taskName + "\n第 " + state.round + " 轮 · " + state.repetition + "/" + state.times + " 次\n" + names[state.phase]);
if (!busy) closeFloating(); if (!busy) closeFloating();
} }
@ -294,6 +307,7 @@ try {
ui.addButton.on("click", guard(function () { ensureIdle(); editTask(-1); })); ui.addButton.on("click", guard(function () { ensureIdle(); editTask(-1); }));
ui.saveButton.on("click", guard(function () { ensureIdle(); persist(); toast("配置已保存"); })); ui.saveButton.on("click", guard(function () { ensureIdle(); persist(); toast("配置已保存"); }));
ui.stopButton.on("click", guard(function () { scheduler.requestStop(); updateScreen(); })); ui.stopButton.on("click", guard(function () { scheduler.requestStop(); updateScreen(); }));
ui.pauseButton.on("click", guard(togglePause));
ui.startButton.on("click", guard(function () { ui.startButton.on("click", guard(function () {
ensureIdle(); ensureIdle();
var candidate = settingsPlan(); var candidate = settingsPlan();

View File

@ -66,6 +66,95 @@ test('启动前和间隔期间停止均不多运行一个任务', () => {
} }
}); });
test('启动前暂停阻止首个任务启动,继续后恢复原来的等待时间', () => {
const {s, launches} = harness();
s.start(plan([task('A')]), 500);
s.requestPause(100); s.tick(100000);
assert.equal(s.phase, 'paused'); assert.equal(s.isBusy(), true);
assert.equal(launches.length, 0);
assert.throws(() => s.start(plan([task('B')]), 100001), /尚未完全停止/);
s.requestResume(100000); s.tick(100399);
assert.equal(launches.length, 0);
s.tick(100400); assert.equal(launches.length, 1);
});
test('运行中暂停会结束当前脚本,确认退出后暂停下一项,继续不重跑当前任务', () => {
const {s, launches, stops} = harness();
s.start(plan([task('A'), task('B')]), 0); s.tick(0);
s.requestPause(100); s.tick(101);
assert.equal(s.phase, 'pausing'); assert.equal(stops.length, 1);
launches[0].done = true; s.tick(200); s.tick(100000);
assert.equal(s.phase, 'paused'); assert.equal(s.successes, 0); assert.equal(s.interrupted, 1);
assert.equal(launches.length, 1);
s.requestResume(100000); s.tick(100001);
assert.deepEqual(launches.map(h => h.task.name), ['A', 'B']);
});
test('任务间隔内暂停保留剩余间隔,重复暂停和继续不会重复启动', () => {
const {s, launches} = harness();
s.start(plan([task('A'), task('B')], {gapSeconds: 10}), 0); s.tick(0);
launches[0].done = true; s.tick(100);
s.requestPause(3100); s.requestPause(5100);
s.requestResume(100000); s.requestResume(101000);
s.tick(106999); assert.equal(launches.length, 1);
s.tick(107000); assert.equal(launches.length, 2);
});
test('当前脚本退出确认前点击继续,仍等待旧引擎销毁才运行下一项', () => {
const {s, launches} = harness();
s.start(plan([task('A'), task('B')]), 0); s.tick(0);
s.requestPause(100); s.requestResume(200); s.tick(300);
assert.equal(s.phase, 'pausing'); assert.equal(launches.length, 1);
launches[0].done = true; s.tick(400); s.tick(401);
assert.equal(launches.length, 2);
});
test('暂停中仍能停止整组,当前任务未退出时不放行后续任务', () => {
for (const active of [false, true]) {
const {s, launches, stops} = harness();
s.start(plan([task('A'), task('B')]), 0);
if (active) s.tick(0);
s.requestPause(100); s.requestStop(); s.tick(200);
assert.equal(s.canTogglePause(), false);
if (active) {
assert.equal(stops.length, 1); assert.equal(s.phase, 'stopping');
launches[0].done = true; s.tick(300);
}
assert.equal(s.phase, 'stopped');
s.tick(100000); assert.equal(launches.length, active ? 1 : 0);
}
});
test('暂停结束的执行不触发失败重试,继续运行同任务的下一次未执行次数', () => {
const {s, launches, stops} = harness();
s.start(plan([task('A', {times: 2, retries: 1})]), 0); s.tick(0);
s.requestPause(100); s.tick(200);
assert.equal(stops.length, 1); assert.equal(s.phase, 'pausing');
launches[0].done = true; launches[0].ok = false; s.tick(1000); s.tick(100000);
assert.equal(s.phase, 'paused'); assert.equal(s.attempt, 0); assert.equal(s.repetition, 2);
assert.equal(s.failures, 0);
assert.equal(launches.length, 1);
s.requestResume(100000); s.tick(100001);
assert.equal(launches.length, 2);
});
test('暂停时旧脚本无法退出会阻止继续,晚到退出也不自动运行后续任务', () => {
const {s, launches} = harness({stopGraceMs: 1000});
s.start(plan([task('A'), task('B')]), 0); s.tick(0); s.requestPause(100);
s.tick(1100); assert.equal(s.phase, 'blocked');
s.requestResume(1200); s.tick(1300); assert.equal(launches.length, 1);
launches[0].done = true; s.tick(1400); s.tick(1500);
assert.equal(s.phase, 'stopped'); assert.equal(launches.length, 1);
});
test('最后一个任务完成时正常结束整组,不停在没有后续任务的暂停状态', () => {
const {s, launches} = harness();
s.start(plan([task('A')]), 0); s.tick(0); s.requestPause(100);
launches[0].done = true; s.tick(200);
assert.equal(s.phase, 'completed'); assert.equal(s.pauseRequested, false);
assert.equal(s.canTogglePause(), false);
});
test('失败重试后恢复,不占用配置的正常执行次数,执行间隔也作用于重试', () => { test('失败重试后恢复,不占用配置的正常执行次数,执行间隔也作用于重试', () => {
const {s, launches} = harness(); const {s, launches} = harness();
s.start(plan([task('A', {times: 2, retries: 1})], {gapSeconds: 2}), 0); s.start(plan([task('A', {times: 2, retries: 1})], {gapSeconds: 2}), 0);

View File

@ -132,6 +132,23 @@ test('缺少悬浮权限时不启动任务,不将权限请求误算为正在
assert.match(h.toasts[0], /悬浮窗权限/); assert.match(h.toasts[0], /悬浮窗权限/);
}); });
test('主界面暂停继续按钮保留任务状态,暂停后可停止并恢复编辑', () => {
const h = load();
assert.equal(h.ui.pauseButton.enabled, false);
h.ui.floatSwitch.setChecked(false); h.ui.startButton.emit('click'); h.advance(500);
h.ui.pauseButton.emit('click');
assert.equal(h.ctx.scheduler.phase, 'pausing'); assert.equal(h.ui.pauseButton.value, '继续');
const e = h.launches[0].engine; e.destroyed = true; h.engines.emit('finish', e); h.advance(500);
assert.equal(h.ctx.scheduler.phase, 'paused'); assert.equal(h.ui.addButton.enabled, false);
h.advance(100000); assert.equal(h.launches.length, 1);
h.ui.pauseButton.emit('click'); h.advance(3000);
assert.equal(h.launches.length, 2); assert.equal(h.ui.pauseButton.value, '暂停');
h.ui.pauseButton.emit('click'); h.ui.stopButton.emit('click'); h.advance(500);
const next = h.launches[1].engine; next.destroyed = true; h.engines.emit('finish', next); h.advance(500);
assert.equal(h.ctx.scheduler.phase, 'stopped');
assert.equal(h.ui.pauseButton.enabled, false); assert.equal(h.ui.addButton.enabled, true);
});
test('任务删除只移除列表,不删除脚本文件;运行中返回键只退到后台', () => { test('任务删除只移除列表,不删除脚本文件;运行中返回键只退到后台', () => {
const h = load(); h.ctx.rowControls[1][4].emit('click'); const h = load(); h.ctx.rowControls[1][4].emit('click');
assert.equal(h.ctx.plan.tasks.length, 1); assert.equal(h.ctx.plan.tasks.length, 1);