From f043aa62e4d39f28c31a4c29dbc5f5921d92b34e Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 22 Dec 2025 14:42:44 +0000 Subject: [PATCH] Fix task status reset issue - preserve running tasks on restart Co-authored-by: whyour <22700758+whyour@users.noreply.github.com> --- back/config/util.ts | 10 ++++++++++ back/loaders/initData.ts | 17 +++++++++++++++-- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/back/config/util.ts b/back/config/util.ts index 7d61f74f..e80ab284 100644 --- a/back/config/util.ts +++ b/back/config/util.ts @@ -397,6 +397,16 @@ export function psTree(pid: number): Promise { }); } +export function isPidRunning(pid: number): boolean { + try { + // Signal 0 doesn't kill the process, just checks if it exists + process.kill(pid, 0); + return true; + } catch (error) { + return false; + } +} + export async function killTask(pid: number) { const pids = await psTree(pid); diff --git a/back/loaders/initData.ts b/back/loaders/initData.ts index f8b5315a..06968765 100644 --- a/back/loaders/initData.ts +++ b/back/loaders/initData.ts @@ -13,7 +13,7 @@ import { AuthDataType, SystemModel } from '../data/system'; import SystemService from '../services/system'; import UserService from '../services/user'; import { writeFile, readFile } from 'fs/promises'; -import { createRandomString, fileExist, safeJSONParse } from '../config/util'; +import { createRandomString, fileExist, safeJSONParse, isPidRunning } from '../config/util'; import OpenService from '../services/open'; import { shareStore } from '../shared/store'; import Logger from './logger'; @@ -132,7 +132,20 @@ export default async () => { }); // 初始化更新所有任务状态为空闲 - await CrontabModel.update({ status: CrontabStatus.idle }, { where: {} }); + // 但保留仍在运行的任务的状态 + const allCrons = await CrontabModel.findAll({ raw: true }); + for (const cron of allCrons) { + // 如果任务有 PID 且进程仍在运行,则保持其状态 + if (cron.pid && isPidRunning(cron.pid)) { + // 保留当前状态(running 或 queued) + continue; + } + // 否则将状态重置为 idle,并清除 PID + await CrontabModel.update( + { status: CrontabStatus.idle, pid: undefined }, + { where: { id: cron.id } } + ); + } // 初始化时执行一次所有的 ql repo 任务 CrontabModel.findAll({