Skip to content

Commit b930494

Browse files
committed
Merge branch 'feature/redis-timeseries-helper' into stage
2 parents efec965 + 2bfe2b8 commit b930494

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

src/redisHelper.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export default class RedisHelper {
2222
/**
2323
* Redis client instance
2424
*/
25-
private redisClient!: RedisClientType;
25+
private redisClient: RedisClientType | null = null;
2626

2727
/**
2828
* Flag to track if we're currently reconnecting
@@ -34,6 +34,11 @@ export default class RedisHelper {
3434
* Initializes the Redis client and sets up error handling with auto-reconnect
3535
*/
3636
constructor() {
37+
if (!process.env.REDIS_URL) {
38+
console.warn('[Redis] REDIS_URL not set, Redis features will be disabled');
39+
return;
40+
}
41+
3742
try {
3843
this.redisClient = createClient({
3944
url: process.env.REDIS_URL,
@@ -76,6 +81,7 @@ export default class RedisHelper {
7681
});
7782
} catch (error) {
7883
console.error('[Redis] Error creating client:', error);
84+
this.redisClient = null;
7985
}
8086
}
8187

@@ -93,6 +99,11 @@ export default class RedisHelper {
9399
* Connect to Redis
94100
*/
95101
public async initialize(): Promise<void> {
102+
if (!this.redisClient) {
103+
console.warn('[Redis] Client not initialized, skipping connection');
104+
return;
105+
}
106+
96107
try {
97108
if (!this.redisClient.isOpen && !this.isReconnecting) {
98109
await this.redisClient.connect();
@@ -109,7 +120,7 @@ export default class RedisHelper {
109120
* Close Redis client
110121
*/
111122
public async close(): Promise<void> {
112-
if (this.redisClient.isOpen) {
123+
if (this.redisClient?.isOpen) {
113124
await this.redisClient.quit();
114125
console.log('[Redis] Connection closed');
115126
}
@@ -119,7 +130,7 @@ export default class RedisHelper {
119130
* Check if Redis is connected
120131
*/
121132
public isConnected(): boolean {
122-
return this.redisClient.isOpen;
133+
return Boolean(this.redisClient?.isOpen);
123134
}
124135

125136
/**
@@ -139,6 +150,10 @@ export default class RedisHelper {
139150
aggregationType: string,
140151
bucketMs: string
141152
): Promise<TsRangeResult[]> {
153+
if (!this.redisClient) {
154+
throw new Error('Redis client not initialized');
155+
}
156+
142157
return (await this.redisClient.sendCommand([
143158
'TS.RANGE',
144159
key,

test/integration/api.env

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ MONGO_EVENTS_DB_URL=mongodb://mongodb:27017/hawk_events
99

1010
# Redis URL (optional, for TimeSeries metrics)
1111
# If not set, Redis features will be disabled gracefully
12-
REDIS_URL=redis:6379
12+
REDIS_URL=
1313

1414
# MongoDB settings
1515
MONGO_RECONNECT_TRIES=60

0 commit comments

Comments
 (0)