import { Module, DynamicModule, forwardRef } from '@nestjs/common'; import { TypeOrmModule } from '@nestjs/typeorm'; import { BullModule } from '@nestjs/bull'; import { ScheduledWorkflow } from './entities/scheduled-workflow.entity'; import { WorkflowExecutionLog } from './entities/workflow-execution-log.entity'; import { WorkflowScheduleService } from './service/workflow-schedule.service'; import { WorkflowScheduleController } from './controller/workflow-schedule.controller'; import { ScheduleProcessor } from './processors/schedule.processor'; import { WORKFLOW_SCHEDULE_QUEUE } from './constants/schedule.constants'; import { WorkflowAutomationModule } from '../workflow-automation/workflow-automation.module'; import { WorkflowScheduleModuleOptions } from './interfaces/workflow-schedule-options.interface'; /** * Workflow Schedule Module * Manages scheduled workflows and their execution using Bull + Redis * * Usage: * - To enable cron processor: WorkflowScheduleModule.forRoot({ is_workflow: true }) * - To disable cron processor: WorkflowScheduleModule.forRoot({ is_workflow: false }) * - Default (no options): WorkflowScheduleModule.forRoot() - processor DISABLED (safe default) */ @Module({}) export class WorkflowScheduleModule { /** * Configure the WorkflowScheduleModule with options * Use this method in CoreModule/AppModule to control whether the cron processor runs * * @param options - Configuration options * @param options.is_workflow - Set to true to enable scheduler on this server * @returns DynamicModule */ static forRoot(options: WorkflowScheduleModuleOptions = {}): DynamicModule { const { is_workflow = false } = options; // Base providers that are always included const providers: any[] = [WorkflowScheduleService]; // Conditionally add the processor based on is_workflow flag if (is_workflow) { providers.push(ScheduleProcessor); } return { module: WorkflowScheduleModule, global: true, // Make this module global so WorkflowAutomationModule can access exports imports: [ // Register TypeORM entities TypeOrmModule.forFeature([ScheduledWorkflow, WorkflowExecutionLog]), // Register Bull queue for workflow scheduling BullModule.registerQueue({ name: WORKFLOW_SCHEDULE_QUEUE, }), forwardRef(() => WorkflowAutomationModule), // 👈 handle circular dependency ], providers, controllers: [ // REST API controller WorkflowScheduleController, ], exports: [ // Export service for use in other modules WorkflowScheduleService, ], }; } }