File size: 10,014 Bytes
90647f1
 
 
ee71e80
90647f1
 
ee71e80
90647f1
ee71e80
90647f1
 
 
 
 
 
 
 
ee71e80
 
 
 
90647f1
dc6cd3d
 
 
 
ee71e80
 
 
 
 
 
 
 
 
 
 
 
 
 
90647f1
 
 
ee71e80
90647f1
 
 
dc6cd3d
 
 
 
ee71e80
dc6cd3d
 
90647f1
 
dc6cd3d
ee71e80
 
 
 
 
90647f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17a830e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ee71e80
 
 
 
 
 
 
 
 
 
 
 
90647f1
ee71e80
 
 
 
90647f1
ee71e80
90647f1
ee71e80
c9393b9
8b46bea
90647f1
 
 
 
ee71e80
90647f1
ee71e80
96b2e68
 
ee71e80
96b2e68
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ee71e80
 
 
 
 
8b46bea
ee71e80
90647f1
 
 
 
 
ee71e80
90647f1
c9393b9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fe06e2a
 
 
c9393b9
fe06e2a
c9393b9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90647f1
ee71e80
90647f1
c9393b9
 
 
 
 
 
 
 
 
 
9e62806
 
 
 
90647f1
9e62806
 
 
 
 
 
 
 
 
 
 
ee71e80
 
9e62806
10d10f3
ee71e80
9e62806
 
 
 
 
 
ee71e80
 
90647f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/**
 * 任务轮询框架 - Upstash REST 适配
 * 从 Redis 队列获取慢任务(非阻塞轮询)
 * ★★★ 并发处理池:支持同时处理多个任务 ★★★
 */
import { getRedis, RedisHelper } from '../lib/redis';
import { setWorkerRunning, setWorkerIdle, addActiveTask, removeActiveTask, WorkerStatus, getWorkerStatus } from './status';
import { processTask } from './processor';
import { setCurrentTaskForWatchdog, clearCurrentTaskForWatchdog, removeWatchedTask } from './index';

const QUEUE_KEYS = {
  boosted: 'slow_task:queue:boosted',
  normal: 'slow_task:queue:normal',
  results: 'slow_task:results',
};

const POLL_INTERVAL = 3000; // 3秒轮询间隔
const MAX_CONCURRENT_TASKS = 3; // ★★★ 最大并发任务数 ★★★

// ★★★ 并发计数器 ★★★
let activeTasksCount = 0;

// ★ 任务 2: 增强日志 - 轮询计数器
let pollCount = 0;
let lastLogTime = Date.now();

/**
 * 获取当前活跃任务数
 */
export function getActiveTasksCount(): number {
  return activeTasksCount;
}

/**
 * 获取最大并发数
 */
export function getMaxConcurrentTasks(): number {
  return MAX_CONCURRENT_TASKS;
}

/**
 * 轮询任务队列(非阻塞)
 * Upstash REST 不支持 brpop,改用 rpop + 定时轮询
 * ★★★ 并发模式:取出任务后立即异步处理,不阻塞轮询循环 ★★★
 */
