UNPKG

1.08 kBJavaScriptView Raw
1import redis from 'redis';
2import clui from 'clui';
3
4let countdown = new clui.Spinner();
5let started;
6
7export const createClient = (config, { debug } = { debug: false }) => {
8 const client = redis.createClient({
9 ...config,
10 retry_strategy: ({ attempt, total_retry_time, error, time_connected }) => {
11 if (attempt > 20) {
12 return;
13 }
14
15 return Math.min(attempt * 100, 3000);
16 },
17 });
18
19 client.on('connect', () => {
20 if (debug) {
21 started = false;
22 countdown.stop();
23 }
24 });
25
26 client.on('reconnecting', ({ delay, attempt }) => {
27 if (debug) {
28 countdown.message(
29 `attempting (${attempt}) to reconnect in ${delay}ms...`
30 );
31 if (!started) {
32 started = true;
33 countdown.start();
34 }
35 }
36 });
37
38 process.on('exit', () => {
39 debug && console.log('client is quiting forcefully');
40 client.end(true);
41 });
42
43 return client;
44};
45
46const defaultClient = createClient({
47 url: process.env.REDIS_URL,
48});
49
50export default defaultClient;
51
52export type RedisClientType = typeof defaultClient;