-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathindex.ts
More file actions
40 lines (34 loc) · 921 Bytes
/
index.ts
File metadata and controls
40 lines (34 loc) · 921 Bytes
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
import { Redis, RedisOptions } from "ioredis";
import { Logger } from "@trigger.dev/core/logger";
const defaultOptions: Partial<RedisOptions> = {
retryStrategy: (times: number) => {
const delay = Math.min(times * 50, 1000);
return delay;
},
maxRetriesPerRequest: 20,
};
const logger = new Logger("Redis", "debug");
export function createRedisClient(
options: RedisOptions,
handlers?: { onError?: (err: Error) => void }
): Redis {
const client = new Redis({
...defaultOptions,
...options,
});
// Skip error handling setup if running in Vitest
if (process.env.VITEST) {
client.on("error", (error) => {
// swallow errors
});
return client;
}
client.on("error", (error) => {
if (handlers?.onError) {
handlers.onError(error);
} else {
logger.error(`Redis client error:`, { error, keyPrefix: options.keyPrefix });
}
});
return client;
}