Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 30, 2025

Scheduled tasks in multi-instance mode stopped executing after the queue filled up (5 tasks). Single-instance mode failed to kill old tasks before starting new ones due to missing status tracking.

Root Causes

Multi-instance mode: Fixed queue limit of 5 caused task rejection when long-running tasks accumulated (e.g., 500s tasks scheduled every minute)

Single-instance mode:

  • Status never updated to running, preventing detection of active tasks
  • Default behavior treated null/undefined as multi-instance instead of single-instance

Changes

back/shared/runCron.ts

  • Add status updates: running on spawn, idle on exit (with error handling)
  • Fix default: allow_multiple_instances !== 1 treats null/undefined as single-instance
  • Enable proper task killing in single-instance mode via status tracking

back/shared/pLimit.ts

  • Query allow_multiple_instances per task to determine queue limits
  • Single-instance: limit 2 (allows queuing new task while killing old)
  • Multi-instance: limit 10 (increased from 5)
  • Import CrontabModel at module level to avoid repeated dynamic imports
// Before: null/undefined incorrectly allowed multiple instances
const allowSingleInstances = existingCron?.allow_multiple_instances === 0;

// After: null/undefined defaults to single-instance mode
const isSingleInstanceMode = existingCron?.allow_multiple_instances !== 1;
Original prompt

This section details on the original issue you should resolve

<issue_title>定时任务-单/多实例模式未触发</issue_title>
<issue_description>### Qinglong version

2.20.1

Steps to reproduce

作者你好,我在使用的时候发现了bug,根据实例模式介绍,单实例模式启动新任务会停止旧任务;多实例模式则会同时运行

我写了以下代码:
`
import random
import time
from datetime import datetime

now = datetime.now()

name=(random.randint(1, 100)) # 生成1-100的随机整数

print(str(now)+'1开始运行:'+str(name))

time.sleep(500)

print(str(now)+'3运行结束:'+str(name))
`

定时规则:*/1 * * * *
实例模式:多实例

通过观察日志:

开始执行... 2025-12-30 17:23:01

2025-12-30 17:23:01.4253411开始运行:8
2025-12-30 17:23:01.4253413运行结束:8

执行结束... 2025-12-30 17:31:21 耗时 500 秒  


开始执行... 2025-12-30 17:27:01

2025-12-30 17:27:01.3780611开始运行:40
2025-12-30 17:27:01.3780613运行结束:40

执行结束... 2025-12-30 17:35:21 耗时 500 秒     

   

预期是:

每分钟执行一次,在多实例的模式下,应该会每分钟都有日志产生,都开始运行,然后每个任务独立完成,之前设置的time.sleep(80)前几分钟正常到后面日志只有 17:58、17:59、18:01中间的18:00没有记录 为了更好的观察 我改成了500s 于是这个差距拉开得更加明显,后续由于内存不够吧报错了,然后有按每分钟一次运行

日志我打包好了:https://wwbwu.lanzoue.com/iJoDN3eujoxe

单实例模式我也测试过 到点后程序运行但是不会停止上次的任务
   

What is expected?

1

What is actually happening?

1

System Info

Any additional comments?

</issue_description>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@vercel
Copy link

vercel bot commented Dec 30, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Review Updated (UTC)
qinglong Ready Ready Preview, Comment Dec 30, 2025 4:46pm
qinglong-back Ready Ready Preview, Comment Dec 30, 2025 4:46pm

Copilot AI and others added 2 commits December 30, 2025 16:41
- 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 <[email protected]>
- 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 <[email protected]>
Copilot AI changed the title [WIP] Fix scheduled task not triggering in multi-instance mode Fix scheduled task instance mode execution and queue management Dec 30, 2025
Copilot AI requested a review from whyour December 30, 2025 16:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

定时任务-单/多实例模式未触发

2 participants