app_auto_all/task_manager/lib/autojs-adapter.js
2026-10-21 15:14:07 +08:00

109 lines
4.9 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"use strict";
var resultFromLog = require("./scheduler.js").resultFromLog;
// AutoJs6 >= 6.6.3:全局 finish/error 事件 + ScriptEngine.isDestroyed() 双重检查。
// finish 事件在引擎销毁前发出,不能把它本身作为启动下一脚本的信号。
function createAdapter(runtime) {
var engines = runtime.engines;
var files = runtime.files;
if (typeof engines.on !== "function" || typeof engines.removeListener !== "function") {
throw new Error("此管理器需要 AutoJs6 6.6.3 或更新版本的脚本引擎事件功能");
}
var parentDirectory = runtime.parentDirectory;
if (typeof parentDirectory !== "function") throw new Error("未提供脚本父目录解析函数");
var records = {};
var recordOrder = [];
var handles = [];
function idOf(engine) { return String(engine.getId()); }
function record(engine) {
if (!engine) return null;
var id = idOf(engine);
if (!records[id]) {
records[id] = {finished: false, error: ""};
recordOrder.push(id);
if (recordOrder.length > 200) delete records[recordOrder.shift()];
}
return records[id];
}
function onFinish(engine) { var r = record(engine); if (r) r.finished = true; }
function onError(engine, args) {
var r = record(engine);
if (!r) return;
// 当前 AutoJs6 的事件第二参数是 Java Object[];也兼容直接传异常。
var cause = args;
try { if (args && args.length) cause = args[0]; } catch (ignore) {}
r.error = String(cause || "脚本引擎报告异常");
}
engines.on("finish", onFinish);
engines.on("error", onError);
function readLog(path) {
if (!path || !files.exists(path)) return "";
return String(files.read(path)).slice(-32000);
}
function forcePending(handle) {
if (handle.stopRequested && handle.engine && !handle.stopIssued) {
handle.engine.forceStop();
handle.stopIssued = true;
}
}
var adapter = {
launch: function (task, now) {
if (!files.exists(task.path) || files.isDir(task.path)) throw new Error("脚本不存在:" + task.path);
var before = readLog(task.resultLog);
// 由管理器逐次启动;不能用 loopTimes,否则异常和次数无法独立处理。
var execution = engines.execScriptFile(task.path, {path: parentDirectory(task.path), delay: 0, loopTimes: 1});
var handle = {execution: execution, id: String(execution.getId()), task: task,
startedAt: now, engine: null, stopRequested: false, stopIssued: false,
destroyedAt: null, before: before};
handles.push(handle);
return handle;
},
poll: function (handle, now) {
if (!handle.engine) handle.engine = handle.execution.getEngine();
if (!handle.engine) {
if (now - handle.startedAt >= 15000) throw new Error("启动15秒后仍未取得脚本引擎,无法确认启动状态");
return {done: false};
}
forcePending(handle);
if (!handle.engine.isDestroyed()) return {done: false};
if (handle.destroyedAt === null) handle.destroyedAt = now;
var r = records[handle.id] || {finished: false, error: ""};
var uncaught = handle.engine.getUncaughtException();
if (uncaught) r.error = String(uncaught);
if (r.error) return {done: true, ok: false, reason: r.error};
if (!r.finished) {
if (now - handle.destroyedAt < 1000) return {done: false};
return {done: true, ok: false, reason: "脚本已销毁但未收到正常结束事件,可能被外部停止"};
}
if (handle.task.resultLog) {
var result;
try { result = resultFromLog(handle.before, readLog(handle.task.resultLog)); }
catch (error) { result = {ok: false, reason: "无法读取本轮结果日志:" + error}; }
return {done: true, ok: result.ok, reason: result.reason};
}
return {done: true, ok: true};
},
stop: function (handle) {
handle.stopRequested = true;
handle.stopIssued = false;
if (!handle.engine) handle.engine = handle.execution.getEngine();
forcePending(handle);
},
release: function (handle) {
delete records[handle.id];
handles = handles.filter(function (other) { return other !== handle; });
},
close: function () {
handles.forEach(function (handle) { try { adapter.stop(handle); } catch (ignore) {} });
engines.removeListener("finish", onFinish);
engines.removeListener("error", onError);
}
};
return adapter;
}
module.exports = {createAdapter: createAdapter};