diff --git a/back/shared/pLimit.ts b/back/shared/pLimit.ts index 38865e69..91e0d15b 100644 --- a/back/shared/pLimit.ts +++ b/back/shared/pLimit.ts @@ -14,6 +14,7 @@ import { import config from '../config'; import { credentials } from '@grpc/grpc-js'; import { ApiClient } from '../protos/api'; +import { CrontabModel } from '../data/cron'; class TaskLimit { private dependenyLimit = new PQueue({ concurrency: 1 }); @@ -136,7 +137,6 @@ class TaskLimit { let maxQueueSize = 10; // Default for multi-instance mode (increased from 5) let isSingleInstanceMode = false; try { - const { CrontabModel } = await import('../data/cron'); const cronRecord = await CrontabModel.findOne({ where: { id: Number(cron.id) }, }); diff --git a/back/shared/runCron.ts b/back/shared/runCron.ts index 4b67fd5d..dcc86a14 100644 --- a/back/shared/runCron.ts +++ b/back/shared/runCron.ts @@ -51,10 +51,16 @@ export function runCron(cmd: string, cron: ICron): Promise { const cp = spawn(cmd, { shell: '/bin/bash' }); // Update status to running after spawning the process - await CrontabModel.update( - { status: CrontabStatus.running, pid: cp.pid }, - { where: { id: Number(cron.id) } }, - ); + try { + await CrontabModel.update( + { status: CrontabStatus.running, pid: cp.pid }, + { where: { id: Number(cron.id) } }, + ); + } catch (error) { + Logger.error( + `[schedule][更新任务状态失败] 任务ID: ${cron.id}, 错误: ${error}`, + ); + } cp.stderr.on('data', (data) => { Logger.info( @@ -74,10 +80,16 @@ export function runCron(cmd: string, cron: ICron): Promise { cp.on('exit', async (code) => { taskLimit.removeQueuedCron(cron.id); // Update status to idle after task completes - await CrontabModel.update( - { status: CrontabStatus.idle, pid: undefined }, - { where: { id: Number(cron.id) } }, - ); + try { + await CrontabModel.update( + { status: CrontabStatus.idle, pid: undefined }, + { where: { id: Number(cron.id) } }, + ); + } catch (error) { + Logger.error( + `[schedule][更新任务状态失败] 任务ID: ${cron.id}, 错误: ${error}`, + ); + } Logger.info( '[schedule][执行任务结束] 参数: %s, 退出码: %j', JSON.stringify({