export async function pollTaskQueue(): Promise<void> {
  const redis = getRedis();
  pollCount++;

  // ★ 每100次轮询打印一次状态(约每5分钟)
  if (pollCount % 100 === 0) {
    console.log(`[Queue] Poll #${pollCount}, active: ${activeTasksCount}/${MAX_CONCURRENT_TASKS}, elapsed: ${Math.floor((Date.now() - lastLogTime) / 1000)}s`);
    lastLogTime = Date.now();
  }

  if (!redis) {
    console.warn('[Queue] ❌ Redis not available');
    return;
  }

  // ★★★ 并发控制:如果已满,跳过本轮 ★★★
  if (activeTasksCount >= MAX_CONCURRENT_TASKS) {
    return;
  }

  // 优先取加速队列,其次普通队列
  let task: any = null;
  let queueName: string = '';

  // 先检查加速队列
  task = await RedisHelper.dequeue(QUEUE_KEYS.boosted);
  if (task) {
    queueName = QUEUE_KEYS.boosted;
  } else {
    // 再检查普通队列
    task = await RedisHelper.dequeue(QUEUE_KEYS.normal);
    if (task) {
      queueName = QUEUE_KEYS.normal;
    }
  }

  if (!task) {
    // 无任务,等待下次轮询
    return;
  }

  // ★ 诊断:保存任务原始数据到诊断 key(读取后可删除)
  try {
    const redis2 = getRedis();
    if (redis2) {
      const diagData = {
        taskId: task.taskId,
        hasChatContext: !!task.chatContext,
        chatContextLen: task.chatContext?.length || 0,
        chatContext0Role: task.chatContext?.[0]?.role || 'N/A',
        chatContext0ContentLen: task.chatContext?.[0]?.content?.length || 0,
        chatContext0ContentPreview: (task.chatContext?.[0]?.content || '').slice(0, 300),
        promptLen: task.prompt?.length || 0,
        promptPreview: (task.prompt || '').slice(0, 300),
        hasDraftContent: !!task.draftContent,
        draftContentLen: task.draftContent?.length || 0,
        title: task.title,
        taskType: task.taskType,
        skipPhase1: task.skipPhase1,
        timestamp: Date.now(),
      };
      await redis2.set('slow_task:diag:last_dequeued', JSON.stringify(diagData), 'EX', 3600);
    }
  } catch (diagErr) {
    // 忽略诊断错误
  }

  console.log(`[Queue] Got task from: ${queueName}, active slots: ${activeTasksCount}/${MAX_CONCURRENT_TASKS}`);

  // ★★★ 并发控制:立即递增计数器 ★★★
  activeTasksCount++;
  addActiveTask(task.taskId);

  // ★★★ 异步处理任务(不阻塞轮询循环)★★★
  processTaskAsync(task, queueName).catch(() => {
    // 兜底:确保计数器不会泄漏
    // processTaskAsync 内部已有完整错误处理,这里只是安全网
  });
}

/**
 * 异步处理单个任务(独立的错误边界)
 */
async function processTaskAsync(task: any, queueName: string): Promise<void> {
  try {
    console.log(`[Queue] ▶ Starting task: ${task.taskId} (type: ${task.taskType}), active: ${activeTasksCount}/${MAX_CONCURRENT_TASKS}`);

    // 设置看门狗监控任务
    setCurrentTaskForWatchdog(task.taskId, task.taskType);
    await setWorkerRunning(task.taskId);

    // 处理任务
    await processTask(task);

    console.log(`[Queue] ✅ Task completed: ${task.taskId}`);
  } catch (err: any) {
    console.error(`[Queue] ❌ Task failed: ${task.taskId}${err.message}`);
    console.error('[Queue] Error stack:', err.stack);

    // 将任务标记为失败
    try {
      const redis = getRedis();
      if (redis && task) {
        const existing = await redis.hget(QUEUE_KEYS.results, task.taskId);
        if (existing) {
          const state = typeof existing === 'string' ? JSON.parse(existing) : existing;
          const updated = {
            ...state,
            status: 'failed',
            errorMessage: err.message || 'Unknown error',
            errorStack: err.stack?.split('\n').slice(0, 3).join('\n'),
            updatedAt: Date.now(),
          };
          await redis.hset(QUEUE_KEYS.results, { [task.taskId]: JSON.stringify(updated) });
          console.log('[Queue] Task marked as failed:', task.taskId);
        } else {
          // 如果没有状态记录,创建一个
          await redis.hset(QUEUE_KEYS.results, {
            [task.taskId]: JSON.stringify({
              taskId: task.taskId,
              status: 'failed',
              errorMessage: err.message || 'Unknown error',
              updatedAt: Date.now(),
            })
          });
          console.log('[Queue] Created failed status for:', task.taskId);
        }
      }
    } catch (updateErr: any) {
      console.error('[Queue] Failed to update task status:', updateErr.message);
    }
  } finally {
    // ★★★ 无论成功失败,都递减计数器 ★★★
    activeTasksCount--;
    removeActiveTask(task.taskId);
    removeWatchedTask(task.taskId);
    await setWorkerIdle();
    console.log(`[Queue] ▶ Slot freed: ${activeTasksCount}/${MAX_CONCURRENT_TASKS} active`);
  }
}

/**
 * 启动轮询循环
 * ★★★ 并发模式:轮询不阻塞,任务异步执行 ★★★
 */
