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
docker-compose.*.yml
data_nedb/
yarn.lock

View File

@ -190,14 +190,14 @@ export class GenerationTask {
static async getTimeoutTasks(): Promise<IGenerationTask[]> {
const currentTime = Math.floor(Date.now() / 1000);
return await this.find({
status: { $in: ['processing', 'polling'] },
$expr: {
$lt: [
{ $add: ['$created_at', '$task_timeout'] },
currentTime
]
}
// NeDB 不支持 $expr先查询所有活跃任务然后在内存中过滤超时任务
const activeTasks = await this.find({
status: { $in: ['processing', 'polling'] }
});
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> {
try {
return await GenerationTask.countDocuments({
const tasks = await GenerationTask.find({
server_id: serverId,
status: { $in: ['processing', 'polling'] }
});
return tasks.length;
} catch (error) {
logger.error(`Failed to get server load for ${serverId}:`, error);
return 0;
@ -301,17 +302,19 @@ export class DatabaseGenerationService {
async cancelTask(taskId: string): Promise<boolean> {
try {
const result = await GenerationTask.updateOne(
{
task_id: taskId,
status: { $in: ['pending', 'processing', 'polling'] }
},
{
{
task_id: taskId,
status: { $in: ['pending', 'processing', 'polling'] }
},
{
$set: {
status: 'failed',
error_message: 'Task cancelled by user',
completed_at: Math.floor(Date.now() / 1000),
updated_at: Math.floor(Date.now() / 1000)
}
);
}
);
const cancelled = result > 0;

View File

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