123 lines
3.8 KiB
TypeScript
123 lines
3.8 KiB
TypeScript
/**
|
||
* 检查任务状态脚本
|
||
* 用于诊断卡住的任务:查看任务状态、进度、创建时间等
|
||
*
|
||
* 使用方法:
|
||
* npx ts-node scripts/check-task-status.ts [taskId]
|
||
* 如果不提供 taskId,则显示所有进行中的任务
|
||
*/
|
||
|
||
import { PrismaClient } from '@prisma/client';
|
||
import dotenv from 'dotenv';
|
||
|
||
dotenv.config();
|
||
|
||
const prisma = new PrismaClient();
|
||
|
||
async function checkTaskStatus(taskId?: string) {
|
||
try {
|
||
if (taskId) {
|
||
// 查询指定任务
|
||
const task = await prisma.courseGenerationTask.findUnique({
|
||
where: { id: taskId },
|
||
include: {
|
||
course: {
|
||
select: {
|
||
id: true,
|
||
title: true,
|
||
status: true,
|
||
},
|
||
},
|
||
user: {
|
||
select: {
|
||
id: true,
|
||
phone: true,
|
||
nickname: true,
|
||
},
|
||
},
|
||
},
|
||
});
|
||
|
||
if (!task) {
|
||
console.log(`❌ 任务不存在: ${taskId}`);
|
||
return;
|
||
}
|
||
|
||
console.log('\n=== 任务详情 ===');
|
||
console.log(`任务ID: ${task.id}`);
|
||
console.log(`课程ID: ${task.courseId}`);
|
||
console.log(`课程标题: ${task.course.title}`);
|
||
console.log(`用户ID: ${task.userId}`);
|
||
console.log(`用户: ${task.user.nickname || task.user.phone || 'N/A'}`);
|
||
console.log(`状态: ${task.status}`);
|
||
console.log(`进度: ${task.progress}%`);
|
||
console.log(`当前步骤: ${task.currentStep || 'N/A'}`);
|
||
console.log(`错误信息: ${task.errorMessage || 'N/A'}`);
|
||
console.log(`来源类型: ${task.sourceType || 'N/A'}`);
|
||
console.log(`导师类型: ${task.persona || 'N/A'}`);
|
||
console.log(`创建时间: ${task.createdAt}`);
|
||
console.log(`更新时间: ${task.updatedAt}`);
|
||
console.log(`运行时长: ${Math.floor((Date.now() - task.createdAt.getTime()) / 1000 / 60)} 分钟`);
|
||
|
||
if (task.status !== 'completed' && task.status !== 'failed') {
|
||
const minutesSinceUpdate = Math.floor((Date.now() - task.updatedAt.getTime()) / 1000 / 60);
|
||
if (minutesSinceUpdate > 5) {
|
||
console.log(`⚠️ 警告: 任务已 ${minutesSinceUpdate} 分钟未更新,可能已卡住`);
|
||
}
|
||
}
|
||
} else {
|
||
// 查询所有进行中的任务
|
||
const tasks = await prisma.courseGenerationTask.findMany({
|
||
where: {
|
||
status: {
|
||
notIn: ['completed', 'failed'],
|
||
},
|
||
},
|
||
include: {
|
||
course: {
|
||
select: {
|
||
id: true,
|
||
title: true,
|
||
},
|
||
},
|
||
},
|
||
orderBy: {
|
||
createdAt: 'desc',
|
||
},
|
||
});
|
||
|
||
if (tasks.length === 0) {
|
||
console.log('✅ 没有进行中的任务');
|
||
return;
|
||
}
|
||
|
||
console.log(`\n=== 进行中的任务 (共 ${tasks.length} 个) ===\n`);
|
||
|
||
for (const task of tasks) {
|
||
const minutesSinceUpdate = Math.floor((Date.now() - task.updatedAt.getTime()) / 1000 / 60);
|
||
const minutesSinceCreate = Math.floor((Date.now() - task.createdAt.getTime()) / 1000 / 60);
|
||
|
||
console.log(`任务ID: ${task.id}`);
|
||
console.log(`课程: ${task.course.title} (${task.courseId})`);
|
||
console.log(`状态: ${task.status} | 进度: ${task.progress}%`);
|
||
console.log(`创建时间: ${task.createdAt} (${minutesSinceCreate} 分钟前)`);
|
||
console.log(`更新时间: ${task.updatedAt} (${minutesSinceUpdate} 分钟前)`);
|
||
|
||
if (minutesSinceUpdate > 5) {
|
||
console.log(`⚠️ 警告: 已 ${minutesSinceUpdate} 分钟未更新,可能已卡住`);
|
||
}
|
||
console.log('---');
|
||
}
|
||
}
|
||
} catch (error: any) {
|
||
console.error('❌ 查询失败:', error.message);
|
||
} finally {
|
||
await prisma.$disconnect();
|
||
}
|
||
}
|
||
|
||
// 从命令行参数获取 taskId
|
||
const taskId = process.argv[2];
|
||
|
||
checkTaskStatus(taskId);
|