Skip to content

Commit 4c8a0d7

Browse files
committed
feat: add redis commands debug logging
1 parent ab834ec commit 4c8a0d7

File tree

3 files changed

+104
-19
lines changed

3 files changed

+104
-19
lines changed

docker/redis.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
notify-keyspace-events Exe

src/RedisStringsHandler.ts

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@ export type CacheEntry = {
1313
tags: string[];
1414
};
1515

16+
export function redisErrorHandler<T extends Promise<unknown>>(
17+
debugInfo: string,
18+
redisCommandResult: T,
19+
): T {
20+
return redisCommandResult.catch((error) => {
21+
console.error('Redis command error', debugInfo, error);
22+
throw error;
23+
}) as T;
24+
}
25+
1626
export type CreateRedisStringsHandlerOptions = {
1727
/** Redis redisUrl to use.
1828
* @default process.env.REDIS_URL? process.env.REDIS_URL : process.env.REDISHOST
@@ -352,9 +362,19 @@ export default class RedisStringsHandler {
352362
const clientGet = this.redisGetDeduplication
353363
? this.deduplicatedRedisGet(key)
354364
: this.redisGet;
355-
const serializedCacheEntry = await clientGet(
356-
getTimeoutRedisCommandOptions(this.timeoutMs),
357-
this.keyPrefix + key,
365+
const serializedCacheEntry = await redisErrorHandler(
366+
'RedisStringsHandler.get(), operation: get' +
367+
(this.redisGetDeduplication ? 'deduplicated' : '') +
368+
this.timeoutMs +
369+
'ms' +
370+
' ' +
371+
this.keyPrefix +
372+
' ' +
373+
key,
374+
clientGet(
375+
getTimeoutRedisCommandOptions(this.timeoutMs),
376+
this.keyPrefix + key,
377+
),
358378
);
359379

360380
debug(
@@ -595,13 +615,17 @@ export default class RedisStringsHandler {
595615

596616
// Setting the cache entry in redis
597617
const options = getTimeoutRedisCommandOptions(this.timeoutMs);
598-
const setOperation: Promise<string | null> = this.client.set(
599-
options,
600-
this.keyPrefix + key,
601-
serializedCacheEntry,
602-
{
618+
const setOperation: Promise<string | null> = redisErrorHandler(
619+
'RedisStringsHandler.set(), operation: set' +
620+
this.timeoutMs +
621+
'ms' +
622+
' ' +
623+
this.keyPrefix +
624+
' ' +
625+
key,
626+
this.client.set(options, this.keyPrefix + key, serializedCacheEntry, {
603627
EX: expireAt,
604-
},
628+
}),
605629
);
606630

607631
debug(
@@ -725,7 +749,16 @@ export default class RedisStringsHandler {
725749
const redisKeys = Array.from(keysToDelete);
726750
const fullRedisKeys = redisKeys.map((key) => this.keyPrefix + key);
727751
const options = getTimeoutRedisCommandOptions(this.timeoutMs);
728-
const deleteKeysOperation = this.client.unlink(options, fullRedisKeys);
752+
const deleteKeysOperation = redisErrorHandler(
753+
'RedisStringsHandler.revalidateTag(), operation: unlink' +
754+
this.timeoutMs +
755+
'ms' +
756+
' ' +
757+
this.keyPrefix +
758+
' ' +
759+
fullRedisKeys,
760+
this.client.unlink(options, fullRedisKeys),
761+
);
729762

730763
// also delete entries from in-memory deduplication cache if they get revalidated
731764
if (this.redisGetDeduplication && this.inMemoryCachingTime > 0) {

src/SyncedMap.ts

Lines changed: 60 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
// SyncedMap.ts
2-
import { Client, getTimeoutRedisCommandOptions } from './RedisStringsHandler';
2+
import {
3+
Client,
4+
getTimeoutRedisCommandOptions,
5+
redisErrorHandler,
6+
} from './RedisStringsHandler';
37
import { debugVerbose, debug } from './utils/debug';
48

59
type CustomizedSync = {
@@ -304,11 +308,22 @@ export class SyncedMap<V> {
304308
if (!this.customizedSync?.withoutRedisHashmap) {
305309
const options = getTimeoutRedisCommandOptions(this.timeoutMs);
306310
operations.push(
307-
this.client.hSet(
308-
options,
309-
this.keyPrefix + this.redisKey,
310-
key as unknown as string,
311-
JSON.stringify(value),
311+
redisErrorHandler(
312+
'SyncedMap.set(), operation: hSet ' +
313+
this.syncChannel +
314+
' ' +
315+
this.timeoutMs +
316+
'ms' +
317+
' ' +
318+
this.keyPrefix +
319+
' ' +
320+
key,
321+
this.client.hSet(
322+
options,
323+
this.keyPrefix + this.redisKey,
324+
key as unknown as string,
325+
JSON.stringify(value),
326+
),
312327
),
313328
);
314329
}
@@ -319,7 +334,18 @@ export class SyncedMap<V> {
319334
value,
320335
};
321336
operations.push(
322-
this.client.publish(this.syncChannel, JSON.stringify(insertMessage)),
337+
redisErrorHandler(
338+
'SyncedMap.set(), operation: publish ' +
339+
this.syncChannel +
340+
' ' +
341+
this.timeoutMs +
342+
'ms' +
343+
' ' +
344+
this.keyPrefix +
345+
' ' +
346+
key,
347+
this.client.publish(this.syncChannel, JSON.stringify(insertMessage)),
348+
),
323349
);
324350
await Promise.all(operations);
325351
}
@@ -345,7 +371,18 @@ export class SyncedMap<V> {
345371
if (!this.customizedSync?.withoutRedisHashmap) {
346372
const options = getTimeoutRedisCommandOptions(this.timeoutMs);
347373
operations.push(
348-
this.client.hDel(options, this.keyPrefix + this.redisKey, keysArray),
374+
redisErrorHandler(
375+
'SyncedMap.delete(), operation: hDel ' +
376+
this.syncChannel +
377+
' ' +
378+
this.timeoutMs +
379+
'ms' +
380+
' ' +
381+
this.keyPrefix +
382+
' ' +
383+
keysArray,
384+
this.client.hDel(options, this.keyPrefix + this.redisKey, keysArray),
385+
),
349386
);
350387
}
351388

@@ -355,7 +392,21 @@ export class SyncedMap<V> {
355392
keys: keysArray,
356393
};
357394
operations.push(
358-
this.client.publish(this.syncChannel, JSON.stringify(deletionMessage)),
395+
redisErrorHandler(
396+
'SyncedMap.delete(), operation: publish ' +
397+
this.syncChannel +
398+
' ' +
399+
this.timeoutMs +
400+
'ms' +
401+
' ' +
402+
this.keyPrefix +
403+
' ' +
404+
keysArray,
405+
this.client.publish(
406+
this.syncChannel,
407+
JSON.stringify(deletionMessage),
408+
),
409+
),
359410
);
360411
}
361412

0 commit comments

Comments
 (0)