-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathredisKeySCript.js
More file actions
70 lines (58 loc) · 2.33 KB
/
Copy pathredisKeySCript.js
File metadata and controls
70 lines (58 loc) · 2.33 KB
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
import { createClient } from "redis";
import dotenv from "dotenv";
// Load environment variables from .env file
dotenv.config();
// Create Redis client
const client = createClient({ host: process.env.REDIS_HOST, port: process.env.REDIS_PORT });
// Connect to Redis
client.on("connect", () => {
console.log("Connected to Redis.");
});
// Handle Redis errors
client.on("error", (err) => {
console.error("Redis error:", err);
// Exit the process to let PM2 restart it
process.exit(1);
});
await client.connect();
// Function to check and update keys
async function updateAgentCallMap() {
try {
// Fetch all agentCallMap keys
const agentCallMapKeys = await client.keys(process.env.ASTERISK_STASIS_APP + ":agentCallMap:*");
if (!agentCallMapKeys.length) {
console.log("No agent mappings.");
return;
}
for (const key of agentCallMapKeys) {
console.log("🚀 ~ updateAgentCallMap ~ key:", key)
const agentCallIds = await client.get(key); // Get the agentCallIds of the agentCallMap key
if (!agentCallIds) continue;
console.log("updateAgentCallMap ~ agentCallIds:", agentCallIds);
// Split the agentCallIds into call IDs
const callIds = agentCallIds.split(",");
const validCallIds = [];
for (const callId of callIds) {
const callDetailsKey = `${process.env.ASTERISK_STASIS_APP}:calls:${callId.slice(1, -1)}:details`;
console.log("🚀 ~ updateAgentCallMap ~ callDetailsKey:", callDetailsKey)
// Check if the call details key exists in Redis
const callDetailsExists = await client.keys(callDetailsKey);
console.log("🚀 ~ updateAgentCallMap ~ callDetailsExists:", callDetailsExists)
if (callDetailsExists.length > 0) {
validCallIds.push(callId); // Add the call ID to the valid list if its details key exists
}
}
// If the valid call IDs differ from the current agentCallIds, update the Redis key
const updatedValue = validCallIds.join(",");
console.log("🚀 ~ updateAgentCallMap ~ updatedValue:", updatedValue)
if (updatedValue !== agentCallIds) {
console.log(`Deleting key ${key}`);
// await client.del(key);
}
}
} catch (err) {
console.error("Error updating agentCallMap:", err);
}
}
// Run the update every 10 seconds
setInterval(updateAgentCallMap, 30000);