This commit is contained in:
jonathang4 2025-08-28 18:09:59 +08:00
parent 3c9625add6
commit 99fb1876f5
5 changed files with 65 additions and 1975 deletions

1
.gitignore vendored
View File

@ -7,3 +7,4 @@ package-lock.json
ecosystem.config.*.json ecosystem.config.*.json
docker-compose.*.yml docker-compose.*.yml
data_nedb/ data_nedb/
yarn.lock

View File

@ -190,14 +190,14 @@ export class GenerationTask {
static async getTimeoutTasks(): Promise<IGenerationTask[]> { static async getTimeoutTasks(): Promise<IGenerationTask[]> {
const currentTime = Math.floor(Date.now() / 1000); const currentTime = Math.floor(Date.now() / 1000);
return await this.find({ // NeDB 不支持 $expr先查询所有活跃任务然后在内存中过滤超时任务
status: { $in: ['processing', 'polling'] }, const activeTasks = await this.find({
$expr: { status: { $in: ['processing', 'polling'] }
$lt: [ });
{ $add: ['$created_at', '$task_timeout'] },
currentTime return activeTasks.filter(task => {
] const timeoutAt = task.created_at + task.task_timeout;
} return timeoutAt < currentTime;
}); });
} }
} }

View File

@ -273,10 +273,11 @@ export class DatabaseGenerationService {
*/ */
async getServerLoad(serverId: string): Promise<number> { async getServerLoad(serverId: string): Promise<number> {
try { try {
return await GenerationTask.countDocuments({ const tasks = await GenerationTask.find({
server_id: serverId, server_id: serverId,
status: { $in: ['processing', 'polling'] } status: { $in: ['processing', 'polling'] }
}); });
return tasks.length;
} catch (error) { } catch (error) {
logger.error(`Failed to get server load for ${serverId}:`, error); logger.error(`Failed to get server load for ${serverId}:`, error);
return 0; return 0;
@ -306,11 +307,13 @@ export class DatabaseGenerationService {
status: { $in: ['pending', 'processing', 'polling'] } status: { $in: ['pending', 'processing', 'polling'] }
}, },
{ {
$set: {
status: 'failed', status: 'failed',
error_message: 'Task cancelled by user', error_message: 'Task cancelled by user',
completed_at: Math.floor(Date.now() / 1000), completed_at: Math.floor(Date.now() / 1000),
updated_at: Math.floor(Date.now() / 1000) updated_at: Math.floor(Date.now() / 1000)
} }
}
); );
const cancelled = result > 0; const cancelled = result > 0;

View File

@ -211,10 +211,12 @@ export class TaskPollingService {
const updateResult = await GenerationTask.updateOne( const updateResult = await GenerationTask.updateOne(
{ task_id: task.task_id, status: 'pending' }, { task_id: task.task_id, status: 'pending' },
{ {
$set: {
status: 'processing', status: 'processing',
started_at: currentTime, started_at: currentTime,
updated_at: currentTime updated_at: currentTime
} }
}
); );
if (updateResult === 0) { if (updateResult === 0) {
@ -236,11 +238,13 @@ export class TaskPollingService {
await GenerationTask.updateOne( await GenerationTask.updateOne(
{ task_id: task.task_id }, { task_id: task.task_id },
{ {
$set: {
status: 'polling', status: 'polling',
'internal_params.history_id': historyId, 'internal_params.history_id': historyId,
next_poll_at: currentTime + task.poll_interval, next_poll_at: currentTime + task.poll_interval,
updated_at: currentTime updated_at: currentTime
} }
}
); );
taskLog(`Task ${task.task_id} started successfully, history_id: ${historyId}`); taskLog(`Task ${task.task_id} started successfully, history_id: ${historyId}`);
@ -279,9 +283,11 @@ export class TaskPollingService {
await GenerationTask.updateOne( await GenerationTask.updateOne(
{ task_id: task.task_id }, { task_id: task.task_id },
{ {
$set: {
next_poll_at: currentTime + task.poll_interval, next_poll_at: currentTime + task.poll_interval,
updated_at: currentTime updated_at: currentTime
} }
}
); );
return; return;
} }
@ -293,9 +299,11 @@ export class TaskPollingService {
await GenerationTask.updateOne( await GenerationTask.updateOne(
{ task_id: task.task_id }, { task_id: task.task_id },
{ {
$set: {
next_poll_at: currentTime + task.poll_interval, next_poll_at: currentTime + task.poll_interval,
updated_at: currentTime updated_at: currentTime
} }
}
); );
} else if (status === 10 || (status !== 30 && item_list && item_list.length > 0)) { } else if (status === 10 || (status !== 30 && item_list && item_list.length > 0)) {
// 生成完成 // 生成完成
@ -368,10 +376,12 @@ export class TaskPollingService {
await GenerationTask.updateOne( await GenerationTask.updateOne(
{ task_id: task.task_id }, { task_id: task.task_id },
{ {
$set: {
status: 'completed', status: 'completed',
completed_at: currentTime, completed_at: currentTime,
updated_at: currentTime updated_at: currentTime
} }
}
); );
taskLog(`Task ${task.task_id} completed successfully`); taskLog(`Task ${task.task_id} completed successfully`);
@ -414,12 +424,14 @@ export class TaskPollingService {
await GenerationTask.updateOne( await GenerationTask.updateOne(
{ task_id: task.task_id }, { task_id: task.task_id },
{ {
$set: {
status: 'failed', status: 'failed',
error_message: errorMessage, error_message: errorMessage,
fail_code: failCode, fail_code: failCode,
completed_at: now, completed_at: now,
updated_at: now updated_at: now
} }
}
); );
taskLog(`Task ${task.task_id} failed: ${errorMessage}`); taskLog(`Task ${task.task_id} failed: ${errorMessage}`);
@ -444,10 +456,12 @@ export class TaskPollingService {
await GenerationTask.updateOne( await GenerationTask.updateOne(
{ task_id: task.task_id }, { task_id: task.task_id },
{ {
$set: {
retry_count: newRetryCount, retry_count: newRetryCount,
next_poll_at: currentTime + nextPollDelay, next_poll_at: currentTime + nextPollDelay,
updated_at: currentTime updated_at: currentTime
} }
}
); );
} }
} }
@ -883,15 +897,15 @@ export class DatabaseCleanupService {
static async cleanupTimeoutTasks(): Promise<number> { static async cleanupTimeoutTasks(): Promise<number> {
const currentTime = Math.floor(Date.now() / 1000); const currentTime = Math.floor(Date.now() / 1000);
// 查找超时的任务并标记为失败 // 查找所有正在处理或轮询的任务
const timeoutTasks = await GenerationTask.find({ const activeTasks = await GenerationTask.find({
status: { $in: ['processing', 'polling'] }, status: { $in: ['processing', 'polling'] }
$expr: { });
$gt: [
{ $subtract: [currentTime, '$started_at'] }, // 在内存中过滤超时任务NeDB 不支持 $expr
'$task_timeout' const timeoutTasks = activeTasks.filter(task => {
] const elapsedTime = currentTime - (task.started_at || task.created_at);
} return elapsedTime > (task.task_timeout || 3600); // 默认1小时超时
}); });
// 批量更新超时任务为失败状态 // 批量更新超时任务为失败状态

1928
yarn.lock

File diff suppressed because it is too large Load Diff