Address code review feedback

- Wrapped status updates in try-catch blocks to handle database errors
- Moved CrontabModel import to top of pLimit.ts to avoid repeated dynamic imports

Co-authored-by: whyour <22700758+whyour@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot] 2025-12-30 16:43:16 +00:00
parent 58eb9feec0
commit 0bbff927b1
2 changed files with 21 additions and 9 deletions

View File

@ -14,6 +14,7 @@ import {
import config from '../config'; import config from '../config';
import { credentials } from '@grpc/grpc-js'; import { credentials } from '@grpc/grpc-js';
import { ApiClient } from '../protos/api'; import { ApiClient } from '../protos/api';
import { CrontabModel } from '../data/cron';
class TaskLimit { class TaskLimit {
private dependenyLimit = new PQueue({ concurrency: 1 }); private dependenyLimit = new PQueue({ concurrency: 1 });
@ -136,7 +137,6 @@ class TaskLimit {
let maxQueueSize = 10; // Default for multi-instance mode (increased from 5) let maxQueueSize = 10; // Default for multi-instance mode (increased from 5)
let isSingleInstanceMode = false; let isSingleInstanceMode = false;
try { try {
const { CrontabModel } = await import('../data/cron');
const cronRecord = await CrontabModel.findOne({ const cronRecord = await CrontabModel.findOne({
where: { id: Number(cron.id) }, where: { id: Number(cron.id) },
}); });

View File

@ -51,10 +51,16 @@ export function runCron(cmd: string, cron: ICron): Promise<number | void> {
const cp = spawn(cmd, { shell: '/bin/bash' }); const cp = spawn(cmd, { shell: '/bin/bash' });
// Update status to running after spawning the process // Update status to running after spawning the process
try {
await CrontabModel.update( await CrontabModel.update(
{ status: CrontabStatus.running, pid: cp.pid }, { status: CrontabStatus.running, pid: cp.pid },
{ where: { id: Number(cron.id) } }, { where: { id: Number(cron.id) } },
); );
} catch (error) {
Logger.error(
`[schedule][更新任务状态失败] 任务ID: ${cron.id}, 错误: ${error}`,
);
}
cp.stderr.on('data', (data) => { cp.stderr.on('data', (data) => {
Logger.info( Logger.info(
@ -74,10 +80,16 @@ export function runCron(cmd: string, cron: ICron): Promise<number | void> {
cp.on('exit', async (code) => { cp.on('exit', async (code) => {
taskLimit.removeQueuedCron(cron.id); taskLimit.removeQueuedCron(cron.id);
// Update status to idle after task completes // Update status to idle after task completes
try {
await CrontabModel.update( await CrontabModel.update(
{ status: CrontabStatus.idle, pid: undefined }, { status: CrontabStatus.idle, pid: undefined },
{ where: { id: Number(cron.id) } }, { where: { id: Number(cron.id) } },
); );
} catch (error) {
Logger.error(
`[schedule][更新任务状态失败] 任务ID: ${cron.id}, 错误: ${error}`,
);
}
Logger.info( Logger.info(
'[schedule][执行任务结束] 参数: %s, 退出码: %j', '[schedule][执行任务结束] 参数: %s, 退出码: %j',
JSON.stringify({ JSON.stringify({