2023-08-02 03:51:25 -07:00
|
|
|
import type Redis from 'ioredis';
|
|
|
|
import type { Cluster } from 'ioredis';
|
2023-10-25 07:35:22 -07:00
|
|
|
import { Service } from 'typedi';
|
2023-09-26 04:58:06 -07:00
|
|
|
import config from '@/config';
|
2023-10-25 07:35:22 -07:00
|
|
|
import { Logger } from '@/Logger';
|
2024-06-20 03:55:07 -07:00
|
|
|
import { RedisClientService } from './redis-client.service';
|
2023-08-02 03:51:25 -07:00
|
|
|
|
|
|
|
export type RedisClientType =
|
|
|
|
| 'subscriber'
|
|
|
|
| 'client'
|
|
|
|
| 'bclient'
|
|
|
|
| 'subscriber(bull)'
|
|
|
|
| 'client(bull)'
|
|
|
|
| 'bclient(bull)'
|
|
|
|
| 'client(cache)'
|
|
|
|
| 'publisher'
|
|
|
|
| 'consumer'
|
|
|
|
| 'producer'
|
|
|
|
| 'list-sender'
|
|
|
|
| 'list-receiver';
|
|
|
|
|
|
|
|
export type RedisServiceMessageHandler =
|
|
|
|
| ((channel: string, message: string) => void)
|
|
|
|
| ((stream: string, id: string, message: string[]) => void);
|
|
|
|
|
2023-10-25 07:35:22 -07:00
|
|
|
@Service()
|
2023-08-02 03:51:25 -07:00
|
|
|
class RedisServiceBase {
|
|
|
|
redisClient: Redis | Cluster | undefined;
|
|
|
|
|
|
|
|
isInitialized = false;
|
|
|
|
|
2024-06-20 03:55:07 -07:00
|
|
|
constructor(
|
|
|
|
protected readonly logger: Logger,
|
|
|
|
private readonly redisClientService: RedisClientService,
|
|
|
|
) {}
|
2023-10-25 07:35:22 -07:00
|
|
|
|
2023-08-02 03:51:25 -07:00
|
|
|
async init(type: RedisClientType = 'client'): Promise<void> {
|
|
|
|
if (this.redisClient && this.isInitialized) {
|
|
|
|
return;
|
|
|
|
}
|
2024-06-20 03:55:07 -07:00
|
|
|
this.redisClient = this.redisClientService.createClient({ type });
|
2023-08-02 03:51:25 -07:00
|
|
|
|
|
|
|
this.redisClient.on('error', (error) => {
|
|
|
|
if (!String(error).includes('ECONNREFUSED')) {
|
2023-10-25 07:35:22 -07:00
|
|
|
this.logger.warn('Error with Redis: ', error);
|
2023-08-02 03:51:25 -07:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async destroy(): Promise<void> {
|
|
|
|
if (!this.redisClient) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
await this.redisClient.quit();
|
2023-09-17 02:05:54 -07:00
|
|
|
this.isInitialized = false;
|
2023-08-02 03:51:25 -07:00
|
|
|
this.redisClient = undefined;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export abstract class RedisServiceBaseSender extends RedisServiceBase {
|
|
|
|
senderId: string;
|
|
|
|
|
2023-09-26 04:58:06 -07:00
|
|
|
async init(type: RedisClientType = 'client'): Promise<void> {
|
|
|
|
await super.init(type);
|
|
|
|
this.senderId = config.get('redis.queueModeId');
|
2023-08-02 03:51:25 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export abstract class RedisServiceBaseReceiver extends RedisServiceBase {
|
|
|
|
messageHandlers: Map<string, RedisServiceMessageHandler> = new Map();
|
|
|
|
|
|
|
|
addMessageHandler(handlerName: string, handler: RedisServiceMessageHandler): void {
|
|
|
|
this.messageHandlers.set(handlerName, handler);
|
|
|
|
}
|
|
|
|
|
|
|
|
removeMessageHandler(handlerName: string): void {
|
|
|
|
this.messageHandlers.delete(handlerName);
|
|
|
|
}
|
|
|
|
}
|