shuttlepro-shared 1.3.5 → 1.3.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/config/redis.js +57 -74
- package/package.json +1 -1
package/config/redis.js
CHANGED
|
@@ -8,53 +8,51 @@ const client = createClient({
|
|
|
8
8
|
url: REDIS_URL,
|
|
9
9
|
socket: { reconnectStrategy: (retries) => Math.min(retries * 50, 1000) }, // Exponential backoff
|
|
10
10
|
});
|
|
11
|
-
|
|
12
|
-
// ✅ Create separate Pub/Sub clients
|
|
13
11
|
const publisher = client.duplicate();
|
|
14
12
|
const subscriber = client.duplicate();
|
|
15
13
|
|
|
16
|
-
// ✅
|
|
17
|
-
client
|
|
18
|
-
client.on("
|
|
19
|
-
client.on("
|
|
20
|
-
client.on("
|
|
14
|
+
// ✅ Redis Events
|
|
15
|
+
const handleEvents = (client, label = "Redis") => {
|
|
16
|
+
client.on("error", (err) => console.error(`❌ ${label} Error:`, err));
|
|
17
|
+
client.on("connect", () => console.log(`✅ ${label} Connected`));
|
|
18
|
+
client.on("ready", () => console.log(`🚀 ${label} Ready to use`));
|
|
19
|
+
client.on("end", () => console.log(`❗ ${label} Connection Closed`));
|
|
20
|
+
client.on("reconnecting", () => console.warn(`🔄 ${label} Reconnecting...`));
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
handleEvents(client, "Main Redis");
|
|
24
|
+
handleEvents(publisher, "Publisher Redis");
|
|
25
|
+
handleEvents(subscriber, "Subscriber Redis");
|
|
21
26
|
|
|
22
|
-
// ✅ Ensure Redis is connected
|
|
27
|
+
// ✅ Ensure Redis is connected once during app startup
|
|
23
28
|
const connectRedis = async () => {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
29
|
+
try {
|
|
30
|
+
if (!client.isOpen) await client.connect();
|
|
31
|
+
if (!publisher.isOpen) await publisher.connect();
|
|
32
|
+
if (!subscriber.isOpen) await subscriber.connect();
|
|
33
|
+
|
|
34
|
+
// Set keyspace events only once
|
|
35
|
+
const config = await client.configGet("notify-keyspace-events");
|
|
36
|
+
if (!config["notify-keyspace-events"]?.includes("Ex")) {
|
|
37
|
+
await client.configSet("notify-keyspace-events", "Ex");
|
|
31
38
|
}
|
|
39
|
+
} catch (err) {
|
|
40
|
+
console.error("❌ Failed to connect to Redis:", err);
|
|
32
41
|
}
|
|
33
|
-
await client.configSet("notify-keyspace-events", "Ex");
|
|
34
42
|
};
|
|
35
43
|
|
|
36
|
-
|
|
37
|
-
* ✅ Publish Data to a Channel
|
|
38
|
-
* @param {string} channel - The Redis Pub/Sub channel
|
|
39
|
-
* @param {any} message - The message to send
|
|
40
|
-
*/
|
|
44
|
+
// ✅ Publish Data to a Channel
|
|
41
45
|
const publishToChannel = async (channel, message) => {
|
|
42
46
|
try {
|
|
43
|
-
await connectRedis();
|
|
44
47
|
await publisher.publish(channel, JSON.stringify(message));
|
|
45
48
|
} catch (err) {
|
|
46
49
|
console.error("❌ Error publishing message:", err);
|
|
47
50
|
}
|
|
48
51
|
};
|
|
49
52
|
|
|
50
|
-
|
|
51
|
-
* ✅ Subscribe & Listen for Messages
|
|
52
|
-
* @param {string} channel - The Redis Pub/Sub channel
|
|
53
|
-
* @param {function} callback - Callback function to handle messages
|
|
54
|
-
*/
|
|
53
|
+
// ✅ Subscribe & Listen for Messages
|
|
55
54
|
const subscribeToChannel = async (channel, callback, expiry = false) => {
|
|
56
55
|
try {
|
|
57
|
-
await connectRedis();
|
|
58
56
|
await subscriber.subscribe(channel, (message) => {
|
|
59
57
|
if (expiry) callback(message);
|
|
60
58
|
else callback(JSON.parse(message));
|
|
@@ -64,13 +62,7 @@ const subscribeToChannel = async (channel, callback, expiry = false) => {
|
|
|
64
62
|
}
|
|
65
63
|
};
|
|
66
64
|
|
|
67
|
-
|
|
68
|
-
* ✅ Set Data in Redis (Supports String & Hash)
|
|
69
|
-
* @param {string} key - The Redis key
|
|
70
|
-
* @param {any} value - The value to store
|
|
71
|
-
* @param {string|null} field - Optional field for Hash storage
|
|
72
|
-
* @param {number} expiryInSeconds - Expiry time in seconds (default: 3600)
|
|
73
|
-
*/
|
|
65
|
+
// ✅ Set Data in Redis (Supports String & Hash)
|
|
74
66
|
const setRedisData = async (
|
|
75
67
|
key,
|
|
76
68
|
value,
|
|
@@ -78,7 +70,6 @@ const setRedisData = async (
|
|
|
78
70
|
expiryInSeconds = 3600
|
|
79
71
|
) => {
|
|
80
72
|
try {
|
|
81
|
-
await connectRedis();
|
|
82
73
|
const stringifiedValue = JSON.stringify(value);
|
|
83
74
|
let result;
|
|
84
75
|
|
|
@@ -99,68 +90,60 @@ const setRedisData = async (
|
|
|
99
90
|
}
|
|
100
91
|
};
|
|
101
92
|
|
|
93
|
+
// ✅ Get Data from Redis (Supports String & Hash)
|
|
94
|
+
const getRedisData = async (key, field = null) => {
|
|
95
|
+
try {
|
|
96
|
+
const result = field
|
|
97
|
+
? await client.hGet(key, field)
|
|
98
|
+
: await client.get(key);
|
|
99
|
+
return result ? JSON.parse(result) : null;
|
|
100
|
+
} catch (err) {
|
|
101
|
+
console.error("❌ Error getting Redis data:", err);
|
|
102
|
+
return null;
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
// ✅ Delete Data from Redis (Supports String & Hash)
|
|
107
|
+
const deleteRedisData = async (key, field = null) => {
|
|
108
|
+
try {
|
|
109
|
+
return field ? await client.hDel(key, field) : await client.del(key);
|
|
110
|
+
} catch (err) {
|
|
111
|
+
console.error("❌ Error deleting Redis data:", err);
|
|
112
|
+
return null;
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
// ✅ Redis Set Operations
|
|
102
117
|
const addDataToRedisSet = async (key, value) => {
|
|
103
118
|
try {
|
|
104
|
-
await connectRedis();
|
|
105
119
|
await client.sAdd(key, value);
|
|
106
120
|
return true;
|
|
107
121
|
} catch (err) {
|
|
122
|
+
console.error("❌ Error adding to Redis set:", err);
|
|
108
123
|
return false;
|
|
109
124
|
}
|
|
110
125
|
};
|
|
111
126
|
|
|
112
127
|
const removeDataFromRedisSet = async (key, value) => {
|
|
113
128
|
try {
|
|
114
|
-
await connectRedis();
|
|
115
129
|
await client.sRem(key, value);
|
|
116
130
|
return true;
|
|
117
131
|
} catch (err) {
|
|
132
|
+
console.error("❌ Error removing from Redis set:", err);
|
|
118
133
|
return false;
|
|
119
134
|
}
|
|
120
135
|
};
|
|
136
|
+
|
|
121
137
|
const isMemberOfRedisSet = async (key, value) => {
|
|
122
138
|
try {
|
|
123
|
-
await connectRedis();
|
|
124
139
|
return await client.sIsMember(key, value);
|
|
125
140
|
} catch (err) {
|
|
126
|
-
console.
|
|
141
|
+
console.error("❌ Error checking membership in Redis set:", err);
|
|
127
142
|
return false;
|
|
128
143
|
}
|
|
129
144
|
};
|
|
130
|
-
/**
|
|
131
|
-
* ✅ Get Data from Redis (Supports String & Hash)
|
|
132
|
-
* @param {string} key - The Redis key
|
|
133
|
-
* @param {string|null} field - Optional field for Hash retrieval
|
|
134
|
-
*/
|
|
135
|
-
const getRedisData = async (key, field = null) => {
|
|
136
|
-
try {
|
|
137
|
-
await connectRedis();
|
|
138
|
-
let result = field ? await client.hGet(key, field) : await client.get(key);
|
|
139
|
-
return result ? JSON.parse(result) : null;
|
|
140
|
-
} catch (err) {
|
|
141
|
-
console.error("❌ Error getting Redis data:", err);
|
|
142
|
-
return null;
|
|
143
|
-
}
|
|
144
|
-
};
|
|
145
145
|
|
|
146
|
-
|
|
147
|
-
* ✅ Delete Data from Redis (Supports String & Hash)
|
|
148
|
-
* @param {string} key - The Redis key
|
|
149
|
-
* @param {string|null} field - Optional field for Hash deletion
|
|
150
|
-
*/
|
|
151
|
-
const deleteRedisData = async (key, field = null) => {
|
|
152
|
-
try {
|
|
153
|
-
await connectRedis();
|
|
154
|
-
return field ? await client.hDel(key, field) : await client.del(key);
|
|
155
|
-
} catch (err) {
|
|
156
|
-
console.error("❌ Error deleting Redis data:", err);
|
|
157
|
-
return null;
|
|
158
|
-
}
|
|
159
|
-
};
|
|
160
|
-
|
|
161
|
-
/**
|
|
162
|
-
* ✅ Graceful Shutdown Handling
|
|
163
|
-
*/
|
|
146
|
+
// ✅ Graceful Shutdown
|
|
164
147
|
const closeClients = async () => {
|
|
165
148
|
if (client.isOpen) await client.quit();
|
|
166
149
|
if (publisher.isOpen) await publisher.quit();
|
|
@@ -184,12 +167,12 @@ module.exports = {
|
|
|
184
167
|
client,
|
|
185
168
|
publisher,
|
|
186
169
|
subscriber,
|
|
170
|
+
connectRedis,
|
|
187
171
|
setRedisData,
|
|
188
172
|
getRedisData,
|
|
189
173
|
deleteRedisData,
|
|
190
174
|
publishToChannel,
|
|
191
175
|
subscribeToChannel,
|
|
192
|
-
connectRedis,
|
|
193
176
|
addDataToRedisSet,
|
|
194
177
|
removeDataFromRedisSet,
|
|
195
178
|
isMemberOfRedisSet,
|