removed configs from all libs and will inject them within the services themselves

This commit is contained in:
Boki 2025-06-18 14:50:47 -04:00
parent fd28162811
commit 6cc5b339bc
32 changed files with 366 additions and 349 deletions

View file

@ -1,6 +1,5 @@
import { EventEmitter } from 'eventemitter3';
import Redis from 'ioredis';
import { dragonflyConfig } from '@stock-bot/config';
import { getLogger } from '@stock-bot/logger';
export interface EventBusMessage {
@ -16,12 +15,21 @@ export interface EventHandler<T = any> {
(message: EventBusMessage & { data: T }): Promise<void> | void;
}
export interface RedisConfig {
host: string;
port: number;
password?: string;
db?: number;
maxRetriesPerRequest?: number;
}
export interface EventBusOptions {
serviceName: string;
enablePersistence?: boolean;
useStreams?: boolean;
maxRetries?: number;
retryDelay?: number;
redisConfig: RedisConfig;
}
export interface StreamConsumerInfo {
@ -53,21 +61,22 @@ export class EventBus extends EventEmitter {
this.retryDelay = options.retryDelay ?? 1000;
this.logger = getLogger(`event-bus:${this.serviceName}`);
const { redisConfig } = options;
this.redis = new Redis({
host: dragonflyConfig.DRAGONFLY_HOST,
port: dragonflyConfig.DRAGONFLY_PORT,
password: dragonflyConfig.DRAGONFLY_PASSWORD,
db: dragonflyConfig.DRAGONFLY_DATABASE || 0,
maxRetriesPerRequest: dragonflyConfig.DRAGONFLY_MAX_RETRIES,
host: redisConfig.host,
port: redisConfig.port,
password: redisConfig.password,
db: redisConfig.db || 0,
maxRetriesPerRequest: redisConfig.maxRetriesPerRequest || 3,
lazyConnect: false,
});
if (!this.useStreams) {
this.subscriber = new Redis({
host: dragonflyConfig.DRAGONFLY_HOST,
port: dragonflyConfig.DRAGONFLY_PORT,
password: dragonflyConfig.DRAGONFLY_PASSWORD,
db: dragonflyConfig.DRAGONFLY_DATABASE || 0,
host: redisConfig.host,
port: redisConfig.port,
password: redisConfig.password,
db: redisConfig.db || 0,
});
this.subscriber.on('message', this.handleRedisMessage.bind(this));
}