fix: prevent duplicate concurrent executions of the same scheduled task

executeTask() lacked a runningTasks guard, so manual "Run Now" or
server restarts could spawn a second subprocess while one was already
running. Now checks runningTasks at entry and skips if already in flight.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
程序员阿江(Relakkes) 2026-04-08 20:09:32 +08:00
parent d9905c78fd
commit 5fe7542160

View File

@ -314,6 +314,22 @@ export class CronScheduler {
* @param options.createSession When true, creates a Session for rich output viewing (used for manual "Run Now")
*/
async executeTask(task: CronTask, options?: { createSession?: boolean }): Promise<TaskRun> {
// Prevent concurrent executions of the same task
const existing = this.runningTasks.get(task.id)
if (existing) {
console.log(
`[CronScheduler] Task ${task.id} is already running (runId=${existing.runId}), skipping`,
)
return {
id: existing.runId,
taskId: task.id,
taskName: task.name || task.prompt.slice(0, 60),
startedAt: new Date(existing.startedAt).toISOString(),
status: 'running',
prompt: task.prompt,
}
}
const runId = crypto.randomBytes(6).toString('hex')
const startedAt = new Date().toISOString()
const workDir = task.folderPath || os.homedir()