mirror of
https://github.com/whyour/qinglong.git
synced 2025-12-23 15:50:07 +08:00
Optimize PID validation and use bulk database update
Co-authored-by: whyour <22700758+whyour@users.noreply.github.com>
This commit is contained in:
parent
f043aa62e4
commit
1124040eeb
|
|
@ -398,6 +398,11 @@ export function psTree(pid: number): Promise<number[]> {
|
||||||
}
|
}
|
||||||
|
|
||||||
export function isPidRunning(pid: number): boolean {
|
export function isPidRunning(pid: number): boolean {
|
||||||
|
// Validate PID is a positive integer
|
||||||
|
if (!pid || pid <= 0 || !Number.isInteger(pid)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Signal 0 doesn't kill the process, just checks if it exists
|
// Signal 0 doesn't kill the process, just checks if it exists
|
||||||
process.kill(pid, 0);
|
process.kill(pid, 0);
|
||||||
|
|
|
||||||
|
|
@ -134,16 +134,23 @@ export default async () => {
|
||||||
// 初始化更新所有任务状态为空闲
|
// 初始化更新所有任务状态为空闲
|
||||||
// 但保留仍在运行的任务的状态
|
// 但保留仍在运行的任务的状态
|
||||||
const allCrons = await CrontabModel.findAll({ raw: true });
|
const allCrons = await CrontabModel.findAll({ raw: true });
|
||||||
|
const idsToReset: number[] = [];
|
||||||
|
|
||||||
for (const cron of allCrons) {
|
for (const cron of allCrons) {
|
||||||
// 如果任务有 PID 且进程仍在运行,则保持其状态
|
// 如果任务有 PID 且进程仍在运行,则保持其状态
|
||||||
if (cron.pid && isPidRunning(cron.pid)) {
|
if (cron.pid && isPidRunning(cron.pid)) {
|
||||||
// 保留当前状态(running 或 queued)
|
// 保留当前状态(running 或 queued)
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
// 否则将状态重置为 idle,并清除 PID
|
// 收集需要重置的任务 ID
|
||||||
|
idsToReset.push(cron.id!);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 批量更新所有需要重置的任务
|
||||||
|
if (idsToReset.length > 0) {
|
||||||
await CrontabModel.update(
|
await CrontabModel.update(
|
||||||
{ status: CrontabStatus.idle, pid: undefined },
|
{ status: CrontabStatus.idle, pid: undefined },
|
||||||
{ where: { id: cron.id } }
|
{ where: { id: { [Op.in]: idsToReset } } }
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user