Fix scheduled task instance mode issues

- Fixed runCron to properly handle allow_multiple_instances setting
- Updated default behavior to single-instance mode for backward compatibility
- Added status updates (running/idle) to track task execution
- Fixed queue limit logic to respect instance mode settings
- For single-instance mode: allow up to 2 queued tasks to handle task killing
- For multi-instance mode: increased queue limit to 10 tasks

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

View File

@ -131,13 +131,39 @@ class TaskLimit {
let runs = this.queuedCrons.get(cron.id); let runs = this.queuedCrons.get(cron.id);
const result = runs?.length ? [...runs, fn] : [fn]; const result = runs?.length ? [...runs, fn] : [fn];
const repeatTimes = this.repeatCronNotifyMap.get(cron.id) || 0; const repeatTimes = this.repeatCronNotifyMap.get(cron.id) || 0;
if (result?.length > 5) {
// Check instance mode from database to determine queue limit
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) },
});
// Default to single instance mode (0) for backward compatibility
// allow_multiple_instances is 1 for multi-instance, 0 or null/undefined for single instance
isSingleInstanceMode = cronRecord?.allow_multiple_instances !== 1;
if (isSingleInstanceMode) {
// For single instance mode, allow up to 2 queued tasks
// This allows the new task to be queued while the old one is being killed
maxQueueSize = 2;
}
} catch (error) {
Logger.error(
`[schedule][检查实例模式失败] 任务ID: ${cron.id}, 错误: ${error}`,
);
}
if (result?.length > maxQueueSize) {
if (repeatTimes < 3) { if (repeatTimes < 3) {
this.repeatCronNotifyMap.set(cron.id, repeatTimes + 1); this.repeatCronNotifyMap.set(cron.id, repeatTimes + 1);
const modeStr = isSingleInstanceMode ? '单实例' : '多实例';
this.client.systemNotify( this.client.systemNotify(
{ {
title: '任务重复运行', title: '任务重复运行',
content: `任务:${cron.name},命令:${cron.command},定时:${cron.schedule},处于运行中的超过 5 个,请检查定时设置`, content: `任务:${cron.name}${modeStr}模式),命令:${cron.command},定时:${cron.schedule},处于运行中的超过 ${maxQueueSize} 个,请检查定时设置`,
}, },
(err, res) => { (err, res) => {
if (err) { if (err) {

View File

@ -15,11 +15,12 @@ export function runCron(cmd: string, cron: ICron): Promise<number | void> {
}); });
// Default to single instance mode (0) for backward compatibility // Default to single instance mode (0) for backward compatibility
const allowSingleInstances = // allow_multiple_instances is 1 for multi-instance, 0 or null/undefined for single instance
existingCron?.allow_multiple_instances === 0; const isSingleInstanceMode =
existingCron?.allow_multiple_instances !== 1;
if ( if (
allowSingleInstances && isSingleInstanceMode &&
existingCron && existingCron &&
existingCron.pid && existingCron.pid &&
(existingCron.status === CrontabStatus.running || (existingCron.status === CrontabStatus.running ||
@ -49,6 +50,12 @@ 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
await CrontabModel.update(
{ status: CrontabStatus.running, pid: cp.pid },
{ where: { id: Number(cron.id) } },
);
cp.stderr.on('data', (data) => { cp.stderr.on('data', (data) => {
Logger.info( Logger.info(
'[schedule][执行任务失败] 命令: %s, 错误信息: %j', '[schedule][执行任务失败] 命令: %s, 错误信息: %j',
@ -66,6 +73,11 @@ 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
await CrontabModel.update(
{ status: CrontabStatus.idle, pid: undefined },
{ where: { id: Number(cron.id) } },
);
Logger.info( Logger.info(
'[schedule][执行任务结束] 参数: %s, 退出码: %j', '[schedule][执行任务结束] 参数: %s, 退出码: %j',
JSON.stringify({ JSON.stringify({