Replace initializeStreams with improved ensureGroup method
This commit is contained in:
parent
d49f527cea
commit
bf809a0309
@ -49,48 +49,21 @@ redis.client = Redis.createClient({
|
||||
url: `redis://default@${redisConfig.REDIS_HOST}:${redisConfig.REDIS_PORT}`
|
||||
});
|
||||
|
||||
redis.initializeStreams = async () => {
|
||||
redis.client.on('error', (err) => console.error('Redis error:', err));
|
||||
redis.ensureGroup = async (stream, group) => {
|
||||
await redis.client.connect().catch(console.error);
|
||||
|
||||
try {
|
||||
await redis.client.connect().catch(console.error);
|
||||
|
||||
console.log("Initializing Redis Streams...");
|
||||
await redis.client.xGroupCreate(stream, group, "0", {MKSTREAM: true});
|
||||
|
||||
// Create transcriptions streams consumer group
|
||||
try {
|
||||
await redis.client.xGroupCreate("communication", "mtf-devices", '0', {MKSTREAM: true});
|
||||
|
||||
console.log("Created consumer group: mtf-devices (communication)");
|
||||
} catch (err) {
|
||||
if (err.message.includes('BUSYGROUP')) {
|
||||
console.log("Consumer group already exists: mtf-devices");
|
||||
} else {
|
||||
console.error(err);
|
||||
}
|
||||
}
|
||||
|
||||
const deviceIds = [config.DEVICE_ID];
|
||||
|
||||
for (const deviceId of deviceIds) {
|
||||
const groupName = `${deviceId}_group`;
|
||||
try {
|
||||
await redis.client.xGroupCreate('communication', groupName, '0', {MKSTREAM: true});
|
||||
console.log(`Created consumer group: ${groupName} (communication)`);
|
||||
} catch (err) {
|
||||
if (err.message.includes('BUSYGROUP')) {
|
||||
console.log(`Consumer group already exists: ${groupName}`);
|
||||
} else {
|
||||
console.error(err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
console.log("Streams initialized successfully");
|
||||
await redis.client.quit();
|
||||
console.log(`Created consumer group: ${group} (${stream})`);
|
||||
} catch (err) {
|
||||
console.error("Error initializing streams:", err.message);
|
||||
if (err.message.includes('BUSYGROUP')) {
|
||||
console.log(`Consumer group already exists: ${group} (${stream})`);
|
||||
} else {
|
||||
console.error(err);
|
||||
}
|
||||
}
|
||||
await redis.client.quit();
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
|
||||
@ -17,8 +17,10 @@ const Role = db.role;
|
||||
const authConfig = require("./config/auth.config");
|
||||
const config = require("./config");
|
||||
|
||||
const createApp = () => {
|
||||
redis.initializeStreams();
|
||||
const createApp = async () => {
|
||||
await redis.ensureGroup("speech:commands", `${config.DEVICE_ID}-cg`);
|
||||
await redis.ensureGroup("speech:events", `${config.DEVICE_ID}-cg`);
|
||||
|
||||
const app = express();
|
||||
|
||||
// Security headers
|
||||
|
||||
Loading…
Reference in New Issue
Block a user