// ─── Zombie Recovery(OPT-07)───

const ZOMBIE_RECOVERY_THRESHOLD_MS = 15 * 60 * 1000; // 15 分钟

/**
 * 扫描 stuck 任务并重新入队
 * Worker 启动时恢复因崩溃/重启而丢失的任务
 */
async function recoverZombieTasks(): Promise<number> {
  const redis = getRedis();
  if (!redis) return 0;

  try {
    const allResults = await redis.hgetall(QUEUE_KEYS.results);
    if (!allResults) return 0;

    const now = Date.now();
    let recovered = 0;

    for (const [taskId, rawState] of Object.entries(allResults)) {
      const state = typeof rawState === 'string' ? JSON.parse(rawState) : rawState;
      if (!state) continue;

      if (state.status === 'completed' || state.status === 'failed') continue;
      const lastActive = state.updatedAt || state.startedAt || state.createdAt || 0;
      if (now - lastActive < ZOMBIE_RECOVERY_THRESHOLD_MS) continue;

      console.log(`[Queue] 🧟 发现 zombie 任务: ${taskId}, 停滞 ${Math.floor((now - lastActive) / 1000)}秒`);

      const recoveredState = {
        ...state,
        status: 'queued',
        progress: 0,
        currentStep: 0,
        detail: 'Zombie recovery: 任务已自动恢复',
        updatedAt: now,
      };

      const queueKey = state.boosted ? QUEUE_KEYS.boosted : QUEUE_KEYS.normal;
      await redis.lpush(queueKey, JSON.stringify(recoveredState));
      await redis.hset(QUEUE_KEYS.results, { [taskId]: JSON.stringify(recoveredState) });

      recovered++;
      console.log(`[Queue] ✅ Zombie 任务已恢复: ${taskId}`);
    }

    return recovered;
  } catch (error) {
    console.warn('[Queue] Zombie recovery failed:', error);
    return 0;
  }
}

export async function startPollingLoop(): Promise<void> {
  console.log(`[Queue] Starting concurrent polling loop, interval: ${POLL_INTERVAL}ms, max concurrent: ${MAX_CONCURRENT_TASKS}`);

  // ★ OPT-07: Worker 启动时执行 zombie recovery
  try {
    const recovered = await recoverZombieTasks();
    if (recovered > 0) {
      console.log(`[Queue] 🔄 Zombie recovery: 恢复了 ${recovered} 个任务`);
    }
  } catch (err) {
    console.warn('[Queue] Zombie recovery skipped:', err);
  }

  // ★★★ 轮询心跳:确保循环在运行 ★★★
  let loopCount = 0;
  const LOOP_HEARTBEAT_KEY = 'slow_task:poll_heartbeat';

  while (true) {
    loopCount++;

    // 每10次轮询更新心跳(约30秒)
    if (loopCount % 10 === 0) {
      const redis = getRedis();
      if (redis) {
        try {
          const status = getWorkerStatus();
          await redis.set(LOOP_HEARTBEAT_KEY, JSON.stringify({
            loopCount,
            timestamp: Date.now(),
            activeTasks: activeTasksCount,
            maxConcurrent: MAX_CONCURRENT_TASKS,
            processedCount: status.processedCount
          }), 'EX', 300);
          console.log(`[Queue] 💓 Poll heartbeat: loop #${loopCount}, active: ${activeTasksCount}/${MAX_CONCURRENT_TASKS}`);
        } catch (err) {
          console.warn('[Queue] Heartbeat write failed');
        }
      }
    }

    // ★★★ 关键变更:不 await,让任务异步执行 ★★★
    // pollTaskQueue 内部有并发控制,满了就跳过
    await pollTaskQueue();

    // 等待下次轮询
    await new Promise(resolve => setTimeout(resolve, POLL_INTERVAL));
  }
}

/**
 * 获取队列状态
 */
export async function getQueueStatus(): Promise<{
  boosted: number;
  normal: number;
}> {
  const redis = getRedis();

  if (!redis) {
    return { boosted: 0, normal: 0 };
  }

  return {
    boosted: await RedisHelper.queueLength(QUEUE_KEYS.boosted),
    normal: await RedisHelper.queueLength(QUEUE_KEYS.normal),
  };
}