Skip to content

Commit 4ba9506

Browse files
committed
跟隨 #1327 修正
1 parent 4536794 commit 4ba9506

1 file changed

Lines changed: 34 additions & 28 deletions

File tree

src/runtime/content/sandbox.ts

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import IoC from "@App/app/ioc";
1414
import ExecScript from "./exec_script";
1515
import { BgExecScriptWarp, CATRetryError } from "./exec_warp";
1616
import { getISOWeek } from "@App/pkg/utils/utils";
17+
import { extractCronExpr } from "@App/pkg/utils/cron";
1718

1819
const utime_1min = 60 * 1000;
1920
const utime_1hr = 60 * 60 * 1000;
@@ -260,48 +261,53 @@ export default class SandboxRuntime {
260261
crontabScript(script: ScriptRunResource) {
261262
// 执行定时脚本 运行表达式
262263
if (!script.metadata.crontab) {
263-
throw new Error("错误的crontab表达式");
264+
throw new Error(script.name + " - " + t("cron_invalid_expr"));
264265
}
265266
// 如果有nextruntime,则加入重试队列
266267
this.joinRetryList(script);
267-
let flag = false;
268+
269+
const ERROR_MESSAGES: Record<number, string> = {
270+
0: "crontabScript: cron expression failed",
271+
2: "crontabScript: onTick creation failed",
272+
4: "crontabScript: create cronjob failed",
273+
6: "crontabScript: cronjob start failed",
274+
};
275+
276+
const logError = (ok: number, val: string, e: unknown) =>
277+
this.logger.error(
278+
ERROR_MESSAGES[ok] ?? "crontabScript: execution failed",
279+
{ uuid: script.uuid, crontab: val },
280+
Logger.E(e)
281+
);
282+
268283
const cronJobList: Array<CronJob> = [];
269284
script.metadata.crontab.forEach((val) => {
270-
let oncePos = 0;
271-
let crontab = val;
272-
if (crontab.indexOf("once") !== -1) {
273-
const vals = crontab.split(" ");
274-
vals.forEach((item, index) => {
275-
if (item === "once") {
276-
oncePos = index;
277-
}
278-
});
279-
if (vals.length === 5) {
280-
oncePos += 1;
281-
}
282-
crontab = crontab.replace(/once/g, "*");
283-
}
285+
let ok = 0;
284286
try {
285-
const cron = new CronJob(crontab, this.crontabExec(script, oncePos));
287+
const { cronExpr, oncePos } = extractCronExpr(val);
288+
ok = 2;
289+
const onTick = this.crontabExec(script, oncePos);
290+
ok = 4;
291+
const cron = new CronJob(cronExpr, onTick);
292+
ok = 6;
286293
cron.start();
294+
ok = 8;
287295
cronJobList.push(cron);
288296
} catch (e) {
289-
flag = true;
290-
this.logger.error("create cronjob failed", {
291-
script: script.id,
292-
crontab: val,
293-
});
297+
logError(ok, val, e);
294298
}
295299
});
296-
if (cronJobList.length !== script.metadata.crontab.length) {
300+
301+
const allSucceeded = cronJobList.length === script.metadata.crontab.length;
302+
if (allSucceeded) {
303+
this.cronJob.set(script.id, cronJobList);
304+
} else {
297305
// 有表达式失败了
298-
cronJobList.forEach((crontab) => {
306+
for (const crontab of cronJobList) {
299307
crontab.stop();
300-
});
301-
} else {
302-
this.cronJob.set(script.id, cronJobList);
308+
}
303309
}
304-
return Promise.resolve(!flag);
310+
return allSucceeded;
305311
}
306312

307313
crontabExec(script: ScriptRunResource, oncePos: number): () => Promise<any> {

0 commit comments

Comments
 (0)