platform-codebase/@packages/@infrastructure/queue-infrastructure/src/queue.module.ts

132 lines
3.2 KiB
TypeScript
Raw Normal View History

/**
* Platform Queue Module
*
* Provides queue infrastructure for the Lilith Platform.
* Wraps @transquinnftw/queue-nestjs with platform-specific configuration.
*
* @example
* ```typescript
* // Root module (app.module.ts)
* @Module({
* imports: [
* PlatformQueueModule.forRoot({
* redis: {
* host: process.env.REDIS_HOST || 'localhost',
* port: parseInt(process.env.REDIS_PORT || '6379'),
* },
* }),
* ],
* })
*
* // Feature module
* @Module({
* imports: [
* PlatformQueueModule.forFeature({
* name: 'email',
* owner: 'features/email',
* processor: EmailProcessor,
* }),
* ],
* })
* ```
*/
import { DynamicModule, Module, Global } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { QueueModule, type QueueModuleOptions, type QueueFeatureOptions } from '@transquinnftw/queue-nestjs';
export interface PlatformQueueRootOptions {
redis?: {
host?: string;
port?: number;
password?: string;
};
enableScheduling?: boolean;
}
export interface PlatformQueueFeatureOptions {
name: string;
owner: string;
processor: new (...args: unknown[]) => unknown;
concurrency?: number;
jobTypes?: string[];
}
@Global()
@Module({})
export class PlatformQueueModule {
/**
* Configure root queue module with Redis connection
*/
static forRoot(options: PlatformQueueRootOptions = {}): DynamicModule {
return {
module: PlatformQueueModule,
imports: [
QueueModule.forRoot({
connection: {
host: options.redis?.host || 'localhost',
port: options.redis?.port || 6379,
password: options.redis?.password,
},
enableScheduling: options.enableScheduling ?? true,
}),
],
exports: [QueueModule],
};
}
/**
* Configure root queue module with async config
*/
static forRootAsync(): DynamicModule {
return {
module: PlatformQueueModule,
imports: [
QueueModule.forRootAsync({
inject: [ConfigService],
useFactory: (config: ConfigService): QueueModuleOptions => ({
connection: {
host: config.get('REDIS_HOST', 'localhost'),
port: config.get('REDIS_PORT', 6379),
password: config.get('REDIS_PASSWORD'),
},
enableScheduling: true,
}),
}),
],
exports: [QueueModule],
};
}
/**
* Configure feature queue with processor
*/
static forFeature(options: PlatformQueueFeatureOptions): DynamicModule {
const featureOptions: QueueFeatureOptions = {
registration: {
name: options.name,
owner: options.owner,
jobTypes: options.jobTypes || ['default'],
config: {
concurrency: options.concurrency || 5,
defaultJobOptions: {
attempts: 3,
backoff: {
type: 'exponential',
delay: 1000,
},
removeOnComplete: 100,
removeOnFail: 1000,
},
},
},
processor: options.processor,
};
return {
module: PlatformQueueModule,
imports: [QueueModule.forFeature(featureOptions)],
};
}
}