38 lines
1.3 KiB
JavaScript
38 lines
1.3 KiB
JavaScript
import { Router } from 'express'
|
|
import redis from '../redis/index.js'
|
|
import initQueue from '../redis/initQueue.js'
|
|
const router = Router()
|
|
|
|
router.post('/all', async (req, res) => {
|
|
res.status(200).json({ success: true, message: 'Received' });
|
|
processCallbackData(req.body).catch(error => {
|
|
console.error('[callback] 处理回调数据出错:', error);
|
|
});
|
|
})
|
|
|
|
async function processCallbackData(body) {
|
|
const { taskId: remoteTaskId, eventData } = body;
|
|
|
|
if (!remoteTaskId) {
|
|
console.error('[callback] 回调数据缺少 taskId');
|
|
return;
|
|
}
|
|
|
|
const taskId = await redis.get(`${initQueue.callback}:${remoteTaskId}`);
|
|
|
|
if (taskId) {
|
|
const taskKey = `${initQueue.prefix}:task:${taskId}`;
|
|
await redis.hSet(taskKey, 'resultData', eventData);
|
|
await redis.rPush(initQueue.callback, taskId);
|
|
await initQueue.addCallbackRQtasks(1);
|
|
await initQueue.removeCallbackPendingTask(remoteTaskId);
|
|
|
|
console.log(`[callback] 回调处理成功: taskId=${taskId}, externalTaskId=${remoteTaskId}`);
|
|
} else {
|
|
console.error(`[callback] 未找到任务映射: externalTaskId=${remoteTaskId}`);
|
|
await redis.set(`callback:missing:${remoteTaskId}`, JSON.stringify(body), { EX: 86400 });
|
|
}
|
|
}
|
|
|
|
export default router
|