(script: ScriptLoadInfo)
| 238 | private crontabSripts: ScriptRunResource[] = []; |
| 239 | |
| 240 | crontabScript(script: ScriptLoadInfo) { |
| 241 | // 执行定时脚本 运行表达式 |
| 242 | if (!script.metadata.crontab) { |
| 243 | throw new Error(script.name + " - " + t("cron_invalid_expr")); |
| 244 | } |
| 245 | // 如果有nextruntime,则加入重试队列 |
| 246 | this.joinRetryList(script); |
| 247 | this.crontabSripts.push(script); |
| 248 | |
| 249 | const ERROR_MESSAGES: Record<number, string> = { |
| 250 | 0: "crontabScript: cron expression failed", |
| 251 | 2: "crontabScript: onTick creation failed", |
| 252 | 4: "crontabScript: create cronjob failed", |
| 253 | 6: "crontabScript: cronjob start failed", |
| 254 | }; |
| 255 | |
| 256 | const logError = (ok: number, val: string, e: unknown) => |
| 257 | this.logger.error( |
| 258 | ERROR_MESSAGES[ok] ?? "crontabScript: execution failed", |
| 259 | { uuid: script.uuid, crontab: val }, |
| 260 | Logger.E(e) |
| 261 | ); |
| 262 | |
| 263 | const cronJobList: Array<CronJob> = []; |
| 264 | script.metadata.crontab.forEach((val) => { |
| 265 | let ok = 0; |
| 266 | try { |
| 267 | const { cronExpr, oncePos } = extractCronExpr(val); |
| 268 | ok = 2; |
| 269 | const onTick = this.crontabExec(script, oncePos); |
| 270 | ok = 4; |
| 271 | const cron = new CronJob(cronExpr, onTick); |
| 272 | ok = 6; |
| 273 | cron.start(); |
| 274 | ok = 8; |
| 275 | cronJobList.push(cron); |
| 276 | } catch (e) { |
| 277 | logError(ok, val, e); |
| 278 | } |
| 279 | }); |
| 280 | |
| 281 | const allSucceeded = cronJobList.length === script.metadata.crontab.length; |
| 282 | if (allSucceeded) { |
| 283 | this.cronJob.set(script.uuid, cronJobList); |
| 284 | } else { |
| 285 | // 有表达式失败了 |
| 286 | for (const crontab of cronJobList) { |
| 287 | crontab.stop(); |
| 288 | } |
| 289 | } |
| 290 | return allSucceeded; |
| 291 | } |
| 292 | |
| 293 | crontabExec(script: ScriptLoadInfo, oncePos: number) { |
| 294 | return this.execScript.bind(this, script, { cronJobOncePos: oncePos }); |
no test coverage detected