-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathredisHelper.ts
More file actions
146 lines (132 loc) · 3.58 KB
/
redisHelper.ts
File metadata and controls
146 lines (132 loc) · 3.58 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import HawkCatcher from '@hawk.so/nodejs';
import { createClient, RedisClientType } from 'redis';
/**
* Helper class for working with Redis
*/
export default class RedisHelper {
/**
* TTL for lock records in Redis (in seconds)
*/
private static readonly LOCK_TTL = 10;
/**
* Singleton instance
*/
private static instance: RedisHelper | null = null;
/**
* Redis client instance
*/
private redisClient!: RedisClientType;
/**
* Flag to track if we're currently reconnecting
*/
private isReconnecting = false;
/**
* Constructor
* Initializes the Redis client and sets up error handling with auto-reconnect
*/
constructor() {
try {
this.redisClient = createClient({
url: process.env.REDIS_URL,
socket: {
reconnectStrategy: (retries) => {
// Exponential backoff: wait longer between each retry
// Max wait time: 30 seconds
const delay = Math.min(retries * 1000, 30000);
console.log(`[Redis] Reconnecting... attempt ${retries}, waiting ${delay}ms`);
return delay;
},
},
});
// Handle connection errors
this.redisClient.on('error', (error) => {
console.error('[Redis] Client error:', error);
if (error) {
HawkCatcher.send(error);
}
});
// Handle successful reconnection
this.redisClient.on('ready', () => {
console.log('[Redis] Client ready');
this.isReconnecting = false;
});
// Handle reconnecting event
this.redisClient.on('reconnecting', () => {
console.log('[Redis] Client reconnecting...');
this.isReconnecting = true;
});
// Handle connection end
this.redisClient.on('end', () => {
console.log('[Redis] Connection ended');
});
} catch (error) {
console.error('[Redis] Error creating client:', error);
}
}
/**
* Get singleton instance
*/
public static getInstance(): RedisHelper {
if (!RedisHelper.instance) {
RedisHelper.instance = new RedisHelper();
}
return RedisHelper.instance;
}
/**
* Connect to Redis
*/
public async initialize(): Promise<void> {
try {
if (!this.redisClient.isOpen && !this.isReconnecting) {
await this.redisClient.connect();
console.log('[Redis] Connected successfully');
}
} catch (error) {
console.error('[Redis] Connection failed:', error);
HawkCatcher.send(error as Error);
// Don't throw - let reconnectStrategy handle it
}
}
/**
* Close Redis client
*/
public async close(): Promise<void> {
if (this.redisClient.isOpen) {
await this.redisClient.quit();
console.log('[Redis] Connection closed');
}
}
/**
* Check if Redis is connected
*/
public isConnected(): boolean {
return this.redisClient.isOpen;
}
/**
* Execute TS.RANGE command with aggregation
*
* @param key - Redis TimeSeries key
* @param start - start timestamp in milliseconds
* @param end - end timestamp in milliseconds
* @param aggregationType - aggregation type (sum, avg, min, max, etc.)
* @param bucketMs - bucket size in milliseconds
* @returns Array of [timestamp, value] tuples
*/
public async tsRange(
key: string,
start: string,
end: string,
aggregationType: string,
bucketMs: string
): Promise<[string, string][]> {
return (await this.redisClient.sendCommand([
'TS.RANGE',
key,
start,
end,
'AGGREGATION',
aggregationType,
bucketMs,
])) as [string, string][];
}
}