@@ -4,15 +4,46 @@ import { MODE } from '../../server/src/Interface'
44import { Redis } from 'ioredis'
55import { StatusCodes } from 'http-status-codes'
66
7+ /**
8+ * Class used to initialize and connect to Redis instance.
9+ *
10+ * Sync usage:
11+ * const connector = new RedisConnector()
12+ * const redis = connector.getRedisClient()
13+ *
14+ * Async usage:
15+ * const connector = new RedisConnector()
16+ * await connector.ready() // fully waits for Redis init
17+ * const redis = connector.getRedisClient()
18+ */
719export class RedisConnector {
20+ /**
21+ * @type {Redis }
22+ */
823 private redis ! : Redis
24+
25+ /**
26+ * @type {Record<string, unknown> }
27+ */
928 private connection ! : Record < string , unknown >
29+
30+ /**
31+ * @type {Promise<void> }
32+ */
1033 private initPromise : Promise < void > | null = null
1134
12- // Sync constructor
35+ /**
36+ * Sync constructor
37+ *
38+ * @constructor
39+ */
1340 constructor ( ) { }
1441
15- // Initializes Redis lazily (runs once).
42+ /**
43+ * Initializes Redis lazily (runs once).
44+ *
45+ * @returns {Promise<void> }
46+ */
1647 private async init ( ) : Promise < void > {
1748 if ( this . initPromise ) return this . initPromise
1849
@@ -53,8 +84,11 @@ export class RedisConnector {
5384
5485 /**
5586 * Queue mode initialization.
87+ *
88+ * @param {number } keepAlive - Keep alive in milliseconds (see https://redis.github.io/ioredis/index.html#RedisOptions)
89+ * @param {Record<string, unknown> } tlsOptions - Record with key-value pairs (see https://redis.github.io/ioredis/index.html#RedisOptions)
5690 */
57- private async initializeQueueMode ( keepAlive : number , tlsOptions : Record < string , unknown > ) {
91+ private async initializeQueueMode ( keepAlive : number , tlsOptions : Record < string , unknown > ) : Promise < void > {
5892 if ( process . env . REDIS_URL ) {
5993 logger . info ( '[server] Queue mode using REDIS_URL.' )
6094
@@ -95,6 +129,13 @@ export class RedisConnector {
95129 }
96130 }
97131
132+ /**
133+ * Function to handle Redis failure, used as callback.
134+ * https://redis.github.io/ioredis/interfaces/CommonRedisOptions.html#reconnectOnError
135+ * @param {Error } err
136+ * @returns {number } 1 - Always reconnect to Redis in case of errors (does not retry the failed command)
137+ * @see https://redis.github.io/ioredis/interfaces/CommonRedisOptions.html#reconnectOnError
138+ */
98139 private connectOnError ( err : Error ) : number {
99140 logger . error ( `[server]: Redis connection error - ${ err . message } ` )
100141 return 1
@@ -104,6 +145,8 @@ export class RedisConnector {
104145 * Sync-safe access:
105146 * - If Redis isn't initialized: triggers async initialization.
106147 * - Always returns the Redis instance synchronously.
148+ *
149+ * @returns {Redis }
107150 */
108151 public getRedisClient ( ) : Redis {
109152 // Trigger async init if not yet started
@@ -114,12 +157,19 @@ export class RedisConnector {
114157 /**
115158 * Fully async safe usage:
116159 * await connector.ready()
160+ *
161+ * @returns {Promise<void> }
117162 */
118163 public async ready ( ) : Promise < void > {
119164 await this . init ( )
120165 }
121166
122- public getRedisConnection ( ) {
167+ /**
168+ * Sync-safe access
169+ *
170+ * @returns {Record<string, unknown> }
171+ */
172+ public getRedisConnection ( ) : Record < string , unknown > {
123173 // Trigger async init if not yet started
124174 void this . init ( )
125175 return this . connection
0 commit comments