117 lines
3.5 KiB
JavaScript
117 lines
3.5 KiB
JavaScript
import dotenv from 'dotenv';
|
||
import { createClient } from 'redis';
|
||
import initQueue from './redis/initQueue.js';
|
||
|
||
dotenv.config();
|
||
|
||
const prefix = process.env.PROJECT_PREFIX || 'default';
|
||
const initInfoKey = `${prefix}:InitInfo`;
|
||
|
||
const redis = createClient({
|
||
url: process.env.REDIS_URL || 'redis://localhost:6379'
|
||
});
|
||
|
||
async function clearAllProjectData() {
|
||
try {
|
||
console.log('正在连接Redis...');
|
||
console.log('REDIS_URL:', process.env.REDIS_URL);
|
||
|
||
await redis.connect();
|
||
console.log('Redis连接成功');
|
||
|
||
console.log(`\n开始清除项目 "${prefix}" 的所有Redis数据...`);
|
||
|
||
// 1. 获取并显示当前初始化信息
|
||
try {
|
||
const initInfoResult = await redis.json.get(initInfoKey, { path: '$' });
|
||
if (initInfoResult) {
|
||
console.log('\n当前初始化信息:', JSON.stringify(initInfoResult, null, 2));
|
||
} else {
|
||
console.log('\n未找到初始化信息');
|
||
}
|
||
} catch (error) {
|
||
console.log('获取初始化信息失败(可能不存在):', error.message);
|
||
}
|
||
|
||
// 2. 删除所有相关的Redis键
|
||
const keysToDelete = [];
|
||
|
||
// 使用SCAN模式删除所有相关键
|
||
const patterns = [
|
||
`${prefix}:*`, // 所有项目前缀的键
|
||
`${initQueue?.prefix || prefix}:*`, // 兼容initQueue的前缀
|
||
];
|
||
|
||
for (const pattern of patterns) {
|
||
console.log(`\n正在搜索模式: ${pattern}`);
|
||
let cursor = '0';
|
||
let totalDeleted = 0;
|
||
|
||
do {
|
||
try {
|
||
const result = await redis.scan(cursor, {
|
||
MATCH: pattern,
|
||
COUNT: 100
|
||
});
|
||
|
||
const newCursor = result.cursor;
|
||
const keys = result.keys || [];
|
||
|
||
console.log(` 游标: ${newCursor}, 找到键数量: ${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);
|
||
totalDeleted += validKeys.length;
|
||
console.log(` 已删除 ${validKeys.length} 个键`);
|
||
}
|
||
}
|
||
|
||
cursor = newCursor;
|
||
} catch (error) {
|
||
console.error(` 搜索过程中出错:`, error.message);
|
||
break;
|
||
}
|
||
} while (cursor !== '0');
|
||
|
||
console.log(`模式 "${pattern}" 共删除 ${totalDeleted} 个键`);
|
||
}
|
||
|
||
// 3. 特殊处理:删除等待队列(可能不包含前缀)
|
||
try {
|
||
const platforms = await redis.json.get(initInfoKey, { path: '$.platforms' });
|
||
if (platforms && platforms[0]) {
|
||
for (const [key, platform] of Object.entries(platforms[0])) {
|
||
if (platform.waitQueue) {
|
||
console.log(`\n删除等待队列: ${platform.waitQueue}`);
|
||
await redis.del(platform.waitQueue);
|
||
}
|
||
}
|
||
}
|
||
} catch (error) {
|
||
console.log('删除等待队列失败:', error.message);
|
||
}
|
||
|
||
console.log('\n========================================');
|
||
console.log('项目所有Redis数据已清除完成!');
|
||
console.log('========================================');
|
||
|
||
} catch (error) {
|
||
console.error('清除Redis数据失败:', error);
|
||
} finally {
|
||
// 断开Redis连接
|
||
if (redis.isOpen) {
|
||
await redis.disconnect();
|
||
console.log('\nRedis连接已关闭');
|
||
}
|
||
process.exit();
|
||
}
|
||
}
|
||
|
||
clearAllProjectData();
|