103 lines
3.5 KiB
JavaScript
103 lines
3.5 KiB
JavaScript
// clearDigitalHumanData.js
|
||
import redis from './redis/index.js';
|
||
import initQueue from './redis/initQueue.js';
|
||
|
||
async function clearDigitalHumanData() {
|
||
try {
|
||
console.log('开始清除数字人相关数据...');
|
||
|
||
// 1. 清除等待队列
|
||
await redis.del('digitalHuman:runninghub:wait');
|
||
await redis.del('digitalHuman:coze:wait');
|
||
console.log('已清除等待队列');
|
||
|
||
// 2. 清除所有数字人相关的任务数据
|
||
let cursor = '0';
|
||
do {
|
||
// 调用scan并打印返回结果(便于调试)
|
||
const result = await redis.scan(cursor, {
|
||
MATCH: `${initQueue.prefix}:task:*`,
|
||
COUNT: 100
|
||
});
|
||
// 【核心修复】从scan返回的对象中正确提取cursor和keys
|
||
const newCursor = result.cursor; // 取cursor属性
|
||
const keys = result.keys || []; // 取keys属性,兜底为空数组
|
||
|
||
console.log(`当前游标: ${newCursor}, 找到keys数量: ${keys.length}`); // 调试日志
|
||
|
||
if (keys.length > 0) {
|
||
// 加强过滤,确保只保留有效字符串key
|
||
const validKeys = keys.filter(key => {
|
||
return typeof key === 'string' && key.trim() !== '';
|
||
});
|
||
|
||
if (validKeys.length > 0) {
|
||
await redis.del(...validKeys);
|
||
console.log(`已清除 ${validKeys.length} 个任务数据`);
|
||
}
|
||
}
|
||
|
||
cursor = newCursor;
|
||
} while (cursor !== '0');
|
||
|
||
// 3. 清除轮询队列数据
|
||
cursor = '0';
|
||
do {
|
||
const result = await redis.scan(cursor, {
|
||
MATCH: `${initQueue.prefix}:processPolling:*`,
|
||
COUNT: 100
|
||
});
|
||
// 同样修复解构问题
|
||
const newCursor = result.cursor;
|
||
const keys = result.keys || [];
|
||
|
||
console.log(`当前游标: ${newCursor}, 找到轮询keys数量: ${keys.length}`);
|
||
|
||
if (keys.length > 0) {
|
||
const validKeys = keys.filter(key => {
|
||
return typeof key === 'string' && key.trim() !== '';
|
||
});
|
||
|
||
if (validKeys.length > 0) {
|
||
await redis.del(...validKeys);
|
||
console.log(`已清除 ${validKeys.length} 个轮询队列数据`);
|
||
}
|
||
}
|
||
|
||
cursor = newCursor;
|
||
} while (cursor !== '0');
|
||
|
||
// 4. 清除结果队列数据
|
||
await redis.del(initQueue.resultName);
|
||
await redis.del(initQueue.resultList);
|
||
console.log('已清除结果队列数据');
|
||
|
||
// 5. 清除错误队列数据
|
||
await redis.del(initQueue.errorName);
|
||
await redis.del(initQueue.errorList);
|
||
console.log('已清除错误队列数据');
|
||
|
||
// 6. 清除回调队列数据
|
||
await redis.del(initQueue.callback);
|
||
console.log('已清除回调队列数据');
|
||
|
||
// 7. 重置平台信息中的数字人相关计数
|
||
await redis.json.set(initQueue.initInfoKey, '$.platforms.digitalHuman:runninghub.WQtasks', '0');
|
||
await redis.json.set(initQueue.initInfoKey, '$.platforms.digitalHuman:runninghub.PQtasks', '0');
|
||
await redis.json.set(initQueue.initInfoKey, '$.platforms.digitalHuman:coze.WQtasks', '0');
|
||
await redis.json.set(initQueue.initInfoKey, '$.platforms.digitalHuman:coze.PQtasks', '0');
|
||
console.log('已重置平台计数');
|
||
|
||
console.log('数字人相关数据清除完成!');
|
||
} catch (error) {
|
||
console.error('清除数据时出错:', error);
|
||
process.exit(1); // 异常退出进程
|
||
} finally {
|
||
if (redis.isOpen) {
|
||
await redis.disconnect();
|
||
console.log('Redis 连接已正常关闭');
|
||
}
|
||
}
|
||
}
|
||
|
||
clearDigitalHumanData(); |